Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.

Similar presentations


Presentation on theme: "1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays."— Presentation transcript:

1 1 Linked List

2 List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays

3 Lists A list is an ordered set of data. It is often used to store objects that are to be processed sequentially.

4 Arrays An array is an indexed set of variables, such as dancer [1], dancer [2], dancer [3],… It is like a set of boxes that hold things. A list is a set of items. An array is a set of variables that each store an item.

5 Arrays and Lists You can see the difference between arrays and lists when you delete items.

6 Arrays and Lists In a list, the missing spot is filled in when something is deleted.

7 Arrays and Lists In an array, an empty variable is left behind when something is deleted.

8 What’s wrong with Array and Why lists? Disadvantages of arrays as storage data structures: –slow searching in unordered array –slow insertion in ordered array –Fixed size Linked lists solve some of these problems Linked lists are general purpose storage data structures and are versatile.

9 Linked Lists Each data item is embedded in a link. Each Link object contains a reference to the next link in the list of items. In an array items have a particular position, identified by its index. In a list the only way to access an item is to traverse the list

10 Operations in a simple linked list: Insertion Deletion Searching or Iterating through the list to display items.

11 © 2005 Pearson Addison-Wesley. All rights reserved4-11 Preliminaries Figure 4.1 a) A linked list of integers; b) insertion; c) deletion

12 © 2005 Pearson Addison-Wesley. All rights reserved4-12 Pointer-Based Linked Lists A node in a linked list is usually a struct struct Node { int item Node *next; }; //end struct A node is dynamically allocated Node *p; p = new Node; Figure 4.6 A node

13 © 2005 Pearson Addison-Wesley. All rights reserved4-13 Pointer-Based Linked Lists The head pointer points to the first node in a linked list If head is NULL, the linked list is empty Executing the statement the statement head=new Node before head=NULL will result in a lost cell

14 © 2005 Pearson Addison-Wesley. All rights reserved4-14 Pointer-Based Linked Lists Figure 4.7 A head pointer to a list Figure 4.8 A lost cell

15 © 2005 Pearson Addison-Wesley. All rights reserved4-15 Displaying the Contents of a Linked List Reference a node member with the -> operator p->item; A traverse operation visits each node in the linked list –A pointer variable cur keeps track of the current node for (Node *cur = head; cur != NULL; cur = cur->next) cout item << endl;

16 © 2005 Pearson Addison-Wesley. All rights reserved4-16 Displaying the Contents of a Linked List Figure 4.9 The effect of the assignment cur = cur->next

17 © 2005 Pearson Addison-Wesley. All rights reserved4-17 Deleting a Specified Node from a Linked List Deleting an interior node prev->next=cur->next; Deleting the first node head=head->next; Return deleted node to system cur->next = NULL; delete cur; cur=NULL;

18 © 2005 Pearson Addison-Wesley. All rights reserved4-18 Deleting a Specified Node from a Linked List Figure 4.10 Deleting a node from a linked list Figure 4.11 Deleting the first node

19 © 2005 Pearson Addison-Wesley. All rights reserved4-19 Inserting a Node into a Specified Position of a Linked List To insert a node between two nodes newPtr->next = cur; prev->next = newPtr; Figure 4.12 Inserting a new node into a linked list

20 © 2005 Pearson Addison-Wesley. All rights reserved4-20 Inserting a Node into a Specified Position of a Linked List To insert a node at the beginning of a linked list newPtr->next = head; head = newPtr; Figure 4.13 Inserting at the beginning of a linked list

21 © 2005 Pearson Addison-Wesley. All rights reserved4-21 Inserting a Node into a Specified Position of a Linked List Inserting at the end of a linked list is not a special case if cur is NULL newPtr->next = cur; prev->next = newPtr; Figure 4.14 Inserting at the end of a linked list

22 © 2005 Pearson Addison-Wesley. All rights reserved4-22 Inserting a Node into a Specified Position of a Linked List Determining the point of insertion or deletion for a sorted linked list of objects for(prev = NULL, cur= head; (cur != null)&& (newValue > cur->item); prev = cur, cur = cur->next;

23 © 2005 Pearson Addison-Wesley. All rights reserved4-23 Comparing Array-Based and Pointer-Based Implementations Size –Increasing the size of a resizable array can waste storage and time Storage requirements –Array-based implementations require less memory than a pointer-based ones

24 © 2005 Pearson Addison-Wesley. All rights reserved4-24 Comparing Array-Based and Pointer-Based Implementations Access time –Array-based: constant access time –Pointer-based: the time to access the i th node depends on i Insertion and deletions –Array-based: require shifting of data –Pointer-based: require a list traversal

25 © 2005 Pearson Addison-Wesley. All rights reserved4-25 Circular Linked Lists Last node references the first node Every node has a successor No node in a circular linked list contains NULL Figure 4.25 A circular linked list

26 © 2005 Pearson Addison-Wesley. All rights reserved4-26 Dummy Head Nodes Dummy head node –Always present, even when the linked list is empty –Insertion and deletion algorithms initialize prev to reference the dummy head node, rather than NULL Figure 4.27 A dummy head node

27 © 2005 Pearson Addison-Wesley. All rights reserved4-27 Doubly Linked Lists Each node points to both its predecessor and its successor Circular doubly linked list –precede pointer of the dummy head node points to the last node –next reference of the last node points to the dummy head node –No special cases for insertions and deletions

28 © 2005 Pearson Addison-Wesley. All rights reserved4-28 Doubly Linked Lists Figure 4.29 (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

29 © 2005 Pearson Addison-Wesley. All rights reserved4-29 Doubly Linked Lists To delete the node to which cur points (cur->precede)->next = cur->next; (cur->next)->precede = cur->precede; To insert a new node pointed to by newPtr before the node pointed to by cur newPtr->next = cur; newPtr->precede = cur->precede; cur->precede = newPtr; newPtr->precede->next = newPtr;

30 © 2005 Pearson Addison-Wesley. All rights reserved4-30 Application: Maintaining an Inventory Operations on the inventory –List the inventory in alphabetical order by title (L command) –Find the inventory item associated with title (I, M, D, O, and S commands) –Replace the inventory item associated with a title (M, D, R, and S commands) –Insert new inventory items (A and D commands)


Download ppt "1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays."

Similar presentations


Ads by Google