Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists Chapter 4.

Similar presentations


Presentation on theme: "Linked Lists Chapter 4."— Presentation transcript:

1 Linked Lists Chapter 4

2 Chapter 4 -- Linked Lists
This chapter introduces you to C++ pointers and the data structure linked list. You will see algorithms for fundamental linked list operations such as insertion and deletion. The chapter also describes several variations of the basic linked list. As you will see you can use a linked list and its variations when implementing many of the ADTs that appear throughout the remainder of this book CS 308 Chapter 4 -- Linked Lists

3 Chapter 4 -- Linked Lists
Preliminaries Issues with Array based implementations of Lists: Insert and Delete require shifting CS 308 Chapter 4 -- Linked Lists

4 Chapter 4 -- Linked Lists
Pointers A pointer is a data type A variable can be declared to be a pointer. int *p; It stores an address. Therefore you have to get the address of something int q; p= &q; It has operations. *p=6; CS 308 Chapter 4 -- Linked Lists

5 Chapter 4 -- Linked Lists
Example: (a) declaration (b) & (c) * (d) new CS 308 Chapter 4 -- Linked Lists

6 Chapter 4 -- Linked Lists
Example (cont): CS 308 Chapter 4 -- Linked Lists

7 Chapter 4 -- Linked Lists
Example (cont): CS 308 Chapter 4 -- Linked Lists

8 Chapter 4 -- Linked Lists
Example: Pointers also have issues Creation of Garbage Dangling Pointers CS 308 Chapter 4 -- Linked Lists

9 Chapter 4 -- Linked Lists
Dynamic Allocation of Arrays Normal allocation (automatic or static) const int MAX_SIZE = 50; double anArray[MAX_SIZE]; Dynamic Allocation int arraySize=50; double *anArray = new double[arraySize]; CS 308 Chapter 4 -- Linked Lists

10 Chapter 4 -- Linked Lists
Deleting the memory delete [ ] anArray; The name of the array is equivalent to the address of the first element. anArray == &(anArray[0]) *anArray == anArray[0] How do you allocate a 2D array dynamically? CS 308 Chapter 4 -- Linked Lists

11 Chapter 4 -- Linked Lists
Pointer-Based Linked Lists Nodes: Declaration struct Node { int item; Node *next }; Allocation Node *p; p = new Node; CS 308 Chapter 4 -- Linked Lists

12 Chapter 4 -- Linked Lists
Usage (*p).item = 3 p->item = 3 Head Node Declaration: Node *head; CS 308 Chapter 4 -- Linked Lists

13 Programming with Linked Lists
The previous section illustrated how you can use pointer variables to implement a linked list This section begins by developing algorithms for displaying the data portions of such a linked list. For inserting and deleting items from a list. CS 308 Chapter 4 -- Linked Lists

14 Chapter 4 -- Linked Lists
Displaying the Contents of a Linked List Suppose we have a list How do we display it? The key is making a traversal of the nodes CS 308 Chapter 4 -- Linked Lists

15 Chapter 4 -- Linked Lists
Deleting a Specified Node from a Linked List General case Special case (prev == NULL) CS 308 Chapter 4 -- Linked Lists

16 Chapter 4 -- Linked Lists
Inserting a Node into a Specified Position of a Linked List General case Special Case (prev == NULL) CS 308 Chapter 4 -- Linked Lists

17 Chapter 4 -- Linked Lists
Special Case (curr == NULL) CS 308 Chapter 4 -- Linked Lists

18 Chapter 4 -- Linked Lists
A Pointer Based Implementation of the ADT List Code was covered last semester, and in Lab 9 Key is shallow vs deep copy CS 308 Chapter 4 -- Linked Lists

19 Chapter 4 -- Linked Lists
Comparing Array-Based and Pointer-Based Implementations Storage: Arrays are easy to use, but they have a fixed size An array based implementation is good for a small list. Access You can access array items directly with equal access time. You must traverse a linked list to access its ith node Insertion and Deletion Insertion and deletion from a linked list do not require you to shift the data (they do require traversal) CS 308 Chapter 4 -- Linked Lists

20 Chapter 4 -- Linked Lists
Saving and Restoring a Linked List by Using a File. Notes: Do not write the pointers to the file Write only its data to a file. Saving: Traverse the list and write out the data Restoring Read the data and insert at the end of the list CS 308 Chapter 4 -- Linked Lists

21 Chapter 4 -- Linked Lists
Passing a Linked List to a Function A function with access to a linked list’s head pointer has access to the entire list. Pass the head pointer as a reference argument Note: a linked list passed as an argument is not copied, even if its head pointer is passed as a value argument. CS 308 Chapter 4 -- Linked Lists

22 Chapter 4 -- Linked Lists
Processing Linked Lists Recursively Traversal void writeList(Node * nodeptr) Insertion void linkedListInsert(Node * & headPtr, ListItemType newItem); CS 308 Chapter 4 -- Linked Lists

23 Variations of the Linked List
This section introduces several variations of the linked list that you have just seen. Circular Linked Lists Dummy Head nodes Doubly Linked Lists CS 308 Chapter 4 -- Linked Lists

24 Chapter 4 -- Linked Lists
Circular Linked Lists CS 308 Chapter 4 -- Linked Lists

25 Chapter 4 -- Linked Lists
Dummy Head Nodes CS 308 Chapter 4 -- Linked Lists

26 Chapter 4 -- Linked Lists
Doubly Linked Lists CS 308 Chapter 4 -- Linked Lists

27 Chapter 4 -- Linked Lists
Doubly Linked Lists Insertion Deletion CS 308 Chapter 4 -- Linked Lists

28 Chapter 4 -- Linked Lists
CS 308 Chapter 4 -- Linked Lists


Download ppt "Linked Lists Chapter 4."

Similar presentations


Ads by Google