Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Queues and Linked Lists
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Data Structures: A Pseudocode Approach with C
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a 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.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Linked Lists Tonga Institute of Higher Education.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structure & Algorithms
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.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
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.
Linked List, Stacks Queues
Double-Ended Queues Chapter 5.
CSCI-255 LinkedList.
Program based on queue & their operations for an application
Linked List Stacks, Linked List Queues, Dequeues
Data Structure Dr. Mohamed Khafagy.
Stacks and Queues.
Priority Queue.
Lists.
Data Structures and Algorithms IT12112
LINKED LISTS CSCD Linked Lists.
Lists.
DATA STRUCTURE QUEUE.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Header and Trailer Sentinels
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Presentation transcript:

Doubly Linked Lists

One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has a pointer to both its successor and its predecessor. nextprevelemnextprevelemnextprevelem head tail

Doubly Linked Lists struct Node { char element; Node *prev; Node *next; Node(const char& e=char(), Node *p=NULL, Node *n=NULL) : element(e), prev(p), next(n) { } };

Sentinel Nodes As always, implementation of a data structure is up to the individual. However, one way of implementing a double linked list is to create sentinel nodes at the beginning and end of the list. – Pointers to the sentinel nodes can be stored in a data structure as data members – Advantage: Not necessary to treat the front or rear differently when inserting or removing nextprevelem nextprevelemnextprevelem nextNULLhead NULLprevtail

Empty Double Linked List head = new Node; tail = new Node; head->next = tail; tail->prev = head; sz = 0; NULLprev nextNULL head tail

Deque - Double-Ended Queues A Deque (pronounced ‘deck’) is a data structure in which items may be added to or deleted from the head or the tail. They are also known as doubly-ended queues. The Deque is typically implemented with a linked list, providing easy access to the front and the back of the list, although a dynamic array could also be used for this.

Deque - Double-Ended Queues The deque data structure typically has the following features: – How are the data related? Sequentially – Operations insertFirst - inserts an element at the front of a deque insertLast - inserts an element at the rear of a deque removeFirst - removes an element from the front of a deque removeLast - removes an element from the rear of a deque (Note that the previous 2 functions did not return and data values) First - returns a reference to the element at the front last - returns a reference to the element at the rear size, isEmpty

Deque Advantages Removing a node from the rear of a singly linked list was inefficient because we had to find the next-to-last by searching from the head The deque is implemented with a doubly linked list. Therefore, now, removing from the rear is not a problem.

Algorithm: insertFirst() //Create a new node t //Make “t->prev” point to node head //Make “t->next” point to node head->next (i.e., u) Node *t = new Node(e,head,head->next); //Put the address of t into head->next’s “prev” pointer //Put the address of t into head’s “next” pointer head->next->prev = t; head->next = t; nextprev u nextprev head NULLprevtail nextprev t

Algorithm: insertFirst() Node *t = new Node(e,head,head->next); head->next->prev = t; head->next = t; sz++;

Algorithm: removeLast() //Save the address of the last node as “old” Node *old = tail->prev; //Make “old->prev->next” point to node “tail” old->prev->next = tail; //Make “tail->prev” point to node “old->prev” tail->prev = old->prev; //Delete “old” delete old-; nextprev tail nextprev p nextNULLheadnextprev old

Algorithm: removeLast() Node *old = tail->prev; old->prev->next = tail; tail->prev = old->prev; delete old; //Recover the memory sz--;