Purpose: | Insert a new node into an ordered linked list based on the value of the key. |
Brief: |
Set a pointer to the head of the list and one to follow along behind.
Compare the key in the new
node to the key in the node in the list. If the key in the new
node is greater than the key in the node in the list then advance
the pointers to the next node in the list. Continue until the
insertion point is found. Set all pointers to insert the new
node into the list. Reset the head of the list if the new item
was inserted before the existing head. |
Cases: |
(1) Inserting first node into an empty list. (2) Inserting node at the head of the list. (3) Inserting node at the tail of the list. (4) Inserting node somewhere in the middle of the list. |
Function Prototype: |
void Insert(Nodetype *newNode); |
Algorithm: |
If head of the list == NULL // head is a member variable set newNode as head of new list (head = newNode) (Case 1) Else set a temporary pointer to the head of the list set a back pointer to NULL Find position to insert new node Loop till location is found or temp pointer falls off the end of the list If newNode->key < temp->key location has been found else advance back to temp and temp to temp->next end loop Insert the newNode If back == NULL insert newNode as new head of list (Case 2) newNode->next = head set newNode as head of new list (head = newNode) Else insert into middle of list or at end (Cases 3 and 4) newNode->next = temp back->next = newNode |
Purpose: | Delete a node from an ordered linked list. |
Brief: |
Set a pointer to the head of the list and one to follow along behind.
Compare the search key to the key in the node in the list. If the keys
do not match then advance the pointers to the next node. If the keys
do match then Set all pointers to remove the node from the list.
Free the removed node. Reset the head of the list if the first node
was the one deleted. |
Cases: |
(1) Deleting node at the head of the list. (2) Deleting node somewhere in the middle of the list. (3) Deleting node at the end of the list. (4) Node to delete was not found. |
Function Prototype: |
void Delete(KeyType key); |
Algorithm: |
Set a temporary pointer to the head of the list and a back pointer to NULL // Note: head is a member variable Find position of node to delete Loop till location is found or temp pointer falls off the end of the list If newNode->key == temp->key location has been found else advance back to temp and temp to temp->next end loop if temp == NULL node to delete was not found (Case 4) return with list unchanged Delete the Node If back == NULL delete node at head of list (Case 1) head = head->next free node pointed to by temp reset new head of list Else delete from middle of list or at end (Cases 2 and 3) back->next = temp->next free node pointed to by temp |
Purpose: | Locate a node containing a specified key in a linked list. |
Brief: |
Set a pointer to the head of the list and move the pointer, in turn,
to each node in the list comparing the key in the node to the search key.
Continue until the search key is found or the search fails. |
Cases: |
(1) List is empty, nothing to search (2) Search key is found (3) Search key is not found |
Function Prototype: |
NodeType *Search(KeyType key); |
Algorithm: |
If head == NULL // head is a member variable return NULL (Case 1) Else set temp = head Loop while temp NOT NULL If search key matches key in current node return pointer to current node (Case 2) else advance temp to temp->next End loop; return NULL (Case 3) |