1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Lists Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
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.
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.
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-5
Doubly-linked list library.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth 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.
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.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Associative Containers Sets Maps Section 4.8. Associative Containers.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CSCI387 Data Structure Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Abstract Data Type (ADT) &List (Part II) Lecture Objectives Implement linked list using pointers Concept of iterators STL's list class Other variations.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Programming Circular Linked List.
Chapter 4 Linked Lists.
Linked List Variations
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Doubly Linked List Implementation
Recursive Linked List Operations
Chapter 4 Linked Lists.
Level 1 STL – Linear DS fb.com/acmicpcfcicu. Static Arrays Creating 1D, 2D, ND Static Arrays. Accessing Time. Traversing a static array with N dimensions.
Chapter 17: Linked Lists.
Lists - I The List ADT.
Lists - I The List ADT.
Doubly Linked List Implementation
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Presentation transcript:

1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation

2 Circular, Doubly-Linked Lists STL list implemented using circular, doubly- linked list (& header node) int A[ ] = { 4, 9, 3, 2 }; list myList (A, A + 4); No data field in header

3 Building Lists Three constructors 0.0 (a) list reals (8); 8:30 (b) list times (6, time24 (8, 30)); array (c) list types (strArr, strArr + 3); listvector

4 CLASS list Constructors list (); Create an empty list. This is the default constructor. list (size_t n, const T& value = T()); Create a list with n elements, each having a specified value. If the value argument is omitted, the elements are filled with the default value for type T. Type T must have a default constructor, and the default value of type T is specified by the notation T(). list (InIter first, InIter last); Initialize the list, using the range [first, last).

Iterators To insert to or remove from middle of vector/list Keeps track of current position in a vector/list Can also be used to traverse a vector/list 5

Traversing a Vector Consider: for (int i = 0; i != v.size(); ++i) cout << v[i] << endl; 6 for (vector ::iterator itr = v.begin(); itr != v.end(); ++i) cout << itr.??? << endl;

7 CLASS list::iterator Operations * :Accesses the value of the item currently pointed to by the iterator.*iter; ++ :Moves the iterator to the next item in the list. iter++; ++ iter; // both pre and post -- :Moves the iterator to the previous item in the list. iter--; --iter;// both pre and post == :Takes two iterators as operands and returns true when they both reference the same item in the list. iter1 == iter2 != :Returns true when the two iterators do not reference the same item in the list. iter1 != iter2

Traversing a Vector Consider: for (int i = 0; i != v.size(); ++i) cout << v[i] << endl; 8 for (vector ::iterator itr = v.begin(); itr != v.end(); ++i) cout << *itr << endl;

Traversing a Vector 9 vector ::iterator itr = v.begin(); while(itr != v.end()) cout << *itr++ << endl; for (vector ::iterator itr = v.begin(); itr != v.end(); ++i) cout << *itr << endl; But why bother?

Operations Requiring Iterators iterator insert(iterator pos, const Object & x) iterator erase(iterator pos) iterator erase(iterator start, iterator end) 10

11 List Insert list li; // then populate list ::iterator newElt = li.insert (iter, 4);

12 List Erase list li; // then populate list ::iterator i = li.erase (iter); cout << *i;

13 Ordered lists // Walk list to find insertion point list ::iterator curr; for (curr = lst.begin (); curr != lst.end () && item > *curr; ++cur) ; lst.insert (curr, item); What if inserting item = 83?

14 Node Composition struct Node { Node (const T& v = T (), Node* n = NULL, Node* p = NULL) : data (v), next (n), prev (p) { } T data; Node* next; Node* prev; };

15 List Class class List { // Insert Node struct def. Node* d; // ptr to dummy header size_t sz; // size public: List () : d (new Node ()), sz (0) { d->next = d->prev = d; } ~List () { while (! empty ()) pop_back (); delete d; } void push_front (const T& v); void push_back (const T& v); void pop_front (); void pop_back (); // Arbitrary location insert and erase };

16 Pushing a Value prev next header newNode Afterinsert:List with one element

17 Pushing Values void push_front (const T& v) { Node* p = d; Node* s = d->next; Node* n; n = new Node (v, s, p); p->next = n; s->prev = n; ++sz; } void push_back (const T& v) { Node* p = d->prev; Node* s = d; Node* n; n = new Node (v, s, p); p->next = n; s->prev = n; ++sz; }

18 Inserting a Node newNode = new Node (item, curr, prevNode); // 2 & 1 prevNode->next = newNode; // 3 curr->prev = newNode; // 4 ++sz; Lobj.insert (curr, item);

19 Erasing a Node curr->prev->next = curr->next; // 1 curr->next->prev = curr->prev; // 2 delete curr; --sz; lObj.erase (curr);