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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Linked Lists.
CS 367 – Introduction to Data Structures
Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
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.
Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Classes with dynamic members. int x; void f() { x=10; } main() { x=0; f(); } ‘Class’ matters! int x; void f() { int x; x=10; } main() { x=0; f(); } class.
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.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Classes with dynamic objects List as a (dynamic) class.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
COMP104 Linked List Algorithms / Slide 1 Some Simple Algorithms on Linked Lists * Write a function that returns the length of a given list. * Write a boolean.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
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.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9.
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.
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.
Algorithms and Data Structures
COMPSCI 105 SS 2015 Principles of Computer Science
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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.
Programming Circular Linked List.
Exercise 1 From Lec80b-Ponter.ppt.
Doubly Linked List Review - We are writing this code
Chapter 4 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
Hash Tables.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Summary on linked lists
Chapter 16-2 Linked Structures
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
as an abstract data structure and
Chapter 4 Linked Lists.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Header and Trailer Sentinels
Linked Lists.
Linked Lists.
Data Structures & Programming
Presentation transcript:

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 prev points to the predecessor next points to the successor Head Cur Cur->nextCur->prev

COMP104 Doubly Linked Lists / Slide 3 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 which require “rewind” and “fast forward” of the data

#include using namespace std; struct Node{ int data; Node* next; Node* prev; }; typedef Node* NodePtr; Doubly Linked List Definition

COMP104 Doubly Linked Lists / Slide 5 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 6 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 7 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 8 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 9 Doubly Linked Lists with Dummy Head n Non-Empty List n Empty List Head 10 Dummy Head Node Head Dummy Head Node

void createHead(NodePtr& Head){ Head = new Node; Head->next = Head; Head->prev = Head; }

COMP104 Doubly Linked Lists / Slide 11 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 12  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 13  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; }

COMP104 Doubly Linked Lists / Slide 15 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 16  Insert a Node New in the middle and before Cur New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; // same as insert front! 55 Head 10 Dummy Head Node 20 Cur 40 New

COMP104 Doubly Linked Lists / Slide 17  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; // same as insert front! Head 10 Dummy Head Node Cur New

COMP104 Doubly Linked Lists / Slide 18  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 New, Cur; New = new Node; New->data = item; Cur = Head->next; while(Cur != Head){//position Cur for insertion if(Cur->data < item) Cur = Cur->next; else break; } New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; }

NodePtr searchNode(NodePtr Head, int item){ NodePtr Cur = Head->next; while(Cur != Head){ if(Cur->data == item) return Cur; if(Cur->data < item) Cur = Cur->next; else break; } return NULL; } Searching a node:

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

void main(){ NodePtr Head, temp; createHead(Head); print(Head); insertNode(Head, 3); print(Head); insertNode(Head, 5); print(Head); 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