Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure Lecture-5

Similar presentations


Presentation on theme: "Data Structure Lecture-5"— Presentation transcript:

1 Data Structure Lecture-5
Prepared by: Shipra Shukla Assistant Professor Kaziranga University

2 Doubly Link List:Node data
info: the user's data next, prev: the address of the next and previous node in the list .prev .data . next next prev

3 Syntax of node struct node { int data; struct node *next; struct node *prev; };

4 insert_at_begin void insert_at_begin(int item) { node *ptr; ptr=(node*)malloc (sizeof(node)); ptr->info=item; if( head==(node*)null) ptr->prev=ptr->next=(node*)null; head=tail=ptr; } else ptr->prev=(node*)null; ptr->next=head; head->prev=ptr; head=ptr;

5 insert_at_end void insert_at_end(int item) { node *ptr;
ptr=(node*)malloc (sizeof(node)); ptr->info=item; if( tail==(node*)null) ptr->prev=ptr->next=(node*)null; head=tail=ptr; } else ptr->next=(node*)null; ptr->prev=tail; tail->next=ptr; tail=ptr;

6 Delete_at_begin void delete_at_begin() { node *ptr; if( head==(node*)nulll) return; else if(head->next==(node*)null) ptr=head; head=tail=(node*)null; } else head=head->next; head->prev=(node*)null; free(ptr);

7 Delete_at_end void delete_at_end() { node *ptr;
if( tail==(node*)nulll) return; else if(tail->prev==(node*)null) ptr=tail; head=tail=(node*)null; } else tail=tail-> prev; tail->next=(node*)null; free(ptr);

8 Create Circular Doubly Linked list
node* create_list() { node* head; head=allocate_node(); head->left= head->right=head; return (head); }

9 Insert a node in a Circular Doubly Linked list
insert_cir(node*head, item) { node*ptr,*temp; int item; ptr=(node*) malloc (sizeof(node)); ptr->info=item; temp=head->right; head->right=ptr; ptr->left=head; ptr->right=temp; temp->left=ptr; return(head); }

10 Insert a node at end in a Circular Doubly Linked list
insert_cir(node*head) { node*ptr,*temp; int item; ptr=(node*) malloc (sizeof(node)); ptr->info=item; temp=head->left; temp->right=ptr; ptr->left=temp; ptr->right=head; head->left=ptr; return(head); }

11 Delete a node at begin in a Circular Doubly Linked list
del_beg(node*head) { node*temp; if(head->right==head) printf(“List is empty”); return; } else temp=head->right; printf(“deleted element is=%d”,temp->info); head->right=temp->right; temp->right=temp->left=head; free(temp); return(head);

12 Delete a node at end in a Circular Doubly Linked list
del_end(node*head) { node*temp; if(head->right==head) printf(“List is empty”); return; } else temp=head->left; printf(“deleted element is=%d”,temp->info); head->left=temp->left; free(temp); return(head);


Download ppt "Data Structure Lecture-5"

Similar presentations


Ads by Google