15
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

Variations of Linked Lists

CS 302 – Data Structures

Sections 6.2, 6.3 and 6.4

Page 2: 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

Page 3: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

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

Page 4: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.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 .next.info

Page 5: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

Node structure (cont’d)

template<class ItemType>

struct NodeType {

ItemType info;

NodeType<ItemType>* next;

NodeType<ItemType>* back;

};

Page 6: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

Inserting an item • We no longer need to use prevLocation (we can

get the predecessor of a node using its back member).

prevLocationlocation

Page 7: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

Inserting into a Doubly Linked List

1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location 4. location->back = newNode;

Page 8: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

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.

Page 9: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

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.

Page 10: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

Implement a linked list using arrays

struct NodeType { ItemType info; NodeType* next;};

David Joshua Leah Miriam

Robert

Page 11: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

A linked list as an array of records:

struct NodeType { ItemType info; int next;};

David Joshua Leah Miriam Robert

Page 12: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

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?

Page 13: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

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.

Page 14: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

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?

Page 15: Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4

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