150 likes | 166 Vues
Learn how to extend linear linked lists into circular linked lists and implement doubly linked lists, including inserting items and using headers/trailers. Understand the benefits of implementing linked lists using arrays.
E N D
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4
Circular Linked Lists • Extending a linear linked list to a circular linked list • Make the last node point back to the first node
Circular Linked Lists (cont’d) • To have access to both the first and last nodes of the list, make listData point to the last node
Doubly-Linked Lists:Node structure • info: the user's data • next, back: the address of the next and previous node in the list .back .info .next
Node structure (cont’d) template<class ItemType> struct NodeType { ItemType info; NodeType<ItemType>* next; NodeType<ItemType>* back; };
Inserting an item • We no longer need to use prevLocation (we can get the predecessor of a node using its back member). location prevLocation
Inserting into a Doubly Linked List 1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location 4. location->back = newNode;
Headers and Trailers • Special cases arise when we are dealing with the first or last nodes. • Idea: make sure that we never insert or delete the ends of the list! • How? Set up dummy nodes with values outside the range of possible values.
Headers and Trailers (cont.) • Header Node: contains a value smaller than any possible list element. • Trailer Node: contains a value larger than any possible list element.
Implement a linked list using arrays David Leah Joshua Miriam Robert struct NodeType { ItemType info; NodeType* next; };
A linked list as an array of records: struct NodeType { ItemType info; int next; }; David Robert Leah Joshua Miriam
A linked list as an array of records: How would you Insert/Delete items? Can you implement Binary Search efficiently? David Robert Leah Joshua Miriam
Why would one implement a linked list using arrays? • Integer indices take up less memory than pointers. • Store/Read lists to/from files more efficiently.
Case Study: Implementing a large integer ADT • The range of integer values varies from one computer to another. • For long integers, the range is typically [-2,147,483,648 to 2,147,483,647] • How can we manipulate larger integers?