CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 13: 2/28/2003 Lecture 13: 2/28/2003 CS148 Spring 2003
Outline Operations on linked lists Insertion in a sorted linked list Deletion from a linked list Lecture 13: 2/28/2003 CS148 Spring 2003
Linked Lists struct node { int ID; //Node ID string name; Data Component Link A node in a linked list struct node { int ID; //Node ID string name; node* next; //pointer to next node }; HEAD 1 2 (N-1) N points to NULL (the end of the list) A linked list (HEAD gives the location of the first node in the list) Lecture 13: 2/28/2003 CS148 Spring 2003
Insertion in order struct node { int ID; //Node ID string name; node* next; //pointer to next node }; HEAD 1 2 8 9 5 Old link NewNode previous current previous->next= NewNode; NewNode->next = current; Lecture 13: 2/28/2003 CS148 Spring 2003
Deletion of a node struct node { int ID; //Node ID string name; node* next; //pointer to next node }; HEAD 1 2 8 9 previous current Delete this node previous->next = current->next; delete current; Lecture 13: 2/28/2003 CS148 Spring 2003