Doubly Linked List Implementation

Slides:



Advertisements
Similar presentations
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Advertisements

1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
Data Structures Using C++ 2E
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
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.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
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.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
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.
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.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates 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.
Link-Based Implementations
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Cpt S 122 – Data Structures Abstract Data Types
The List ADT Reading: Textbook Sections 3.1 – 3.5
CE 221 Data Structures and Algorithms
The List ADT Sections 3.2, 3.3, 3.5 Introduction
Doubly Linked List Review - We are writing this code
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
C++ Plus Data Structures
Chapter 4 Linked Lists.
Lists The List ADT.
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
LINKED LISTS CSCD Linked Lists.
Linked Lists Chapter 4.
Chapter 16-2 Linked Structures
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
CMSC 341 Binary Search Trees.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
CMSC 341.
Recursive Linked List Operations
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
CMSC 341.
Lists - I The List ADT.
Lists - I The List ADT.
Copyright © – Curt Hill STL List Details Copyright © – Curt Hill.
CMSC 341.
CMSC 341 Binary Search Trees.
Lists - I The List ADT.
CMSC 341 List 2.
Lists - I The List ADT.
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Doubly Linked List Implementation
Abstract Data Types ADT: A set of objects and a set of operations on those objects. examples: integers: +, - , *, … Collection, insert, remove, … set:
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 3 Lists, Stacks, and Queues
The List Container and Iterators
Data Structures & Programming
Presentation transcript:

Doubly Linked List Implementation CMSC 341 Lists - II Doubly Linked List Implementation

Recall the List ADT A list is a dynamic ordered tuple of homogeneous elements Ao, A1, A2, …, AN-1 where Ai is the ith element of the list Operations on a List create an empty list destroy a list construct a (deep) copy of a list find(x) – returns the position of the first occurrence of x remove(x) – removes x from the list if present insert(x, position) – inserts x into the list at the specified position isEmpty( ) – returns true if the list has no elements makeEmpty( ) – removes all elements from the list findKth(position) – returns the element in the specified position The implementations of these operations in a class may have different names than the generic names above 2/18/2006

A Linked List Implementation An alternative to the vector’s array-based implementation of the List ADT is a linked-list implementation. The STL provides the “list” container which is a singly linked list. We will implement our own “List” class (note the upper-case “L”) as a doubly linked list with both header and tail nodes. As we’ll see, the use of the header and tail nodes will simplify the coding by eliminating special cases. 2/18/2006

A doubly-linked list with header and tail nodes 2/18/2006

An empty List 2/18/2006

List classes To implement the doubly-linked List, four classes are required The List class itself which contains pointers to the header and tail nodes, all the list methods, and required supporting data A List Node class to hold the data and the forward and backward Node pointers A const_iterator class to abstract the position of an element in the List. Uses a Node pointer to the “current” node. An iterator class similar to the const_iterator class The Node and iterator classes will be nested inside the List class 2/18/2006

The List class outline template< typename Object> class List { private: struct Node { /* see following slide */ } public: class const_iterator class iterator : public const_iterator { /* see following slide */ // A whole host of List methods private: int theSize; Node *head; Node *tail; // helper function(s) }; 2/18/2006

The List’s Node struct The Node will be nested in the List class template and will be private, so a struct is sufficient and easier to code. What alternative ways are there to define the Node? struct Node { Object data; Node *prev; Node *next; Node( const Object & d = Object( ), Node *p = NULL, Node *n = NULL ) : data( d ), prev( p ), next( n ) { /* no code */ } }; Alternatives for Node (1) make a class with all data/methods private…then make List it’s friend – this is ok (2) make a class with public methods – not ok, it allows users to instantiate Nodes 2/18/2006

const_iterator class class const_iterator { public: const_iterator( ) : current( NULL ) { } const Object & operator* ( ) const { return retrieve( ); } bool operator== ( const const_iterator & rhs ) const { return current == rhs.current; } bool operator!= ( const const_iterator & rhs ) const { return !( *this == rhs ); } 2/18/2006

const_iterator class (2) // pre-increment const_iterator & operator++ ( ) { current = current->next; return *this; } // post-increment const_iterator operator++ ( int dummy) const_iterator old = *this; ++( *this ); return old; 2/18/2006

const_iterator class (3) // pre-decrement const_iterator & operator-- ( ) { current = current->prev; return *this; } // post-decrement const_iterator operator-- ( int dummy) const_iterator old = *this; --( *this ); return old; 2/18/2006

const_iterator class (4) protected: // available to iterator class Node *current; Object & retrieve( ) const { return current->data; } const_iterator( Node *p ) : current( p ) { } friend class List<Object>; // why? }; 2/18/2006

iterator class class iterator : public const_iterator { public: { } // this is different than in const_iterator // because it’s not a const method Object & operator* ( ) { return retrieve( ); } // explicitly reimplement const operator* // otherwise the original is hidden by operator* above const Object & operator* ( ) const { return const_iterator::operator*( ); } // operator== and operator!= inherited 2/18/2006

iterator class (2) // reimplement increment operators // to override those in const_iterator // because of different return types iterator & operator++ ( ) { current = current->next; return *this; } iterator operator++ ( int dummy) iterator old = *this; ++( *this ); return old; List<Object> must be redeclared as a friend since friendship is not inherited 2/18/2006

iterator class (3) // also reimplement decrement operators // to override those in const_iterator // because of different return type iterator & operator-- ( ) { current = current->prev; return *this; } iterator operator-- ( int dummy) iterator old = *this; --( *this ); return old; 2/18/2006

iterator class (4) protected: // no data since the “current” is inherited iterator( Node *p ) : const_iterator( p ) { /* no code */ } friend class List<Object>; // why? }; 2/18/2006

The List class template< typename Object> class List { private: struct Node { /* see previous slide */ } public: class const_iterator class iterator : public const_iterator { /* see previous slide */ // public List methods (within class definition) follow 2/18/2006

List class (2) // default constructor and the Big-3 List( ) { init( ); } List( const List & rhs ) { init( ); *this = rhs; } const List & operator= ( const List & rhs ) if( this == &rhs ) // self-assignment check return *this; clear( ); // make this List empty for(const_iterator itr = rhs.begin( ); itr != rhs.end( ); ++itr ) push_back( *itr ); // destructor ~List( ) clear( ); // delete all the data nodes delete head; // delete the header node delete tail; // delete the tail node

List Class (3) // Functions that create const_iterators const_iterator begin( ) const { return const_iterator( head->next ); } const_iterator end( ) const { return const_iterator( tail ); } // Functions that create iterators iterator begin( ) { return iterator( head->next ); } iterator end( ) { return iterator( tail ); } 2/18/2006

List class (4) // accessors and mutators for front/back of the List Object & front( ) { return *begin( ); } const Object & front( ) const Object & back( ) { return *--end( ); } const Object & back( ) const void push_front( const Object & x ) { insert( begin( ), x ); } void push_back( const Object & x ) { insert( end( ), x ); } void pop_front( ) { erase( begin( ) ); } void pop_back( ) { erase( --end( ) ); }

List class (5) // how many elements in the List? int size ( ) const { return theSize; } // is the List empty? bool empty( ) const { return size( ) == 0; } // remove all the elements void clear ( ) { while (! Empty( ) ) pop_front( ); } 2/18/2006

List class (6) // Insert x before itr. iterator insert( iterator itr, const Object & x ) { Node *p = itr.current; theSize++; return iterator( p->prev = p->prev->next = new Node( x, p->prev, p ) ); } 2/18/2006

List class (7) // Erase item at itr. iterator erase( iterator itr ) { Node *p = itr.current; iterator retVal( p->next ); p->prev->next = p->next; p->next->prev = p->prev; delete p; theSize--; return retVal; } // erase items between “from” and “to” // including “from”, but not including “to” iterator erase( iterator from, iterator to ) for( iterator itr = from; itr != to; ) itr = erase( itr ); return to;

List class (8) private: int theSize; Node *head; Node *tail; // private helper function for constructors // creates an empty list void init( ) { theSize = 0; head = new Node; tail = new Node; head->next = tail; tail->prev = head; } }; // end of class definition 2/18/2006

Problems with the code What problems or inadequacies did you find in the code? How can they be solved? Also note that this code is written entirely within the class definition. How would the code be different if the methods were implemented outside the class definition (as some of them should be)? 2/18/2006

Performance of List operations What is the asymptotic performance of each List operation in terms of the number of elements in the list, N… When the List is implemented as a vector? When the List is implemented as a Doubly-Linked List? 2/18/2006

Circular Linked List Use the header node’s “prev” pointer to point to the tail Use the tail node’s “next” pointer to point to the head 2/18/2006

Sorted Lists Suppose we decided that the data in the lists should be stored in sorted order. How would the sorted order be determined? What List code would need to be changed? How would sorting the data affect the performance of Finding an element in the list Inserting an element into the list Removing an element from the list other List functions 1/18/2007