Chapter 9 Linked Lists 1 Outline Abstract Model
Chapter 9 Linked Lists 1
Outline Abstract Model of a List Obj. Insertion into a List Handling the Back of the List Designing a New LL Structure Inserting a Node at a Position Linked List (LL) nodes Circular Doubly LL’s Node Composition Updating a Doubly LL Inserting at the Front of a LL Deleting from the Front of a LL Removing a Target Node 2
Abstract Model of a List Object 3
Insertion Into a List by Shifting Vector Storage Elements 4
Linked List Nodes n Each Node is like a piece of a chain Individual Piece l Pop Chain To insert a new link, break the chain at the desired location and simply reconnect at both ends of the new piece. 5
Linked List Nodes n Removal is like Insertion in reverse. 6
Node Composition l l 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 7
§- 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 8
§- Inserting/Deleting at the front of a singly linked list 1)Insert - 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 9
Inserting at the Front of a Linked List 10
Deleting From the Front of a Linked List 11
§- 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 12
Removing a Target Node 13
Handling the Back of the List 14
§- 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 15
Designing a New Linked List Structure 16
Singly Linked. List P 1050: Linked. List. h 17
§- 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 18
Inserting a Node at a Position 19
Deleting a Node at a Position 20
Circular Doubly Linked Lists n A Watch Band provides a good Real Life analogue for this Data Structure 21
Circular Doubly Linked Lists n Implemented on a Computer it might look something like this. 22
Updating a Doubly Linked List 23
- Slides: 23