1 / 15

# Variations of Linked Lists - PowerPoint PPT Presentation

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).

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
1. Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

2. Circular Linked Lists • Extending a linear linked list to a circular linked list • Make the last node point back to the first node

3. 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

4. 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

5. Node structure (cont’d) template<class ItemType> struct NodeType { ItemType info; NodeType<ItemType>* next; NodeType<ItemType>* back; };

6. Inserting an item • We no longer need to use prevLocation (we can get the predecessor of a node using its back member). location prevLocation

7. Inserting into a Doubly Linked List 1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location 4. location->back = newNode;

8. 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.

9. 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.

10. Implement a linked list using arrays David Leah Joshua Miriam Robert struct NodeType { ItemType info; NodeType* next; };

11. A linked list as an array of records: struct NodeType { ItemType info; int next; }; David Robert Leah Joshua Miriam

12. 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

13. 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.

14. 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?

15. Case Study: Implementing a large integer ADT (cont.)