A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.

Slides:



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

Linked Lists Geletaw S..
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
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.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Chapter 17 Linked List.
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.
ADSA: Linked Lists/ Advanced Data Structures and Algorithms Objective – –implement and use linked lists Semester 2, Linked.
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 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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Working with Pointers An exercise in destroying your computer.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
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.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Objectives of these slides: to describe linked lists in C 6 – Lists.
COMP x1 Computing 2 C Outside the Style Guide, Linked Lists and Function Pointers 1.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
CSCI387 Data Structure Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University.
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.
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
Abstract Data Type (ADT) &List (Part II) Lecture Objectives Implement linked list using pointers Concept of iterators STL's list class Other variations.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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
Linked Lists Chapter 6 Section 6.4 – 6.6
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Linked lists.
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
Data Structures and Algorithms IT12112
Chapter 16-2 Linked Structures
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked List Implementation
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked lists.
Chapter 9 Linked Lists.
The List Container and Iterators
16 – Sequential Containers
Data Structures & Programming
Presentation transcript:

A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode

Doubly Linked List Implementation template 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 *prev;// previous node in the list dnode *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); };

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

Node Insertion prevnextprev next current node dnode *newNode, *prevNode; // allocate a new node and assign prevNode to reference the predecessor of curr newNode = new dnode (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; newNode prevNode

The insert() Function template dnode *insert(dnode *curr, const T& item) { // declare pointer variables for the new node and the previous node dnode *newNode, *prevNode; // allocate new dnode with item as initial value newNode = new dnode (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; }

Node Deletion – Self Removal next prev nextprev curr prevNode -> next = succNode; succNode -> prev = prevNode; dnode *prevNode = curr->prev, *succNode = curr->next; delete curr; prevNodesuccNode

The erase() Function template void erase(dnode *curr) { // return if the list is empty if (curr->next == curr) return; // declare pointers for the predecessor and successor nodes dnode *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; }

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

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

Application: The Josephus Problem n customers to compete in a contest for a free world cruise. The customers are placed in a circle by a travel agent. The agent draws a number m (m  n) from a hat. He rotates clockwise about the circle and stop at every m th contestant. This person is then removed from the game. The game continues with every m th person eliminated until only one remains. The survivor is the winner!

A Game Example n = m = 5 The winner!

Solution to the Josephus Problem void josephus(int n, int m) { // declare the circular doubly linked list dList and scan pointer dnode *dList = new dnode, *curr; int i, j; // initialize the list of contestants n for (i = 1; i <= n; i++) insert(dList, i); // curr moves around the list, starting at person 1 curr = dList->next; // delete all but one person from the list for (i=1; i < n; i++) { // counting current person at curr, visit m persons. // we must advance m-1 times. for (j=1; j <= m-1; j++) { // advance the pointer curr = curr->next;

Solution (cont’d) // if curr at the header, move again if (curr == dList) curr = curr->next; } cout nodeValue << endl; // advance curr and erase the node we just left curr = curr->next; erase(curr->prev); // might have deleted the rear of the list, so // curr is now at the header. move again if (curr == dList) curr = curr->next; } cout nodeValue << " wins the cruise" << endl; // delete the one remaining node and the list header delete curr; delete dList; }

The Main Program void main() { // numContestants is number of contestants // removeM is the rotation selector randomNumber rnd; int n, m; cout << "Enter the number of contestants: "; cin >> n; // generate a random number between 1 // and numContestants m = 1 + rnd.random(n); cout << "Generated the random number " << m << endl; // solve the Josephus problem and output the cruise winner josephus(n, m); } Enter the number of contestants? 10 Generated the random number 5 Delete person 5 Delete person 10 Delete person 6 Delete person 2 Delete person 9 Delete person 8 Delete person 1 Delete person 4 Delete person 7 Contestant 3 wins the cruise.

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 *header; int listSize; dnode *getDNode(const T& item); // allocate a dnode dnode *dinsert(dnode *curr, const T& item); // insert before node curr and // return address of the new node void derase(dnode *curr); // erase node curr from the linked list

A Constructor template miniList ::miniList(int n, const T& value): listSize(n) { int i; // create an empty list header = new dnode ; 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); }

Copy Constructor template miniList ::miniList(const miniList & obj): listSize(obj.listSize) { // curr moves through the nodes in obj, and end marks the finish // of a traversal through obj dnode *curr = obj.header->next, *end = obj.header; // create an empty list header = new dnode ; 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; }

Iterator Nested Class template class miniList { public: // include the iterator // nested classes #include “d_liter.h” … private: dnode *header; int listSize; dnode *getDNode( const T& item); dnode *dinsert( dnode *curr, const T& item); void derase(dnode *curr); }; class iterator { public: friend class miniList ; // 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 *nodePtr; // pointer to the current list node // private constructor. converts p to an iterator // by assigning p to nodePtr iterator(dnode *p): nodePtr(p) {} };

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 }

begin() and insert() Both are member functions of class miniList. template miniList ::iterator miniList ::begin() { return iterator(header->next); } template miniList ::iterator miniList ::insert(iterator pos, const T& item) { dnode *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); }