Variations of Linked Lists CS 302 Data Structures

  • Slides: 15
Download presentation
Variations of Linked Lists CS 302 – Data Structures Sections 6. 2, 6. 3

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

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

Circular Linked Lists (cont’d) – To have access to both the first and last nodes of the list, make list. Data point to the last node

Doubly-Linked Lists: Node structure • info: the user's data • next, back: the address

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 Item. Type> struct Node. Type { Item. Type info; Node.

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

Inserting an item • We no longer need to use prev. Location (we can

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

Inserting into a Doubly Linked List 1. new. Node->back = location->back; 2. new. Node->next

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

Headers and Trailers • Special cases arise when we are dealing • • with

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 •

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 Joshua Leah Miriam Robert struct Node. Type

Implement a linked list using arrays David Joshua Leah Miriam Robert struct Node. Type { Item. Type info; Node. Type* next; };

A linked list as an array of records: struct Node. Type { Item. Type

A linked list as an array of records: struct Node. Type { Item. Type info; int next; }; David Joshua Leah Miriam Robert

A linked list as an array of records: How would you Insert/Delete items? Can

A linked list as an array of records: How would you Insert/Delete items? Can you implement Binary Search efficiently? David Joshua Leah Miriam Robert

Why would one implement a linked list using arrays? • Integer indices take up

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

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

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