Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Pointers.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked List Variations
Data Structure Lecture-5
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
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.
Data Structures: A Pseudocode Approach with C
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Data Structures & Algorithms
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.
Chapter 3: Arrays, Linked Lists, and Recursion
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Reference: Vinu V Das, Principles of Data Structures using C and C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
Subject Name : Data Structure Using C Title : Linked Lists
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
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.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
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.
Unit – I Lists.
Data Structure By Amee Trivedi.
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Data Structures Interview / VIVA Questions and Answers
Chapter 20: Binary Trees.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Chapter 21: Binary Trees.
Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Linked List.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
BY PROF. IRSHAD AHMAD LONE.
Presentation transcript:

Doubly Linked List

Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application  Advantage & disadvantage

Linked List  Linked list is data structure used to store similar data in memory (may or may not be in adjacent memory location )  Types of linked list 1. Singly linked list 2. Doubly linked list 3. Circular linked list

Structure of linked list Struct node { int info; struct node *next; }; infonext node

Doubly linked list  Doubly linked list is linked data structure that consist of sequentially linked records called nodes and each node consist of two fields called links.  Two fields(links): 1. Previous (backward) 2. Next(forward)  Head is the starting node or first node  Tail is the ending node or last node

Circular linked lists  Last node references the first node  Every node has a successor  No node in a circular linked list contains NULL

Representation of node  Node of doubly linked list consist of three parts: 1. previous 2. data 3. next prev data next node

Representation  If there is only one sentinel node then the list is circularly linked via the sentinel node. It can be concept as two singly linked list formed from the same data item, but in opposite sequencial order

Structure of node  doubly linked list is a list that contains links to links to next and previous nodes. Doubly linked list allows traversal in both ways typedef struct*node ; { void *data; struct node *next; struct node *prev; } node;

Dummy head nodes  Eliminates the special case for insertion into & deletion from beginning of linked list.  Dummy head node Always present, even when the linked list is empty. insertion & deletion algorithms initialize previous to reference the dummy head node rather than NULL

(a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

Double-ended list  Doubly –linked list can also be created as double- ended list  The reference to the last link permits to insert a new link directly at the end of the list as well as at the beginning.  This could not be done with ordinary linked list without traversing the whole list. NULL First last

Doubly linked list

Inserting a node in doubly linked list A node can be inserted: a)after given node b)before given node c) at the beginning of an empty list d) at the end of an empty list

Inserting a node in doubly linked list

Suppose a new node, B needs to be inserted after the node A Code: void insertafter() struct node *B; B=(struct node *)malloc(sizeof(struct node)); A->next = B->next; A->next = B; B->prev = A; (B->next)->prev = B;

Inserting a node in doubly linked list Suppose a new node, B needs to be inserted before the node C void insertbefore() struct node *B; B=(struct node *)malloc(sizeof(struct node)); C->prev=B->prev; C->prev=B; B->next=C; (B->next)->prev = B;

Inserting a node in doubly linked list Suppose a new node, B needs to be inserted before the node A Void insertbegin() Struct node *B; B=(struct node *)malloc(sizeof(struct node)); B->next=A; A->prev=B; B->prev=NULL;

Inserting a node in doubly linked list Suppose a new node, B needs to be inserted after the node C Void insertend() Struct node *B; B=(struct node *)malloc(sizeof(struct node)); C->next=B; B->prev=C; B->next=NULL;

Page 2020 Deleting a Node from a Doubly-Linked List Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i.e., return it to the heap). Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL. To logically delete a node: ◦ First locate the node itself. ◦ Change the predecessor’s and succesor’s link fields to point each other. ◦ Recycle the node using the free() function.

Page 2121 Deleting the First Node from a Doubly-Linked List Deletion At First: If(first==NULL) printf(“memory of link list is empty”); else { *p=first First=first->rptr first->lptr=NULL Free(p) }

Page 2222 DELETION OF FIRST NODE Before: After: Recycled p first NN NN

Page 2323 Deleting a Last Node From a Doubly-Linked List The Code if (first == NULL) { printf(“memory of link list is empty”); } Else { *p=first while(p->rptr != NULL) { P=p->rptr } p->lptr->rptr=NULL free(p)

Deletion At Last Node Before: Page 2424 After: Deleted Node NN NN P

Traversing the Doubly linklist 1.Traversal of a doubly-linked list can be in either direction. 2.In fact, the direction of traversal can change many times, if desired. Traversal is often called iteration, but that choice of terminology is unfortunate, for iteration has well-defined semantics (e.g., in mathematics) which are not analogous to traversal.iteration

A doubly linked list can be traversed either way and that too very conveniently. 1.Inorder/Forward Traversal 2.reversorder/Backward Traversal

Inorder Traversal To traverse the doubly linked list, we walk the list from the beginning, and process each element until we reach the last element. void traverseinorder(node *head) { while(head!=NULL) { printf("%dn",head->info); head=head->next; } To call the above function, use: traverseinorder(head);

Revers order traversal The following listing shows how to traverse a doubly linked list in the backward direction. Note that the tail pointer will need to be passed in a call to this function. void traversereverseorder(node *tail) { if(tail!=NULL) { printf("%dn",tail->info); //print data tail = tail->prev; // go to previous node } To call the above function, use: traversereverseorder(tail);

Applications of a doubly linked list - A great way to represent a deck of cards in a game. - Applications that have a Most Recently Used (MRU) list (a linked list of file names) - A stack, hash table, and binary tree can be implemented using a doubly linked list. - Undo functionality in Photoshop or Word (a linked list of state).

Advantage  We can traverse in both directions i.e. from starting to end and as well as from end to starting.  It is easy to reverse the linked list.  If we are at a node, then we can go to any node. But in linear linked list, it is not possible to reach the previous node.

Disadvantage  It requires more space per node because one extra field is required for pointer to previous node.  Insertion and deletion take more time than linear linked list because more pointer operations are required than linear linked list.

THANK YOU THANK YOU