Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include <stdlib.h>
- struct node
- {
- int data;
- struct node* prev;
- struct node* next;
- };
- struct node* head = NULL, *end= NULL;
- void insert(int x){
- struct node* newnode;
- newnode = (struct node*) malloc(sizeof(struct node));
- newnode -> prev = NULL;
- newnode -> data = x;
- newnode -> next = NULL;
- if(head != NULL)
- {
- newnode -> next = head;
- head -> prev = newnode;
- }
- else
- {
- end=newnode;
- }
- head = newnode;
- }
- void insert_middle(int x, int pos)
- {
- struct node* newnode,*cur;
- newnode = (struct node*) malloc(sizeof(struct node));
- newnode -> prev = NULL;
- newnode -> data = x;
- newnode -> next = NULL;
- int index=1;
- cur = head;
- while(cur->next != NULL && index<pos)
- {
- cur=cur->next;
- index++;
- }
- if(cur->next != NULL)
- {
- newnode->next=cur->next;
- newnode->prev=cur;
- (cur->next)->prev = newnode;
- cur->next=newnode;
- }
- else
- {
- newnode->prev = head;
- cur->next = newnode;
- }
- }
- void l_insert(int x)
- {
- struct node* newnode;
- newnode = (struct node*) malloc(sizeof(struct node));
- newnode -> prev = NULL;
- newnode -> data = x;
- newnode -> next = NULL;
- if(head != NULL)
- {
- newnode -> prev = end;
- end -> next = newnode;
- }
- else
- {
- head=newnode;
- }
- end = newnode;
- }
- void delete_1st()
- {
- struct node* cur = head;
- cur = head;
- head=head->next;
- head->prev = NULL;
- free(cur);
- }
- void Delete_mid()
- {
- int pos,i;
- printf("Enter your position for Delete :- ");
- scanf("%d",&pos);
- struct node *cur = head;
- for(i = 1; i < pos ; i++)
- {
- cur = cur -> next;
- }
- if(cur==head){
- head=head->next;
- head->prev = NULL;
- free(cur);
- }
- else if(cur==end){
- end=end->prev;
- end->next = NULL;
- free(cur);
- }
- else{
- (cur->prev)->next = cur->next;
- (cur->next)->prev = cur->prev;
- free(cur);
- }
- }
- void delete_last()
- {
- struct node* temp=end;
- while (temp -> next !=NULL)
- {
- temp = temp -> next;
- }
- end = end->prev;
- end->next=NULL;
- free(temp);
- }
- void printdata()
- {
- struct node* temp = head;
- while(temp != NULL){
- printf("%d >--< ", temp -> data);
- temp = temp -> next;
- }
- printf("\n");
- }
- int main()
- {
- int x, t;
- while(1)
- { printf("\t\t\t\tPress(1) for First Insert\n\t\t\t\tPress(2) for last Middle\n\t\t\t\tPress(3) for insert Last\n");
- printf("\t\t\t\tPress(4) for delete First\n\t\t\t\tPress(5) for delete Middle\n\t\t\t\tPress(6) for delete last\n");
- printf("\nchoose a option :-");
- scanf("%d",&t);
- if(t==1)
- {
- printf("Input a Data :- ");
- scanf("%d", &x);
- insert(x);
- printdata();
- }
- else if( t == 2)
- {
- int pos;
- printf("Input a Data :- ");
- scanf("%d", &x);
- printf("Enter position for insert middle :- ");
- scanf("%d",&pos);
- insert_middle(x,pos);
- printdata();
- }
- else if(t==3)
- {
- printf("Input a Data :- ");
- scanf("%d", &x);
- l_insert(x);
- printdata();
- }
- else if(t==4)
- {
- delete_1st();
- printdata();
- }
- else if(t==5)
- {
- Delete_mid();
- printdata();
- }
- else if(t==6)
- {
- delete_last();
- printdata();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement