This is a program to show how double linked list works.
#include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { struct node *previous; int data; struct node *next; }*head, *last; void insert_begning(int value); int delete_from_end(); void display(); int main() { int value, i, loc; head=NULL; printf("Select the choice of operation on link list"); printf("\n1.) insert at begning\n2.)delete from end \n3.) display\n4.exit\n"); while(1) { printf("\n\nenter the choice\n"); scanf("%d",&i); switch(i) { case 1: { printf("enter the value you want to insert in node "); scanf("%d",&value); insert_begning(value); display(); break; } case 2: delete_from_end(); display(); break; case 3 : display(); break; case 4 : { exit(0); break; } } } printf("\n\n%d",last->data); display(); getch(); } void insert_begning(int value) { struct node *var,*temp; var=(struct node *)malloc(sizeof(struct node)); var->data=value; if(head==NULL) { head=var; head->previous=NULL; head->next=NULL; last=head; } else { temp=var; temp->previous=NULL; temp->next=head; head->previous=temp; head=temp; } } int delete_from_end() { struct node *temp; temp=last; if(temp->previous==NULL) { free(temp); head=NULL; last=NULL; return 0; } printf("\nData deleted from list is %d \n",last->data); last=temp->previous; last->next=NULL; free(temp); return 0; } void display() { struct node *temp; temp=head; if(temp==NULL) { printf("List is Empty"); } while(temp!=NULL) { printf("-> %d ",temp->data); temp=temp->next; } }
Nice manoj..Useful one :)
ReplyDelete