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

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Review Learn about linked lists
DATA STRUCTURE & ALGORITHMS
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Chapter 4 ADT Sorted List.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 17: Linked Lists.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Linked Lists Dr. Youssef Harrath
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
Data Structures Using C++ 2E
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
Data Structures Using Java1 Chapter 4 Linked Lists.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Structures Using C++1 Chapter 5 Linked Lists.
Linked Lists Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 17 © 2002 Addison Wesley.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 3)
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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 C++ Programming:
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
LINKED LISTS.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Chapter 16: Linked Lists.
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Data Structures Using C++ 2E
Linking in double direction
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Binary Trees, Binary Search Trees
Chapter 4 Link Based Implementations
List Implementations Chapter 9.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Applications of Arrays
Presentation transcript:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 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

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

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

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

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

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

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

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

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

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

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

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

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:

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

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

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

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

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:

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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 )