1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.

Slides:



Advertisements
Similar presentations
COMP171 Fall 2005 Lists.
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked Lists CENG 213 Data Structures.
What is a Queue? A queue is a FIFO “first in, first out” structure.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Classes and Data Structures Jeffrey S. Childs
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data Structures Using C++
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
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.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Abstract Data Types and a review of C++ programming concepts CS 400/600 – Data Structures.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 11 Hash Tables Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
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 and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 16: Linked Lists.
C++ Classes and Data Structures Jeffrey S. Childs
Doubly linked lists.
Chapter 16-2 Linked Structures
C++ Classes and Data Structures Jeffrey S. Childs
C++ Plus Data Structures
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Chapter 16 Linked Structures
CENG 218 Classes and Data Structures
CSI 1340 Introduction to Computer Science II
Lists CMSC 202, Version 4/02.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 Chapter 7 The Linked List as a Data Structure

2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list operations. Abstract data type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. It supports support abstraction, encapsulation, and information hiding.

3 List ADT Operations first, returns the first element in the linked list. insertFront, to insert a new item into the front of the list. insertBack, to insert a new item into the back of the list. find, to determine whether or not a certain item exists in a list. remove, to remove an item from a list. IsEmpty, to determine whether or not the list is empty. makeEmpty, to empty out the list.

4 Retrieving Elements When the client needs to retrieve an element in the list, the main practical reason is because it contains information that the client doesn’t have. Yet, the clients must know something about it; otherwise, they would not be able to tell the List object to look for it. The clients know about the key...

5 Keys A key is a value that uniquely identifies an object –If objects are people, a good key would be the SSN –books – ISBN key –parts – part number key The elements in a list ADT are usually objects – the key is just a single data member of the object.

6 An Example A customer of an insurance company has a problem with the amount paid by the insurance company for an operation. The customer calls the insurance company. The insurance company asks the customer for the claim number (the key). The customer provides the claim number.

7 An Example (cont.) The insurance company representative types the claim number (key) into the computer. The claim number is typed into a program which is using one or more data structures. The retrieve function of a data structure is called, passing in the claim number (key).

8 An Example (cont.) The retrieve function searches the data structure for the object that has the key. The retrieve function finds the object and returns the object. All the data in the object is now provided to the main program. The main program shows all the data on the screen.

9 An Example (cont.) The insurance company representative looks at the data. The insurance company representative can now see what the customer is complaining about.

10 List Implementation In C++, lists can be implemented with arrays or linked lists. Recall 2 advantages of linked lists –conserve memory for large objects (such as objects with keys). –can easily remove an element from the middle. So, we’ll focus on using the linked list. Instead of saying “linked-list implementation of a list”, we’ll just say “linked list”.

11 LinkedList Implementation A general linked list is more involved than the linked list queue or the linked list stack. The client must be able to access, change, or remove any element in the linked list at any time. It should be implemented to handle key- oriented types of objects, but also be general enough to handle other objects without keys, like strings.

12 LinkedList.hpp 1 template 2 struct Node { 3T info; 4Node *next; 5 }; 6 7 template 8 class LinkedList { 9 Node *start; 10 public: 11LinkedList();

13 LinkedList.hpp (cont.) 15~LinkedList( ); 16 void insertFront ( T & element ); 17 void insertBack ( T & element ); 18bool first( T & listEl ); 19bool find ( T & element ); 20bool remove( T & element ); 21bool isEmpty( ); 22void makeEmpty( ); 23 };

14 Constructor & Destructor LinkedList.cpp 1 2 LinkedList ::LinkedList ( ) 3 { 4start = NULL; 5 } 6 7 LinkedList :: ~LinkedList( ) 8 { 9makeEmpty( ); 10 }

15 insert 20 void insertFront (T & newElement) 21 { 22Node *newNode = new Node ; 23newNode->info = newElement; 24newNode->next = start; 25start = newNode; 26 } Inserting at the beginning of the linked list.

16 insertBack 30 void LinkedList ::insertBack (T & newElement) 31 { 32Node *newNode = new Node ; 33newNode->info = newElement; 34newNode->next = NULL; 35if (start == NULL) { // list is empty. 36start = newNode; 37return; 38} 39Node * ptr = start; 40while (ptr->next != NULL) // list is not empty. 41ptr = ptr->next; 42ptr->next = newNode; 43 } Inserting at the end of the linked list.

17 first 49 bool LinkedList ::first( T & listEl ) 50 { 51if ( start == NULL ) 52return false; 53 54listEl = start->info; 55return true; 56 }

18 find 73 bool LinkedList ::find( T & element ) 74 { 75bool found = false; 76Node * ptr = start; 77while (ptr != NULL && !found) { 78if (ptr->info == target) 79found = true; 80if (!found) 81ptr = ptr->next; 82} 83return found; 84 } Overloaded operator if T is a struct object

19 remove We need to keep ptr one node in front of the node to remove, so the first node is a special case. remove continued… 105 bool LinkedList ::remove( T & element ) 106 { 107if ( start == NULL ) 108return false; 109Node *ptr = start; 110if ( ptr->info == element ) { 111start = start->next; 112delete ptr; 113return true; 114}

20 remove 105 bool LinkedList ::remove( T & element ) 106 { 107if ( start == NULL ) 108return false; 109Node *ptr = start; 110if ( ptr->info == element ) { 111start = start->next; 112delete ptr; 113return true; 114} remove continued… Case 1: List is empty

21 remove 105 bool LinkedList ::remove( T & element ) 106 { 107if ( start == NULL ) 108return false; 109Node *ptr = start; 110if ( ptr->info == element ) { 111start = start->next; 112delete ptr; 113return true; 114} remove continued… Case 2: Element is the first element of the list

22 remove (cont.) 117while ( ptr->next != NULL ) { 118if ( ptr->next->info == element ) { 119Node *tempPtr = ptr->next; 120ptr->next = tempPtr->next; 121delete tempPtr; 122return true; 123} 124ptr = ptr->next; 125} return false; 128 } Case 3

23 isEmpty 132 bool LinkedList ::isEmpty( ) 133 { 134return start == NULL; 135 }

24 makeEmpty 137 void LinkedList ::makeEmpty( ) 138 { 139while ( start != NULL ) { 140Node * ptr = start; 141start = start->next; 142delete ptr; 143} 144 }

Reference Childs, J. S. (2008). The Linked List as a Data Structure. C++ Classes and Data Structures. Prentice Hall. 25