C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.

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++
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
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.
Data Structures & Algorithms
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.
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.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List 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 17 Linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Linked Lists Dr. Youssef Harrath
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
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)
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.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Data Structures Using C++1 Chapter 5 Linked Lists.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
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:
Data Structure & Algorithms
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 16: Linked Lists.
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
UNIT-3 LINKED LIST.
Linked lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 18: Linked Lists.
Indirection.
Chapter 16 Linked Structures
Linked lists.
Chapter 9 Linked Lists.
Presentation transcript:

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 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: From Problem Analysis to Program Design, Fourth Edition3 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: From Problem Analysis to Program Design, Fourth Edition4 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: From Problem Analysis to Program Design, Fourth Edition5 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: From Problem Analysis to Program Design, Fourth Edition6 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: From Problem Analysis to Program Design, Fourth Edition7 Linked Lists: Some Properties

C++ Programming: From Problem Analysis to Program Design, Fourth Edition8 Linked Lists: Some Properties (continued) current = head; −Copies value of head into current

C++ Programming: From Problem Analysis to Program Design, Fourth Edition9 Linked Lists: Some Properties (continued) current = current->link;

C++ Programming: From Problem Analysis to Program Design, Fourth Edition10 Linked Lists: Some Properties (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition11 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: From Problem Analysis to Program Design, Fourth Edition12 Traversing a Linked List (continued) To traverse a linked list: Example:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition13 Item Insertion and Deletion Consider the following definition of a node: We will use the following variable declaration:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 Insertion Consider the following linked list: A new node with info 50 is to be created and inserted after p

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:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition17 Insertion (continued) Using two pointers, we can simplify the insertion code somewhat To insert newNode between p and q : The order in which these statements execute does not matter

C++ Programming: From Problem Analysis to Program Design, Fourth Edition19 Deletion Node with info 34 is removed from the list, but memory is still occupied; node is dangling

C++ Programming: From Problem Analysis to Program Design, Fourth Edition21 Building a Linked List 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: From Problem Analysis to Program Design, Fourth Edition22 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: From Problem Analysis to Program Design, Fourth Edition25 Building a Linked List Forward (continued) We now repeat statements 1 through 6b three more times:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition27 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: From Problem Analysis to Program Design, Fourth Edition29 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: From Problem Analysis to Program Design, Fourth Edition30 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: From Problem Analysis to Program Design, Fourth Edition31 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 abstract class linkedListType will implement the basic linked list operations −Derived classes: unorderedLinkedList and orderedLinkedList

C++ Programming: From Problem Analysis to Program Design, Fourth Edition32 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: From Problem Analysis to Program Design, Fourth Edition33 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: From Problem Analysis to Program Design, Fourth Edition34 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: From Problem Analysis to Program Design, Fourth Edition35 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: From Problem Analysis to Program Design, Fourth Edition36 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 two member variables: One to refer to (the current) node One to refer to the node just before the (current) node

C++ Programming: From Problem Analysis to Program Design, Fourth Edition38 Linked List Iterators (continued)

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition46 Length of a List

C++ Programming: From Problem Analysis to Program Design, Fourth Edition47 Retrieve the Data of the First Node

C++ Programming: From Problem Analysis to Program Design, Fourth Edition48 Retrieve the Data of the Last Node

C++ Programming: From Problem Analysis to Program Design, Fourth Edition49 Begin and End

C++ Programming: From Problem Analysis to Program Design, Fourth Edition50 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: From Problem Analysis to Program Design, Fourth Edition53 Destructor

C++ Programming: From Problem Analysis to Program Design, Fourth Edition54 Copy Constructor

C++ Programming: From Problem Analysis to Program Design, Fourth Edition55 Overloading the Assignment Operator

C++ Programming: From Problem Analysis to Program Design, Fourth Edition56 Unordered Linked Lists We derive the class unorderedLinkedList from the abstract class linkedListType and implement the operations search, insertFirst, insertLast, and deleteNode

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition59 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: From Problem Analysis to Program Design, Fourth Edition61 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: From Problem Analysis to Program Design, Fourth Edition63 Insert the Last Node

C++ Programming: From Problem Analysis to Program Design, Fourth Edition64 Delete a Node Case 1: List is 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: From Problem Analysis to Program Design, Fourth Edition65 Delete a Node (continued) −Second scenario: List of more than one node

C++ Programming: From Problem Analysis to Program Design, Fourth Edition66 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: From Problem Analysis to Program Design, Fourth Edition67 Delete a Node (continued) −Case 3b: Node to be deleted is the last node

C++ Programming: From Problem Analysis to Program Design, Fourth Edition68 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: From Problem Analysis to Program Design, Fourth Edition71 Header File of the Unordered Linked List

C++ Programming: From Problem Analysis to Program Design, Fourth Edition72 Header File of the Unordered Linked List (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition73 Ordered Linked Lists (This topic continues in file 02096_PPT_ch18-2.ppt )