Doubly linked list operations in C language.
/* Doubly linked list operationsInsertion , Deletion, Searching*/#include<stdio.h>#include<conio.h>#include<malloc.h>void insert();void deleted();void search();void display();int i=0;struct node *head,*temp,*m,*loc;int count;struct node{int data;struct node *prev;struct node *next;};int main(){ int op; do{printf("n <<Doubly Linked List Operations>>");printf("n1.Insert Element");printf("n2.Delete Element");printf("n3.Search Element");printf("n4.Display");printf("n5.Exit"); scanf("%d",&op); switch(op) { case 1: insert(); break; case 2: deleted(); break; case 3: search(); break; case 4: display(); break; case 5: break; default: printf("nPlease Enter Valid Choice.."); } }while(op!=5);getch();}void insert(){printf("nEnter Elements To Be Inserted");temp =(struct node *) malloc(sizeof(struct node)); scanf("%d", &temp->data);if(i==0) {temp->next = NULL;temp->prev =NULL;head=temp;m=temp;i++;}else{ temp->prev = m; m->next = temp; m=temp; } m->next=NULL; printf("n Element Inserted successfully..");} void display(){ printf(" PPREV DATA NEXT NODE_ADDRESS "); temp=head; while(temp != NULL) { printf("n %10u %7d %13u %13u ",temp->prev,temp->data,temp->next,temp); temp = temp->next; }}void deleted(){ printf("nEnter the address of the location to delete.."); scanf("%u", &loc); if( loc == NULL) { printf("nOverflow"); } else{ if(loc->prev ==NULL) head =loc; loc->prev->next=loc->next; loc->next->prev=loc->prev; free(loc); printf("Element deleted Successfully"); }}void search(){ int item; printf("nEnter the element to be Search.."); scanf("%d",&item); temp=head; while(temp != NULL) { if(temp->data == item) { printf("Item found at address LOC => %u",temp); break; } temp = temp->next; } if(temp==NULL) printf("n Item Not found");}
Comments
Post a Comment