A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
DATA STRUCTURES USING C++ Chapter 5
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 …
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 11 Implementing.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Reference: Vinu V Das, Principles of Data Structures using C and C++
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
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.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Data Structures Using C++1 Chapter 5 Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
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.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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.
  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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Linked lists.
Chapter 4 Linked Lists
Data Structures and Algorithms IT12112
LinkedList Class.
Chapter 16-2 Linked Structures
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Linked Lists.
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Doubly Linked List Implementation
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
The Class chain.
Programming II (CS300) Chapter 07: Linked Lists
Doubly Linked List Implementation
Linked lists.
Chapter 9 Linked Lists.
The List Container and Iterators
16 – Sequential Containers
Data Structures & Programming
Presentation transcript:

A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1

Doubly Linked List Implementation template <typename T> class dnode { public: // the members of a dnode object are used for operations within a // doubly linked list; access is simplified by making them public T nodeValue; // data value of the node dnode<T> *prev; // previous node in the list dnode<T> *next; // next node in the list // default constructor. creates object with value T(), the default value // of type T. set the node pointers to point at the node itself dnode(); // constructor with an argument to initialize nodeValue. // set the node pointers to point at the node itself dnode(const T& value): nodeValue(value); }; 2

Default and Copy Constructors Constructor creates an empty list by assigning this to both left and right. template <typename T> dnode<T>::dnode() { next = this; // the next node is the current node prev = this; // the previous node is the current node } dnode<T>::dnode(const T& value): nodeValue(value) 3

Node Insertion current node prev next prev prev next prevNode newNode dnode<T> *newNode, *prevNode; // allocate a new node and assign prevNode to reference the predecessor of curr newNode = new dnode<T>(item); prevNode = curr->prev; // update pointer fields in newNode newNode->prev = prevNode; newNode->next = curr; // update curr and its predecessor to point to newNode prevNode->next = newNode; curr->prev = newNode; 4

The insert() Function template <typename T> dnode<T> *insert(dnode<T> *curr, const T& item) { // declare pointer variables for the new node and the previous node dnode<T> *newNode, *prevNode; // allocate new dnode with item as initial value newNode = new dnode<T>(item); // assign prevNode the pointer value of node before p prevNode = curr->prev; // update pointer fields in newNode newNode->prev = prevNode; newNode->next = curr; // update curr and prevNode to point at newNode prevNode->next = newNode; curr->prev = newNode; return newNode; } 5

Node Deletion – Self Removal curr prevNode succNode prev next next prev prev next dnode<T> *prevNode = curr->prev, *succNode = curr->next; prevNode -> next = succNode; succNode -> prev = prevNode; delete curr; 6

The erase() Function template <typename T> void erase(dnode<T> *curr) { // return if the list is empty if (curr->next == curr) return; // declare pointers for the predecessor and successor nodes dnode<T> *prevNode = curr->prev, *succNode = curr->next; // update pointer fields for predecessor and successor prevNode->next = succNode; succNode->prev = prevNode; // deallocate the memory used by the node delete curr; } 7

Circular Doubly Linked Lists header Simplify coding. Avoid tests to determine if the list is empty. Remove the additional code for updating the head pointer. prev header next An empty circular linked list header -> next == header header-> prev == header 8

Scanning a Doubly Linked List template <typename T> void writeDlinkedList(dnode<T>* header, const string& separator = “ “) { // header points at first dnode. p moves through the list dnode<T> *p = header->next; while (p != header) cout << p->nodeValue << separator; p = p->next; } 9

The miniList Class A variation of the STL list class. A doubly linked list as the underlying structure. Implementation of an iterator. Private members: dnode<T> *header; int listSize; dnode<T> *getDNode(const T& item); // allocate a dnode dnode<T> *dinsert(dnode<T> *curr, const T& item); // insert before node curr and // return address of the new node void derase(dnode<T> *curr); // erase node curr from the linked list 10

A Constructor template <typename T> miniList<T>::miniList(int n, const T& value): listSize(n) { int i; // create an empty list header = new dnode<T>; if (header == NULL) throw memoryAllocationError (“miniList(): memory allocation failure”); // insert n copies of value at the front of the list for (i = 0; i < n; i++) dinsert(header->next, value); } 11

Copy Constructor template <typename T> miniList<T>::miniList(const miniList<T>& obj): listSize(obj.listSize) { // curr moves through the nodes in obj, and end marks the finish // of a traversal through obj dnode<T> *curr = obj.header->next, *end = obj.header; // create an empty list header = new dnode<T>; if (header == NULL) throw memoryAllocationError (“miniList(): memory allocation failure”); // insert the values in the linked list obj.header at the back of the current list while (curr != end) dinsert(header, curr->nodeValue); // before the header, i.e., back of the list curr = curr -> next; } 12

Iterator Nested Class template <typename T> class miniList { class iterator { public: friend class miniList<T>; // needed by the const_iterator constructor // that converts a const iterator to a const_iterator friend class const_iterator; // constructor iterator() {} bool operator==(const iterator& rhs) const; bool operator!= (const iterator& rhs) const; T& operator* (); // pointer dereference operator iterator& operator++ (); iterator operator++ (int) ; // postfix increment. move forward one node iterator& operator-- (); iterator operator-- (int); private: dnode<T> *nodePtr; // pointer to the current list node // private constructor. converts p to an iterator // by assigning p to nodePtr iterator(dnode<T> *p): nodePtr(p) {} }; template <typename T> class miniList { public: // include the iterator // nested classes #include “d_liter.h” … private: dnode<T> *header; int listSize; dnode<T> *getDNode( const T& item); dnode<T> *dinsert( dnode<T> *curr, const T& item); void derase(dnode<T> *curr); }; 13

Implementing Iterator Operations T& operator* () { // if the node's successor is itself, the list is empty if (nodePtr->next == nodePtr) throw referenceError("miniList iterator: reference error"); return nodePtr->nodeValue; } iterator& operator++ () nodePtr = nodePtr->next; // move to the successor of nodePtr return *this; // return new iterator value iterator operator++ (int) // save the current value of the iterator iterator tmp = *this; // move to the successor of nodePtr nodePtr = nodePtr->next; return tmp; // return original iterator value 14

begin() and insert() Both are member functions of class miniList. template <typename T> miniList<T>::iterator miniList<T>::begin() { return iterator(header->next); } miniList<T>::iterator miniList<T>::insert(iterator pos, const T& item) dnode<T> *curr = pos.nodePtr, *newNode; // insert item before curr and capture the new node’s address newNode = dinsert(curr, item); // increment the list size listSize++; // constructor converts newNode to an iterator return iterator(newNode); 15