Linked Lists Useful when the number of elements is not known in advance or varies widely during execution Allows efficient insertion and removal, sequential.

Slides:



Advertisements
Similar presentations
QUEUE Education is the best friend. An educated person is respected everywhere. Education beats the beauty and the youth. Chanakya.
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Data Structures: A Pseudocode Approach with C
Data Structures & Algorithms
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Containers Overview and Class Vector
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
C++ STL CSCI 3110.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
Data Structures Using C++
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
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 Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Computer Engineering Rabie A. Ramadan Lecture 6.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
IndexedListWithIteratorsViaLinear1Ind
Object-Oriented Programming (OOP) Lecture No. 41
CS505 Data Structures and Algorithms
Pointers and Linked Lists
Chapter 12 – Data Structures
Copy Constructor / Destructors Stacks and Queues
Big-O notation Linked lists
Review Deleting an Element from a Linked List Deletion involves:
Dr. Bernard Chen Ph.D. University of Central Arkansas
CISC181 Introduction to Computer Science Dr
Standard Template Library (STL)
Chapter 4 Linked Lists.
Stacks and Queues.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Chapter 16-2 Linked Structures
Object Oriented Programming COP3330 / CGS5409
Data Structures and Database Applications Stacks in C#
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Indirection.
Recursive Linked List Operations
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
Containers: Queue and List
Chapter 5 Stack (part 1).
STL List.
Linked Lists.
Chapter 9 Linked Lists.
The List Container and Iterators
STL List.
Presentation transcript:

Linked Lists Useful when the number of elements is not known in advance or varies widely during execution Allows efficient insertion and removal, sequential access HeadABC

Useful Linked List Functions Constructors and assignment: –list L; // default constructor –list L (L2); // copy constructor –L = L2;// assignment operator Element access: –L.front();// first item in the list –L.back();// last item in the list Size: –L.empty();// true if list is empty –L.size();// returns the number of items

Useful Linked List Functions (cont) Iterators: –list ::iterator i;// declare a new iterator –L.begin();// starting iterator –L.end();// ending iterator Insertion and removal: –L.push_front(value);// add value to front of the list –L.push_back(value);// add value to end of the list –L.insert(iterator,value);// add value at specified location –L.pop_front();// remove item from front of list –L.pop_back();// remove item from end of list –L.erase(iterator);// remove referenced item –L.remove(value);// remove all occurrences of value

Linked List Functions - Examples Constructors: –list L1;// create a new (empty) list // of integers –list L2;// create a new (empty) list // of pointers to widgets –list L3;// create a new (empty) list // of widgets –list L4 (L1);// copy constructor

Linked List Functions – Examples (cont) Assignment: –list L3;// create a new (empty) list // of widgets –list L5; // create a new (empty) list // of widgets –…// add some items to L3 –L5 = L3;// assignment

Linked List Functions – Examples (cont) Adding elements: –L.push_front(7); L123L7132

Linked List Functions – Examples (cont) Adding elements: –L.push_back(7); L123L1273

Linked List Functions – Examples (cont) Adding elements: –L.insert(L.end(),8); L123L1283

Linked List Functions – Examples (cont) Adding elements: –list ::iterator loc = find(L.begin(),L.end(),2); –loc = L.insert(loc,8); L123L1832

Linked List Functions – Examples (cont) Deleting elements: –L.pop_front(); L123L32

Linked List Functions – Examples (cont) Deleting elements: –L.pop_back(); L123L21

Linked List Functions – Examples (cont) Deleting elements: –L.remove(17); L83L1783

Linked List Functions – Examples (cont) Deleting elements: –list ::iterator i = find(L.begin(),L.end(),3); –L.erase(i); L124L1243

Linked List Functions – Examples (cont) Deleting elements: –list ::iterator start = find(L.begin(),L.end(),2); –list ::iterator stop = find(L.begin(),L.end(),5); –L.erase(start,stop); L12534L125

Linked List Functions – Examples (cont) Number of elements: –cout << There are << L.size() << elements in the list 5 L12534

Linked List Functions – Examples (cont) Number of elements: –if (L1.empty()) cout << L1 is empty –if (!L2.empty()) cout << L2 is not empty L1 is empty L2 is not empty L L2

Linked List Functions – Examples (cont) Number of elements: –int num=0; –count(L1.begin(),L.end(),7,num); –cout << L1 contains << num << 7s L1 contains 2 7s L

Linked List Functions – Examples (cont) Miscellaneous: –L1.sort(); L

Linked List Functions – Examples (cont) Miscellaneous: –L1.reverse(); L

Insert Iterators Assignment to an iterator is normally an overwriting operation (replaces the contents of the target): copy(L2.begin(),L2.end(),L1.begin()); L183L2125L1125

Insert Iterators (cont) For lists (and sets) often instead want to perform insertion. Can use a list insertion iterator: copy(L2.begin(),L2.end(),back_inserter(L1)); L183L2125L183512

Insert Iterators (cont) In addition to back_inserter (which adds one list to the end of another) there is also: –front_inserter – adds one list to the front of another –Inserter – inserts one list in another at the position pointed to by an iterator An insert iterator is a form of adaptor –An adaptor changes the interface of an object but does little or no work itself –The insert iterator changes the list insert interface into the iterator interface

Example Program – Inventory System A business, World Wide Widget Works, manufactures widgets class Widget { public: Widget():id_number(0){} // constructor Widget(int a):id(a) {} // constructor // operations int id() {return id_number}; void operator = (Widget & rhs) {id.number = rhs.id_number;} bool operator == (Widget & rhs) {id.number == rhs.id_number;} bool operator < (Widget & rhs) {id.number < rhs.id_number;} protected: int id_number; // widget identification number };