Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and Data Structures

Similar presentations


Presentation on theme: "Algorithms and Data Structures"— Presentation transcript:

1 Algorithms and Data Structures
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Double Linked List Course No.: Fall 2014

2 Double Linked List (DLL)
Every node contains three fields: Data field to store the relevant data. pointer field, called after, to store the address of the next node in the list. pointer field, called before, to store the address of the before node in the list. When we delete from end in single linked list we do a loop because we don’t have the pointer to the previous node

3 Implement DLL Operations
We will create the program that does the following: Initialization of the Linked List Adding a new node at the beginning of the Linked List Adding a new node at the end of the Linked List Adding a new node at any place of the Linked List Deleting a node from the beginning of the Linked List Deleting a node from the end of the Linked List Deleting a node from any place of the Linked List Destroying the Linked List

4 Create 5 double linked list
# include<iostream.h> struct node{ int Data ; node * after ; node *before ; }; class SLL { int size; nod *head, *cura, *curb, *last; public: SLL(int s){size = s; } //function for initialization of the DLL void initila_D( ) { head = last = NULL ; }

5 Insert data to the double linked list
void write( ) { cura = head ; while( cura) cout<< " Enter the values of the items : " ; cin>> cura -> Data ; cura = cura -> after; };

6 Traversing a Double Linked List
Traverse mean visit each node and read its data To Traverse list from the head node to the last void read( ) { Int i=0; cura = head ; while( cura) cout<< "Data of node [ " <<i++<<" ] = " Cout<<cur -> Data ; cura = cura -> after; }; How To traverse the double linked list from the last node to the head?

7 Double Linked list Operation
Insertion for inserting a new item into the list Deletion for deleting an item from a linked list. The operation of both insertion and deletion will be: At the beginning of the double linked list At a required place At the end of the double linked list

8 Inserting a node at the beginning of the double linked list
void Addfirst (int NewData) { if(!head) { head = new node; head->Data = NewData; head -> after = NULL; last = head; last -> before = NULL; } else node *temp = new node; temp -> Data = NewData; temp -> after =head; temp -> before = NULL; head -> before = temp ; head = temp; } }

9 Inserting a new node at the end of double linked list
void AddLast(int NewData){ If ( !head ) { last = new node; Last -> Data = NewData; last -> after = NULL ; last -> before = NULL ; head = last ; } else node *temp = new node ; temp -> Data = NewData ; last -> after = temp ; temp -> before = last ; temp -> after = NULL: last = temp ; } }

10 Inserting a new node in any place in double linked list
void adany_D (int m) { nod *temp; temp = new nod ; temp -> Data = m ; cura = head ; while (cura -> Data < m ) cura = cura -> after ; } temp -> after = cura ; temp -> before = cura -> before; cura -> before -> after = temp; cura -> before = temp;

11 Deleting a node at the beginning of the doubly linked list
void delFirst( ) { cura = head; head = head -> after; delete cura; head -> before = NULL ; }

12 Deleting a node at the end of the doubly linked list
void delLast( ) { curb = last ; last = last -> before ; delete curb; last -> after = NULL; }

13 Deleting a node from the linked list
void deany_D(int me ) { assert(head -> Data != me); assert(last -> Data != me); cura = head ; while (cura -> Data != me) cura = cura -> after ; } cura -> before -> after =cura ->after; cura -> after -> before =cura ->before; delete cura ;

14 Destroy Doubly Linked List
void destroy_D() { if ( ! head ) exit(1) ; cura = head ; while(cura) head = cura -> after; delete cura; cura = head; } cout<<" No linked List exist \n";

15 Use Doubly Linked list main( ) { SLL x(4); x.create_D( ) ; x.read_D(); x.write_D(); //using other operation x. destroy_D(); }

16 Advantages of Double Linked List
The creation of double linked list have many advantages: add data from the last node in addition to first node. traverse the list from the end of the list also. easier to add nodes from both sides. easier to delete a node from both sides.


Download ppt "Algorithms and Data Structures"

Similar presentations


Ads by Google