Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int a;
- char ch;
- struct node*next;
- struct node*previous;
- } node;
- node*head;
- void display()
- {
- node *list=head;
- while(list !=NULL)
- {
- printf("A: %d\n",list->a);
- printf("C: %c\n",list->ch);
- printf("\n");
- list=list->next;
- }
- }
- void insert_at_first(int aN, char chN)
- {
- node *N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- N->previous=NULL;
- if(head==NULL)
- {
- head=N;
- return;
- }
- N->next=head;
- head->previous=N;
- head=N;
- }
- void insert_at_end(int aN,char chN)
- {
- node*N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- N->previous=NULL;
- node*list=head;
- if(head==NULL)
- {
- head=N;
- }
- else
- {
- while(list->next!=NULL)
- {
- list=list->next;
- N->previous=list;
- }
- list->next=N;
- N->next=NULL;
- N->previous=list;
- }
- }
- void insert_nth(int n,int aN,char chN)
- {
- node *N=(node*)malloc(sizeof(node));
- N->a=aN;
- N->ch=chN;
- N->next=NULL;
- N->previous=NULL;
- if(n==1)
- {
- N->previous=NULL;
- N->next=head;
- head=N;
- return;
- }
- else
- {
- node*list=head;
- n=n-2;
- while(n!=0&&list->next!=NULL)
- {
- list=list->next;
- n--;
- }
- N->next=list->next;
- if(list->next !=NULL)
- {
- list->next->previous=N;
- }
- list->next=N;
- }
- }
- int search(int n)
- {
- node*list=head;
- int x=0;
- while(list!=NULL)
- {
- if(list->a==n)
- {
- printf("\n%d is Found\n\n",n);
- x=1;
- break;
- }
- else
- list=list->next;
- }
- if(x==0)
- {
- printf("\n%d is Not Found\n\n",n);
- }
- }
- void create()
- {
- int value;
- char ch;
- while(1)
- {
- printf(" \nENTER NUMBER & Character(-0 for exit)\n");
- scanf("%d",&value);
- if(value==-0)
- {
- break;
- }
- scanf(" %c",&ch);
- insert_at_end(value,ch);
- }
- }
- void delete_pos(int pos)
- {
- node *list=head,*temp=NULL,*tail;
- if(pos==1)
- {
- if(list->next==NULL)
- {
- printf("DATA DELETED\n\n");
- free(list);
- }
- else
- {
- head=list->next;
- head->previous=NULL;
- printf("DATA DELETED\n\n");
- free(list);
- }
- }
- else
- {
- pos=pos-2;
- while(pos!=0 && list->next!=NULL)
- {
- list=list->next;
- pos--;
- if(list==NULL)
- {
- printf("NO DATA FOUND!!\n\n");
- break;
- }
- }
- if(list->next==NULL)
- {
- printf("You Choose Wrong Position!!\n\n");
- }
- else if(list==NULL && pos==0)
- {
- temp=tail;
- tail=temp->previous;
- tail->next=NULL;
- printf("DATA DELETED\n\n");
- free(temp);
- }
- else if(list!=NULL && pos==0)
- {
- temp=list->next;
- temp->next->previous=list;
- list->next=temp->next;
- printf("DATA DELETED\n\n");
- free(temp);
- }
- }
- }
- void delete_item(int value)
- {
- node *list = head, *temp=NULL;
- int x = 0;
- while(list!=NULL)
- {
- if(list->a==value)
- {
- if(temp==NULL)
- {
- head = list->next;
- head->previous=NULL;
- }
- else
- temp->next = list->next;
- printf("%d is deleted from list\n", value);
- x = 1;
- free(list);
- break;
- }
- temp = list;
- list = list->next;
- list->previous=temp;
- }
- if(x==0)
- printf("Key not found!\n");
- }
- void MENU()
- {
- int x,af,l;
- char cf;
- printf(" \n Create Doubly Linked List \n");
- create();
- display();
- printf(" \n Insert At FIRST \n");
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_at_first(af,cf);
- display();
- printf(" \n Insert At END \n");
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_at_end(af,cf);
- display();
- printf(" \n Insert At Nth(pos,int,char) \n");
- scanf("%d",&x);
- scanf("%d",&af);
- scanf(" %c",&cf);
- insert_nth(x,af,cf);
- display();
- printf(" \n SEARCH VALUE \n");
- scanf("%d",&l);
- search(l);
- printf(" \n DELETE BY POSITION \n");
- scanf("%d",&x);
- delete_pos(x);
- display();
- printf(" \n DELETE BY VALUE \n");
- scanf("%d",&l);
- delete_item(l);
- printf(" \n FINAL VALUE \n");
- display();
- return 0;
- }
- int main()
- {
- head=NULL;
- MENU();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement