Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming:. Program Design Including

Similar presentations


Presentation on theme: "C++ Programming:. Program Design Including"— Presentation transcript:

1 C++ Programming:. Program Design Including
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists

2 Objectives In this chapter, you will: Learn about linked lists
Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list Learn how to construct a doubly linked list C++ Programming: Program Design Including Data Structures, Fourth Edition

3 Introduction Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is slow Sorted array: insertion and deletion is slow C++ Programming: Program Design Including Data Structures, Fourth Edition

4 Linked Lists Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node Link field in last node is NULL C++ Programming: Program Design Including Data Structures, Fourth Edition

5 Linked Lists (continued)
Example: Suppose that the first node is at memory location 1200, and the second node is at memory location 1575 C++ Programming: Program Design Including Data Structures, Fourth Edition

6 Linked Lists (continued)
Because each node of a linked list has two components, we need to declare each node as a class or struct Data type of a node depends on the specific application The link component of each node is a pointer C++ Programming: Program Design Including Data Structures, Fourth Edition

7 Linked Lists: Some Properties
C++ Programming: Program Design Including Data Structures, Fourth Edition

8 Linked Lists: Some Properties (continued)
current = head; Copies value of head into current C++ Programming: Program Design Including Data Structures, Fourth Edition

9 Linked Lists: Some Properties (continued)
current = current->link; C++ Programming: Program Design Including Data Structures, Fourth Edition

10 Linked Lists: Some Properties (continued)
C++ Programming: Program Design Including Data Structures, Fourth Edition

11 Traversing a Linked List
The basic operations of a linked list are: Search to determine if an item is in the list Insert an item in the list Delete an item from the list Traversal: given a pointer to the first node of the list, step through the nodes of the list C++ Programming: Program Design Including Data Structures, Fourth Edition

12 Traversing a Linked List (continued)
To traverse a linked list: Example: C++ Programming: Program Design Including Data Structures, Fourth Edition

13 Item Insertion and Deletion
Consider the following definition of a node: We will use the following variable declaration: C++ Programming: Program Design Including Data Structures, Fourth Edition

14 Insertion Consider the following linked list:
A new node with info 50 is to be created and inserted after p C++ Programming: Program Design Including Data Structures, Fourth Edition

15

16 The sequence of statements to insert the node is very important
The sequence of statements to insert the node is very important. Suppose that we reverse the sequence of the statements and execute the statements in the following order:

17 Deletion Node with info 34 is removed from the list, but memory is still occupied; node is dangling C++ Programming: Program Design Including Data Structures, Fourth Edition

18

19 Building a Linked List (version 1)
This method’s propety: If data is unsorted: The list will be unsorted Can build a linked list forward or backward Forward: a new node is always inserted at the end of the linked list Backward: a new node is always inserted at the beginning of the list C++ Programming: Program Design Including Data Structures, Fourth Edition

20 Building a Linked List Forward
You need three pointers to build the list: One to point to the first node in the list, which cannot be moved One to point to the last node in the list One to create the new node C++ Programming: Program Design Including Data Structures, Fourth Edition

21

22

23 Building a Linked List Forward (continued)
We now repeat statements 1 through 6b three more times: C++ Programming: Program Design Including Data Structures, Fourth Edition

24

25 Building a Linked List Backward
The algorithm is: Initialize first to NULL For each item in the list Create the new node, newNode Store the item in newNode Insert newNode before first Update the value of the pointer first C++ Programming: Program Design Including Data Structures, Fourth Edition

26

27 Linked List as an ADT The basic operations on linked lists are:
Initialize the list Determine whether the list is empty Print the list Find the length of the list Destroy the list C++ Programming: Program Design Including Data Structures, Fourth Edition

28 Linked List as an ADT (continued)
The basic operations on linked lists are: (continued) Retrieve the info contained in the first node Retrieve the info contained in the last node Search the list for a given item Insert an item in the list Delete an item from the list Make a copy of the linked list C++ Programming: Program Design Including Data Structures, Fourth Edition

29 Linked List as an ADT (continued)
In general, there are two types of linked lists: Sorted and unsorted lists The algorithms to implement the operations search, insert, and remove slightly differ for sorted and unsorted lists C++ Programming: Program Design Including Data Structures, Fourth Edition

30 Linked List as an ADT (continued)
If a linked list is unordered, we can insert a new item at either the end or the beginning buildListForward inserts item at the end buildListBackward inserts new item at the beginning To accommodate both operations, we will write two functions: insertFirst and insertLast We will use two pointers in the list: first and last C++ Programming: Program Design Including Data Structures, Fourth Edition

31 Structure of Linked List Nodes
The node has two member variables To simplify operations such as insert and delete, we define the class to implement the node of a linked list as a struct The definition of the struct nodeType is: C++ Programming: Program Design Including Data Structures, Fourth Edition

32 Member Variables of the class linkedListType
We use two pointers: first and last We also keep a count of the number of nodes in the list linkedListType has three member variables: C++ Programming: Program Design Including Data Structures, Fourth Edition

33 Linked List Iterators One of the basic operations performed on a list is to process each node of the list List must be traversed, starting at first node Common technique is to provide an iterator Iterator: object that produces each element of a container, one element at a time The two most common operations are: ++ (the increment operator) * (the dereferencing operator) C++ Programming: Program Design Including Data Structures, Fourth Edition

34 Linked List Iterators (continued)
Note that an iterator is an object We need to define a class (linkedListIterator) to create iterators to objects of the class linkedListType Would have one member variable: Refers to (the current) node C++ Programming: Program Design Including Data Structures, Fourth Edition

35 Good Use for Templates Since a node can contain anything (not just an int), we want the nodeClass to be templated. template <class Type> class nodeType { private: Type info; nodeType<Type> * link }; C++ Programming: Program Design Including Data Structures, Fourth Edition

36

37

38

39

40

41

42 Print the List C++ Programming: Program Design Including Data Structures, Fourth Edition

43 Length of a List C++ Programming: Program Design Including Data Structures, Fourth Edition

44 Retrieve the Data of the First Node
C++ Programming: Program Design Including Data Structures, Fourth Edition

45 Retrieve the Data of the Last Node
C++ Programming: Program Design Including Data Structures, Fourth Edition

46 Begin and End C++ Programming: Program Design Including Data Structures, Fourth Edition

47 Copy the List Steps: Create a node, and call it newNode
Copy the info of the node (in the original list) into newNode Insert newNode at the end of the list being created C++ Programming: Program Design Including Data Structures, Fourth Edition

48

49

50 Destructor C++ Programming: Program Design Including Data Structures, Fourth Edition

51 Copy Constructor C++ Programming: Program Design Including Data Structures, Fourth Edition

52 Overloading the Assignment Operator
C++ Programming: Program Design Including Data Structures, Fourth Edition

53

54 Search the List Steps: Compare the search item with the current node in the list If the info of the current node is the same as the search item, stop the search Otherwise, make the next node the current node Repeat Step 1 until the item is found Or, until no more data is left in the list to compare with the search item C++ Programming: Program Design Including Data Structures, Fourth Edition

55

56 Insert the First Node Steps: Create a new node
Store the new item in the new node Insert the node before first Increment count by 1 C++ Programming: Program Design Including Data Structures, Fourth Edition

57

58 Insert the Last Node C++ Programming: Program Design Including Data Structures, Fourth Edition

59 Delete a Node Case 1: List is empty Case 2: List is not empty
If the list is empty, output an error message Case 2: List is not empty The node to be deleted is the first node First scenario: List has only one node C++ Programming: Program Design Including Data Structures, Fourth Edition

60 Delete a Node (continued)
Second scenario: List of more than one node C++ Programming: Program Design Including Data Structures, Fourth Edition

61 Delete a Node (continued)
Case 3: Node to be deleted is not the first one Case 3a: Node to be deleted is not last one C++ Programming: Program Design Including Data Structures, Fourth Edition

62 Delete a Node (continued)
Case 3b: Node to be deleted is the last node C++ Programming: Program Design Including Data Structures, Fourth Edition

63 Delete a Node (continued)
Case 4: Node to be deleted is not in the list The list requires no adjustment Simply output an error message C++ Programming: Program Design Including Data Structures, Fourth Edition

64

65

66 Header File of the Unordered Linked List
C++ Programming: Program Design Including Data Structures, Fourth Edition

67 Header File of the Unordered Linked List (continued)
C++ Programming: Program Design Including Data Structures, Fourth Edition


Download ppt "C++ Programming:. Program Design Including"

Similar presentations


Ads by Google