Chapter 9 Linked Lists
Outline Abstract Model of a List Obj. Handling the Back of the List Insertion into a List Linked List (LL) nodes Node Composition Inserting at the Front of a LL Deleting from the Front of a LL Removing a Target Node Handling the Back of the List Designing a New LL Structure Inserting a Node at a Position Circular Doubly LL’s Updating a Doubly LL
Abstract Model of a List Object
Insertion Into a List by Shifting Vector Storage Elements
Linked List Nodes Each Node is like a piece of a chain Individual Piece Pop Chain To insert a new link, break the chain at the desired location and simply reconnect at both ends of the new piece.
Linked List Nodes Removal is like Insertion in reverse.
Node Composition An individual Node is composed of two parts, a Data field containing the data stored by the node, and a Pointer field that marks the address of the next Node in the list. Node Class: Page 1049
§- singly linked lists - Each node contains a value and a pointer to the next node in the list. - The list begins with a pointer to the first node of the list and terminates when a node has a NULL pointer. 8
§- Inserting/Deleting at the front of a singly linked list - Set the pointer in the new node to the previous value of front. - update front to point at the new node. 2) Erase - assign front the pointer value of the first node, and then delete the node. 9
Inserting at the Front of a Linked List
Deleting From the Front of a Linked List
§- Inserting/Erasing inside a singly linked list - Maintain a pointer to the current list node and a pointer to the previous node. - Change the pointer value in the previous node. 12
Removing a Target Node
Handling the Back of the List
§- Insert/Delete at the back of a singly linked list - Maintain a pointer to the last list node that has value NULL when the list is empty. - Assign a “back” pointer the address of the first node added to the list. - To add other nodes at the back: 1) allocate a new node 2) assign the pointer in node “back” to point to the new node 3) assign the pointer “back” the address of the new node. 15
Designing a New Linked List Structure
Singly LinkedList P1050: LinkedList.h
§- Doubly linked lists - provide the most flexible implementation for the sequential list. §- Its nodes have pointers to the next and the previous node, so the program can traverse a list in either the forward or backward direction. - Traverse a list by starting at the first node and follow the sequence of next nodes until you arrive back at the header. - To traverse a list in reverse order, start at the last node and follow the sequence of previous nodes until arriving back at the header. dnode Class 18
Inserting a Node at a Position
Deleting a Node at a Position
Circular Doubly Linked Lists A Watch Band provides a good Real Life analogue for this Data Structure
Circular Doubly Linked Lists Implemented on a Computer it might look something like this.
Updating a Doubly Linked List