| 1 | #include<stdio.h> | ||||
| 2 | #include<stdlib.h> | ||||
| 3 | struct node | ||||
| 4 | { | ||||
| 5 | int data; | ||||
| 6 | struct node *previous; | ||||
| 7 | struct node *next; | ||||
| 8 | }; | ||||
| 9 | struct node *front, *rear; | ||||
| 10 | int count; | ||||
| 11 | void display(); | ||||
| 12 | void insert_begin(int x); | ||||
| 13 | void insert_last(int x); | ||||
| 14 | int delete_begin(); | ||||
| 15 | int delete_last(); | ||||
| 16 | int main() | ||||
| 17 | { | ||||
| 18 | int ch; | ||||
| 19 | int ele; | ||||
| 20 | printf("\n\t1. Insert-begin\n\t2. Insert-last\n\t3. Delete-begin\n\t4. Delete-last\n\t5. Display | ||||
| 21 | \n6.exit"); | ||||
| 22 | while(1) | ||||
| 23 | { | ||||
| 24 | printf("Enter_your_choice:"); | ||||
| 25 | scanf("%d",&ch); | ||||
| 26 | switch(ch) | ||||
| 27 | { | ||||
| 28 | case 1: | ||||
| 29 | printf("Enter_value_for_insertion :"); | ||||
| 30 | scanf("%d",&ele); | ||||
| 31 | insert_begin(ele); | ||||
| 32 | break; | ||||
| 33 | case 2: | ||||
| 34 | printf(" Enter_the_value_for_insertion:"); | ||||
| 35 | scanf("%d",&ele); | ||||
| 36 | insert_last(ele); | ||||
| 37 | break; | ||||
| 38 | case 3: | ||||
| 39 | ele = delete_begin(); | ||||
| 40 | if(ele!=-1) | ||||
| 41 | printf("%d is deleted .",ele); | ||||
| 42 | break; | ||||
| 43 | case 4: | ||||
| 44 | ele = delete_last(); | ||||
| 45 | if(ele!=-1) | ||||
| 46 | printf("%d is deleted .",ele); | ||||
| 47 | break; | ||||
| 48 | case 5: | ||||
| 49 | display(); | ||||
| 50 | break; | ||||
| 51 | case 6: exit(0); | ||||
| 52 | } | ||||
| 53 | } | ||||
| 54 | } | ||||
| 55 | void display() | ||||
| 56 | { | ||||
| 57 | struct node * ptr; | ||||
| 58 | ptr = front; | ||||
| 59 | if(front==NULL || rear==NULL) | ||||
| 60 | { | ||||
| 61 | printf("List is empty"); | ||||
| 62 | return; | ||||
| 63 | } | ||||
| 64 | while(ptr != NULL) | ||||
| 65 | { | ||||
| 66 | printf( "%d -> ",ptr ->data); | ||||
| 67 | ptr = ptr->next; | ||||
| 68 | } | ||||
| 69 | printf("\n");; | ||||
| 70 | } | ||||
| 71 | void insert_begin(int x) | ||||
| 72 | { | ||||
| 73 | struct node *new1; | ||||
| 74 | new1 = (struct node*)malloc(sizeof(struct node)); | ||||
| 75 | new1 -> data =x; | ||||
| 76 | new1 ->previous = new1 ->next =NULL; | ||||
| 77 | if(front == NULL||rear==NULL) | ||||
| 78 | front = rear = new1; | ||||
| 79 | else | ||||
| 80 | { | ||||
| 81 | new1 ->next = front; | ||||
| 82 | front ->previous = new1; | ||||
| 83 | front = new1; | ||||
| 84 | } | ||||
| 85 | } | ||||
| 86 | void insert_last(int x) | ||||
| 87 | { | ||||
| 88 | struct node *new1; | ||||
| 89 | new1 = (struct node*)malloc(sizeof(struct node));; | ||||
| 90 | new1 ->data = x; | ||||
| 91 | new1 -> previous = new1 ->next = NULL; | ||||
| 92 | if (front == NULL||rear==NULL) | ||||
| 93 | front = rear = new1; | ||||
| 94 | else | ||||
| 95 | { | ||||
| 96 | rear ->next = new1; | ||||
| 97 | new1 ->previous = rear; | ||||
| 98 | rear = new1; | ||||
| 99 | } | ||||
| 100 | } | ||||
| 101 | int delete_begin() | ||||
| 102 | { | ||||
| 103 | int x; | ||||
| 104 | struct node *temp; | ||||
| 105 | if (front == NULL || rear==NULL) | ||||
| 106 | { | ||||
| 107 | printf( " LIST_IS_EMPTY "); | ||||
| 108 | return -1; | ||||
| 109 | } | ||||
| 110 | else | ||||
| 111 | { | ||||
| 112 | temp = front; | ||||
| 113 | x= temp->data; | ||||
| 114 | if(front==rear) | ||||
| 115 | { | ||||
| 116 | front=NULL; | ||||
| 117 | rear=NULL; | ||||
| 118 | } | ||||
| 119 | else | ||||
| 120 | { | ||||
| 121 | front = front->next; | ||||
| 122 | front->previous = NULL; | ||||
| 123 | } | ||||
| 124 | count --; | ||||
| 125 | free(temp); | ||||
| 126 | return x; | ||||
| 127 | } | ||||
| 128 | } | ||||
| 129 | int delete_last( ) | ||||
| 130 | { | ||||
| 131 | int x; | ||||
| 132 | struct node *temp; | ||||
| 133 | if(rear == NULL || front==NULL) | ||||
| 134 | { | ||||
| 135 | printf( " LIST_IS_EMPTY "); | ||||
| 136 | return -1; | ||||
| 137 | } | ||||
| 138 | else | ||||
| 139 | { | ||||
| 140 | temp = rear; | ||||
| 141 | if(front==rear) | ||||
| 142 | { | ||||
| 143 | front=NULL; | ||||
| 144 | rear=NULL; | ||||
| 145 | } | ||||
| 146 | else | ||||
| 147 | { | ||||
| 148 | rear = rear->previous; | ||||
| 149 | rear -> next = NULL; | ||||
| 150 | } | ||||
| 151 | x= temp ->data; | ||||
| 152 | free(temp); | ||||
| 153 | count --; | ||||
| 154 | return x; | ||||
| 155 | } | ||||
| 156 | } | ||||
| Output: | |||||
| 1. Insert-begin | |||||
| 2. Insert-last | |||||
| 3. Delete-begin | |||||
| 4. Delete-last | |||||
| 5. Display | |||||
| 6.exit | |||||
| Enter_your_choice:1 | |||||
| Enter value for insertion :10 | |||||
| Enter_your_choice:2 | |||||
| Enter_the_value_for_insertion:20 | |||||
| Enter_your_choice:5 | |||||
| 10 -> 20 -> | |||||
| Enter_your_choice:1 | |||||
| Enter_value_for_insertion :30 | |||||
| Enter_your_choice:5 | |||||
| 30 -> 10 -> 20 -> | |||||
| Enter_your_choice:2 | |||||
| Enter_the_value_for_insertion:40 | |||||
| Enter_your_choice:5 | |||||
| 30 -> 10 -> 20 -> 40 -> | |||||
| Enter_your_choice:3 | |||||
| 30 is deleted . | |||||
| Enter_your_choice:5 | |||||
| 10 -> 20 -> 40 -> | |||||
| Enter_your_choice:4 | |||||
| 40 is deleted . | |||||
| Enter_your_choice:5 | |||||
| 10 -> 20 -> | |||||
| Enter_your_choice:6 | |||||
C programs for Double ended queue ADT Using Doubly Linked List
Share to other apps