Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)

Similar presentations


Presentation on theme: "C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)"— Presentation transcript:

1 C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)

2 C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 Ordered Linked Lists orderedLinkedList is derived from class linkedListType −We provide the definitions of the abstract functions insertFirst, insertLast, search, and deleteNode We assume that elements of an ordered linked list are arranged in ascending order We include the function insert to insert an element in an ordered list at the proper place

3

4 C++ Programming: From Problem Analysis to Program Design, Fourth Edition4 Ordered Linked Lists (continued)

5 C++ Programming: From Problem Analysis to Program Design, Fourth Edition5 Search the List Steps: −Compare the search item with the current node in the list If the info of the current node is >= to the search item, stop the search Otherwise, make the next node the current node −Repeat Step 1 until an item in the list that >= to the search item is found Or, until no more data is left in the list to compare with the search item

6

7 C++ Programming: From Problem Analysis to Program Design, Fourth Edition7 Insert a Node Case 1: The list is empty

8 C++ Programming: From Problem Analysis to Program Design, Fourth Edition8 Insert a Node (continued) Case 2: List is not empty, and the item to be inserted is smaller than smallest item in list

9 C++ Programming: From Problem Analysis to Program Design, Fourth Edition9 Insert a Node (continued) Case 3: New item is larger than first item −Case 3a: New item is larger than largest item

10 C++ Programming: From Problem Analysis to Program Design, Fourth Edition10 Insert a Node (continued) −Case 3b: Item to be inserted goes somewhere in the middle of the list

11

12 C++ Programming: From Problem Analysis to Program Design, Fourth Edition12 Insert a Node (continued)

13 C++ Programming: From Problem Analysis to Program Design, Fourth Edition13 Insert First and Insert Last

14 C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 Delete a Node Case 1: List is initially empty  error Case 2: Item to be deleted is contained in the first node of the list −We must adjust the head ( first ) pointer Case 3: Item is somewhere in the list − current points to node containing item to be deleted; trailCurrent points to the node just before the one pointed to by current Case 4: Item is not in the list  error

15 C++ Programming: From Problem Analysis to Program Design, Fourth Edition15 Delete a Node (continued)

16

17 C++ Programming: From Problem Analysis to Program Design, Fourth Edition17 Header File of the Ordered Linked List

18

19 C++ Programming: From Problem Analysis to Program Design, Fourth Edition19 Print a Linked List in Reverse Order (Recursion Revisited) Assume current is a pointer to a linked list:

20

21 C++ Programming: From Problem Analysis to Program Design, Fourth Edition21 printListReverse

22 C++ Programming: From Problem Analysis to Program Design, Fourth Edition22 Doubly Linked Lists Doubly linked list: every node has next and back pointers Can be traversed in either direction

23 C++ Programming: From Problem Analysis to Program Design, Fourth Edition23 Doubly Linked Lists (continued) Operations: −Initialize the list −Destroy the list −Determine whether the list is empty −Search the list for a given item −Retrieve the first element of the list −Retrieve the last element of the list

24 C++ Programming: From Problem Analysis to Program Design, Fourth Edition24 Doubly Linked Lists (continued) Operations: (continued) −Insert an item in the list −Delete an item from the list −Find the length of the list −Print the list −Make a copy of the doubly linked list

25 C++ Programming: From Problem Analysis to Program Design, Fourth Edition25 Doubly Linked Lists (continued) The following class defines a doubly linked list as an ADT:

26

27

28

29 C++ Programming: From Problem Analysis to Program Design, Fourth Edition29 Default Constructor

30 C++ Programming: From Problem Analysis to Program Design, Fourth Edition30 isEmptyList

31 C++ Programming: From Problem Analysis to Program Design, Fourth Edition31 Destroy the List This operation deletes all the nodes in the list, leaving the list in an empty state

32 C++ Programming: From Problem Analysis to Program Design, Fourth Edition32 Initialize the List This operation reinitializes the doubly linked list to an empty state

33 C++ Programming: From Problem Analysis to Program Design, Fourth Edition33 Length of the List

34 C++ Programming: From Problem Analysis to Program Design, Fourth Edition34 Print the List

35 C++ Programming: From Problem Analysis to Program Design, Fourth Edition35 Reverse Print the List

36 C++ Programming: From Problem Analysis to Program Design, Fourth Edition36 Search the List

37 C++ Programming: From Problem Analysis to Program Design, Fourth Edition37 First and Last Elements

38 C++ Programming: From Problem Analysis to Program Design, Fourth Edition38 Insert a Node There are four cases: −Case 1: Insertion in an empty list −Case 2: Insertion at the beginning of a nonempty list −Case 3: Insertion at the end of a nonempty list −Case 4: Insertion somewhere in nonempty list Cases 1 and 2 require us to change the value of the pointer first Cases 3 and 4 are similar (after inserting an item, count is incremented by 1)

39

40 C++ Programming: From Problem Analysis to Program Design, Fourth Edition40 Delete a Node Case 1: The list is empty Case 2: The item to be deleted is in the first node of the list, which would require us to change the value of the pointer first Case 3: Item to be deleted is somewhere in the list Case 4: Item to be deleted is not in the list After deleting a node, count is decremented by 1

41

42 C++ Programming: From Problem Analysis to Program Design, Fourth Edition42 Circular Linked Lists Circular linked list: a linked list in which the last node points to the first node

43 C++ Programming: From Problem Analysis to Program Design, Fourth Edition43 Circular Linked Lists (continued) Operations on a circular list are: −Initialize the list (to an empty state) −Determine if the list is empty −Destroy the list −Print the list

44 C++ Programming: From Problem Analysis to Program Design, Fourth Edition44 Circular Linked Lists (continued) Operations on a circular list are: (continued) −Find the length of the list −Search the list for a given item −Insert an item in the list −Delete an item from the list −Copy the list

45 C++ Programming: From Problem Analysis to Program Design, Fourth Edition45 Programming Example: Video Store A new video store in your neighborhood is about to open However, it does not have a program to keep track of its videos and customers The store managers want someone to write a program for their system so that the video store can operate (This topic continues in file 02096_PPT_ch18-3.ppt )


Download ppt "C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)"

Similar presentations


Ads by Google