Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.

Similar presentations


Presentation on theme: "CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in."— Presentation transcript:

1 CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in the list. No “previous node”. Can we get rid of this special case?

2 CPSC 252 Linked Lists III Page 2 Variations on Singly Linked Lists Head Nodes / Dummy Nodes A dummy node is not used to store data, it is created when the list is created. An empty list with a dummy node looks like: head

3 CPSC 252 Linked Lists III Page 3 Insertions go after the dummy node: This code always works, assuming that previousNode is a pointer to the node after which the new node is to be inserted: Node* newNode = makeNode( item, previousNode->next ); previousNode->next = newNode; head item

4 CPSC 252 Linked Lists III Page 4 Deletions always have a previous node: This code always works, assuming that previousNode is a pointer to the node before the one we are deleting: previousNode->next = victim->next; or we can say previousNode->next = previousNode->next->next ; head item1 item2 victim

5 CPSC 252 Linked Lists III Page 5 Dummy node summary Simplifies our algorithms for insertion and deletion. Uses more memory, the empty list is bigger (and slower to create).

6 CPSC 252 Linked Lists III Page 6 Linked list application Logged in users on a shared computer. List size grows and shrinks as necessary. On logoff, we can remove a user from the middle. The sharing policy is time-slicing – each user gets access to the system’s resources for some fixed period of time. The “which user goes next” code has a special case for the end of the list. Could we get rid of this special case?

7 CPSC 252 Linked Lists III Page 7 Circular Linked Lists The node at the end of the list contains a link to the node at the head of the list: item1item2item3item4 head

8 CPSC 252 Linked Lists III Page 8 Another problem with our lists What if we want to traverse our list backwards? Say, to search for the last occurrence of an item. Or what if we want to delete an element when we have a pointer to it (from the find operation)?

9 CPSC 252 Linked Lists III Page 9 Doubly Linked Lists Each node has a pointer to the previous node as well as a pointer to the next node. Such a list looks as follows: head item1 item2item3item4 Doubly Linked Circular Lists First points (back) to last and last points (forward) to first.

10 CPSC 252 Linked Lists III Page 10 Let’s look at doubly linked circular lists with a dummy node typedef int Item_type; struct Node { Item_type item; Node *next, *prev; }; We need a makeEmpty() function that creates an empty list (one with only a dummy node, but no “real”nodes)

11 CPSC 252 Linked Lists III Page 11 Implementing the makeEmpty() function Node *makeEmpty() { Node *head = new Node; head->next = head; head->prev = head; return head; }

12 CPSC 252 Linked Lists III Page 12 Implementing the insertFirst() function void insertFirst( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: item is inserted at the front of the list { Node* newNode = new Node; newNode->item = item; newNode->next = ???; newNode->prev = ???; ???->prev = newNode; ???->next = newNode; }

13 CPSC 252 Linked Lists III Page 13 Implementing the insertLast() function void insertLast( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: item is inserted at the end of the list { Node* newNode = new Node; newNode->item = item; newNode->next = ???; newNode->prev = ???; ???->prev = newNode; ???->next = newNode; }

14 CPSC 252 Linked Lists III Page 14 Implementing the find() function – does the old version work? Node* find( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: if item is in the list, a pointer to the node //containing it is returned; otherwise a null pointer //is returned { Node* cursor = head; while( cursor != NULL && cursor->item != item ) cursor = cursor->next; return cursor; }

15 CPSC 252 Linked Lists III Page 15 Implementing the find() function –the new version Node* find( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: if item is in the list, a pointer to the node //containing it is returned; otherwise a null pointer //is returned { Node* cursor = head->next; while( cursor != head && cursor->item != item ) cursor = cursor->next; return cursor == head ? NULL : cursor; }

16 CPSC 252 Linked Lists III Page 16 Implementing the remove() function void remove( Node* victim ) //Pre: victim points to the node to be removed //Post: the victim node is removed from the list { }

17 CPSC 252 Linked Lists III Page 17 Implementing the remove() function void remove( Node* victim ) //Pre: victim points to the node to be removed //Post: the victim node is removed from the list { victim->next->prev = victim->prev; victim->prev->next = victim->next; }

18 CPSC 252 Linked Lists III Page 18 If we have two link fields in our nodes, we could also maintain two lists through one set of data. Suppose we want to sort students by last name and student number. We can maintain a list of students sorted by both criteria by using our two link fields and two head pointers: nameList Smith 5647 numList Jones 3623 Wong 1287


Download ppt "CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in."

Similar presentations


Ads by Google