Vous êtes sur la page 1sur 5

#include<stdio.h> #include<conio.h> #include<stdlib.

h> struct list { int data; //char data[30]; struct list *next; struct list *prev; }; typedef struct list DNODE; void create_list(); void display_list(); void insatbeg(); void insatpos(); int delend(); int delfrnt(); int delatpos(); void sort(); DNODE *start=NULL; int main(void) { int ch,x; clrscr(); while(1) { printf("\n 1: to create \n 2: display \n 3: to exit\n"); printf(" 4: to insert at begnning \n 5: to insert any position\n "); printf(" 6: to delete at end \n 7: to del front\n 8: to del at p osition"); printf(" 9: to sort \n"); printf("\n plz enter your choice \n"); scanf("%d",&ch); switch(ch) { case 1: create_list(); break; case 2: display_list(); break; case 3: exit(0); break; case 4: insatbeg(); break; case 5: insatpos(); break; case 6: x=delend(); printf("\n the deleted element is %d ",x); break; case 7: x=delfrnt(); printf("\n the deleted element is %d ", x); break; case 8: x=delatpos(); printf("\n the deleted element is %d ", x);

break; /* case 9: sort(); break; */ default: printf("\n invalid choice \n"); } } } void create_list() { DNODE *p, *node; p=(DNODE *)malloc(sizeof(DNODE)); printf("\n enter the value into node \n"); scanf("%d",&p->data); p->next=NULL; p->prev=NULL; if(start==NULL) start=p; else { node=start; while(node->next!=NULL) node=node->next; node->next=p; p->prev=node; } } void display_list() { DNODE *node; if(start==NULL) printf("\n list is empty \n"); else { node=start; while(node->next!=NULL) { //printf("%s",node->data); printf("%d\t",node->data); node=node->next; //printf("\n"); } //printf("%s",node->data); printf("%d",node->data); printf("\n"); } } void insatbeg() { DNODE *p, *node; p=(DNODE *)malloc(sizeof(DNODE)); printf("\n plz enter the value into node \n"); scanf("%d",&p->data); p->next=NULL; p->prev=NULL; if(start==NULL)

start=p; else { node=start; start=p; p->next=node; node->prev=p; } } void insatpos() { DNODE *p, *node; int pos,i=1; p=(DNODE *)malloc(sizeof(DNODE)); printf("\n plz enter the position \n"); scanf("%d",&pos); printf("\n plz enter the value into node\n"); scanf("%d",&p->data); if(start==NULL) { start=p; p->prev=NULL; } else { node=start; while(node->next!=NULL) { node=node->next; //i++; if(i==(pos-1)) { p->next=node->next; node->next=p; p->prev=node; p->next->prev=p; } i++; /*if(node==NULL) printf("\n wrong location \n"); */ } } } int delend() { DNODE *node,*p; int value; if(start==NULL) { printf("\n list is empty \n"); return 0; } else { node=start;

while(node->next!=NULL) { p=node; node=node->next; } value=node->data; p->next=NULL; free(node); return(value); } } int delfrnt() { int value; DNODE *node; if(start==NULL) { printf("\n list is empty \n"); return 0; } else { node=start; start=node->next; node->next->prev=NULL; value=node->data; free(node); return(value); } } int delatpos() { DNODE *node, *p; int pos, i,value; printf("\n plz enter the position\n"); scanf("%d",&pos); if(start==NULL) { printf("\n list is empty \n"); return 0; } else { node=start; for(i=1;i<pos;i++) { p=node; node=node->next; if(node==NULL) { printf("\n wrong location\n"); return 0; } } p->next=node->next;

value=node->data; node->next->prev=p; free(node); return(value); } }

Vous aimerez peut-être aussi