Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”

Slides:



Advertisements
Similar presentations
Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Advertisements

Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Stacks, Queues, and Linked Lists
Linked Lists.
Queues and Linked Lists
Linear Lists – Linked List Representation
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
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.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Concept of lists and array implementations of lists.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
(Walls & Mirrors - Beginning of Chapter 4)
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Abstract Data Type: List (optional, not required).
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.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
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.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
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.
Data Structure & Algorithms
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Programming Circular Linked List.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Exercise 1 From Lec80b-Ponter.ppt.
Doubly Linked List Review - We are writing this code
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists Chapter 4.
Summary on linked lists
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
as an abstract data structure and
Chapter 4 Linked Lists.
Linked Lists: Implementation of Queue & Deque
Chapter 4 Linked Lists.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Header and Trailer Sentinels
Linked Lists.
Presentation transcript:

Doubly Linked List

COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind” and instant “replay” * They are also useful for other linked data where “require” a “fast forward” of the data as needed

COMP104 Doubly Linked Lists / Slide 3 * list using an array: n Knowledge of list size n Access is easy (get the ith element) n Insertion/Deletion is harder * list using ‘singly’ linked lists: n Insertion/Deletion is easy n Access is harder n But, can not ‘go back’!

COMP104 Doubly Linked Lists / Slide 4 Doubly Linked Lists In a Doubly Linked-List each item points to both its predecessor and successor n prev points to the predecessor n next points to the successor Head Cur Cur->nextCur->prev

struct Node{ int data; Node* next; Node* prev; }; typedef Node* NodePtr; Doubly Linked List Definition

COMP104 Doubly Linked Lists / Slide 6 Doubly Linked List Operations * insertNode(NodePtr& Head, int item) //add new node to ordered doubly linked //list * deleteNode(NodePtr& Head, int item) //remove a node from doubly linked list * SearchNode(NodePtr Head, int item) * Print(nodePtr Head, int item)

COMP104 Doubly Linked Lists / Slide 7 Deleting a Node  Delete a node Cur (not at front or rear) (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; Head Cur

COMP104 Doubly Linked Lists / Slide 8 void deleteNode(NodePtr& head, int item) { NodePtr cur; cur = searchNode(head, item); if (head==NULL) { … } else if (cur->prev == NULL) { … } else if (cur->next==NULL) { … } else { (cur->prev)->next = cur->next; (cur->next)->prev = cur->prev; delete cur; } Empty case At-the-beginning case At-the-end case General case A systematic way is to start from all these cases, then try to simply the codes, …

COMP104 Doubly Linked Lists / Slide 9 Inserting a Node  Insert a node New before Cur (not at front or rear) Head New Cur New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New;

COMP104 Doubly Linked Lists / Slide 10 void insertNode(NodePtr& head, int item) { NodePtr cur; cur = searchNode(head, item); if (head==NULL) { … } else if (cur->prev == NULL) { … } else if (cur->next==NULL) { … } else { blablabla … } } Many special cases to consider.

COMP104 Doubly Linked Lists / Slide 11 Many different linked lists … * singly linked lists n Without ‘dummy’ n With dummy n circular * doubly linked lists n Without ‘dummy’ n With dummy Using ‘dummy’ is a matter of personal preference! + simplify codes (not that much - Less logically sounds

COMP104 Doubly Linked Lists / Slide Head Rear Head Head 10 Dummy singly linked list (singly) circular linked list (regular) doubly linked list doubly linked list with dummy

COMP104 Doubly Linked Lists / Slide 13 Doubly Linked Lists with Dummy Head Node * To simplify insertion and deletion by avoiding special cases of deletion and insertion at front and rear, a dummy head node is added at the head of the list * The last node also points to the dummy head node as its successor

COMP104 Doubly Linked Lists / Slide 14 Idea of ‘dummy’ object l Instead of pointing to NULL, point to the ‘dummy’!!! l Skip over the dummy for the real list Head 10 Dummy Head Node ‘dummy object’ is also called a ‘sentinel’, it allows the simplification of special cases, but confuses the emptyness NULL!

COMP104 Doubly Linked Lists / Slide 15 Head Dummy Head Node Empty list: Head->next = head; compared with head=NULL;

void createHead(NodePtr& head){ head = new Node; head->next = head; head->prev = head; } NodePtr head; createHead(head); NodePtr cur=head; cur=cur->next; cur=head; // dummy head NodePtr head=NULL; NodePtr cur=head; cur=head; cur=NULL; // or head=NULL; operationsDoubly linked with dummySingly linked creation Empty test Start from reference

void print(NodePtr head){ NodePtr cur=head->next; while(cur != head){ cout data << " "; cur = cur->next; } Print the whole list:

NodePtr searchNode(NodePtr head, int item){ NodePtr cur = head->next; while ((cur != head) && (item != cur->data)) cur=cur->next; if (cur == head) cur = NULL; // we didn ’ t find return cur; } Searching a node (returning NULL if not found the element): End of the list, empty

COMP104 Doubly Linked Lists / Slide 19 Deleting a Node  Delete a node Cur at front Head 10 Dummy Head Node Cur (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;

COMP104 Doubly Linked Lists / Slide 20  Delete a node Cur in the middle (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;// same as delete front! 70 Head 10 Dummy Head Node Cur

COMP104 Doubly Linked Lists / Slide 21  Delete a node Cur at rear (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;// same as delete front and middle! Head 10 Dummy Head Node Cur

void deleteNode(NodePtr head, int item){ NodePtr cur; cur = searchNode(head, item); if(cur != NULL){ cur->prev->next = cur->next; cur->next->prev = cur->prev; delete cur; } If we found the element, it does not mean any emptyness!

COMP104 Doubly Linked Lists / Slide 23 Inserting a Node  Insert a Node New after dummy node and before Cur Head Dummy Head Node Cur 20 New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; 10 New

COMP104 Doubly Linked Lists / Slide 24  Insert a Node New at Rear (with Cur pointing to dummy head) New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; Head 10 Dummy Head Node Cur New

COMP104 Doubly Linked Lists / Slide 25  Insert a Node New in the middle and before Cur New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; 55 Head 10 Dummy Head Node 20 Cur 40 New

COMP104 Doubly Linked Lists / Slide 26  Insert a Node New to Empty List (with Cur pointing to dummy head node) Head Dummy Head Node New 20 New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; Cur

void insertNode(NodePtr head, int item){ NodePtr newp, cur; newp = new Node; newp->data = item; cur = head->next; while ((cur != head)&&(!(item data))) cur = cur->next; newp->next = cur; newp->prev = cur->prev; cur->prev = newp; (newp->prev)->next = newp; } It is similar to, but different from SearchNode! (it returns NULL if no element) creation location insertion

void main(){ NodePtr Head, temp; createHead(Head); insertNode(Head, 3); insertNode(Head, 5); insertNode(Head, 2); print(Head); insertNode(Head, 7); insertNode(Head, 1); insertNode(Head, 8); print(Head); deleteNode(Head, 7); deleteNode(Head, 0); print(Head); temp = searchNode(Head, 5); if(temp !=NULL) cout<<" Data is contained in the list"<<endl; else cout<<" Data is NOT contained in the list"<<endl; } Result is Data is contained in the list