View
219
Download
0
Tags:
Embed Size (px)
Citation preview
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
– To have access to both the first and last nodes of the list, make listData point to the last node
Circular Linked Lists (cont’d)
Doubly-Linked Lists:Node structure
• info: the user's data
• next, back: the address of the next and previous node in the list
.back .next.info
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).
prevLocationlocation
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
struct NodeType { ItemType info; NodeType* next;};
David Joshua Leah Miriam
Robert
A linked list as an array of records:
struct NodeType { ItemType info; int next;};
David Joshua Leah Miriam Robert
A linked list as an array of records:
How would you Insert/Delete items?
David Joshua Leah Miriam Robert
Can you implement Binary Search efficiently?
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?
Case Study: Implementing a large integer ADT (cont.)