A Journey 2 Eternity

Tracking the Head Element of a Singly Linked List

Posted on: August 29, 2008

The head element of a singly-linked list must always be tracked; otherwise, the list will be lost in memory. This means that the pointer or reference to the head of the list must be updated when a new element is inserted ahead of the first element or when the existing first element is removed from the list. Tracking the head element becomes a problem when you alter the list inside a function or method, because the caller must be made aware of the new head element.

bool InsertInFront( ListElement **head, int data )
{
	ListElement *newElem = new ListElement;
	if( !newElem ) {
		return false;
	}

	newElen->data = data;
	*head = newElem;

	return true;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Pages

Categories

August 2008
M T W T F S S
« Apr   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Blog Stats

  • 26,968 hits
%d bloggers like this: