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,*next;
- } *head = NULL;
- struct node *create(int value){
- struct node *newNode = (struct node*) malloc(sizeof(struct node));
- newNode->data = value; newNode->prev = newNode->next = NULL;
- return newNode;
- }
- void insert(struct node *newNode){
- struct node *help = head;
- if(!head || newNode->data < head->data){ //Front insertion
- if(head){
- newNode->next = head;
- head->prev = newNode;
- }
- head = newNode;
- }
- else{
- while(help->next && newNode->data >= help->data) help = help->next;
- if(!help->next && newNode->data >= help->data){ //Back insertion
- help->next = newNode;
- newNode->prev = help;
- }
- if(newNode->data < help->data){ //Middle insertion
- newNode->next = help;
- newNode->prev = help->prev;
- help->prev->next = newNode;
- help->prev = newNode;
- }
- }
- }
- void delet(int value){
- struct node *help = head, *target = NULL;
- while(help && help->data != value) help = help->next;
- if(!help) printf("\nData not found.");
- else{
- target = help;
- if(target == head) head = head->next;
- else{
- if(target->prev) target->prev->next = target->next;
- if(target->next) target->next->prev = target->prev;
- }
- free(target);
- }
- }
- void display(){
- struct node *help = head;
- printf("\nThe list is: ");
- if(!head) printf("Empty.");
- while(help){
- printf("\t%d\t", help->data);
- help = help->next;
- }
- }
- int main(){
- int choice, n;
- while(1){
- display();
- printf("\n\n1.Insert 2.Delete 3.Exit: ");
- scanf("%d", &choice);
- switch(choice){
- case 1: printf("Enter the value to enter: ");
- scanf("%d", &n);
- insert(create(n));
- break;
- case 2: if(head){
- printf("Enter the value to delete: ");
- scanf("%d", &n);
- delet(n);
- }break;
- case 3: return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement