1 | #include<stdio.h> | |||
2 | #include<stdlib.h> | |||
3 | #define MAXSIZE 4 | |||
4 | int Queue[MAXSIZE],rear=-1,front=-1,i; | |||
5 | void insert_at_front(); | |||
6 | void insert_at_rear(); | |||
7 | void delete_at_front(); | |||
8 | void delete_at_rear(); | |||
9 | void display(); | |||
10 | int main() | |||
11 | { | |||
12 | int ch; | |||
13 | printf("\n 1.insert at front\n 2.insert at rear\n\n 3.delete at front\n4.delete at rear\n5.DISPLAY\n\n | |||
14 | 6.EXIT\n"); | |||
15 | do | |||
16 | { | |||
17 | printf("\n\tenter_your_choice[1-6]:"); | |||
18 | scanf("%d",&ch); | |||
19 | switch(ch) | |||
20 | { | |||
21 | case 1:insert_at_front(); | |||
22 | break; | |||
23 | case 2:insert_at_rear(); | |||
24 | break; | |||
25 | case 3:delete_at_front(); | |||
26 | break; | |||
27 | case 4:delete_at_rear(); | |||
28 | break; | |||
29 | case 5:display(); | |||
30 | break; | |||
31 | case 6:exit(0); | |||
32 | default:printf("invalid_option \n"); | |||
33 | } | |||
34 | }while(1); | |||
35 | } | |||
36 | void insert_at_rear() | |||
37 | { | |||
38 | if(rear==MAXSIZE-1) | |||
39 | { | |||
40 | printf("queue_is_full(overflow)\n"); | |||
41 | return; | |||
42 | } | |||
43 | rear++; | |||
44 | printf("enter_the_element_to_insert_at_rear:"); | |||
45 | scanf("%d",&Queue[rear]); | |||
46 | if(front==-1) | |||
47 | front++; | |||
48 | } | |||
49 | void insert_at_front() | |||
50 | { | |||
51 | int num; | |||
52 | if(front<=1) | |||
53 | { | |||
54 | printf("\n Can't add_item at front_end"); | |||
55 | return; | |||
56 | } | |||
57 | else | |||
58 | { | |||
59 | front--; | |||
60 | printf("\n Enter_item to insert:"); | |||
61 | scanf("%d",&Queue[front]); | |||
62 | } | |||
63 | } | |||
64 | void delete_at_front() | |||
65 | { | |||
66 | if(front==-1) | |||
67 | { | |||
68 | printf("queue_is empty(underflow)\n"); | |||
69 | return; | |||
70 | } | |||
71 | printf("the_delete_at_front_element is:%d \n",Queue[front]); | |||
72 | if(front==rear) | |||
73 | front=rear=-1; | |||
74 | else | |||
75 | front++; | |||
76 | } | |||
77 | void delete_at_rear() | |||
78 | { | |||
79 | if(rear==-1) | |||
80 | { | |||
81 | printf("queue_is empty(underflow)\n"); | |||
82 | return; | |||
83 | } | |||
84 | printf("the_delete_at_rear element is:%d \n",Queue[rear]); | |||
85 | if(front==rear) | |||
86 | front=rear=-1; | |||
87 | else | |||
88 | { | |||
89 | rear--; | |||
90 | } | |||
91 | } | |||
92 | void display() | |||
93 | { | |||
94 | if(front==-1) | |||
95 | { | |||
96 | printf("queue_is empty(underflow)\n"); | |||
97 | return; | |||
98 | } | |||
99 | printf("the_element_in_queue are:front->"); | |||
100 | for(i=front;i<=rear;i++) | |||
101 | printf("%d ",Queue[i]); | |||
102 | printf("<-REAR\n"); | |||
103 | } | |||
Output | ||||
1.insert at front | ||||
2.insert at rear | ||||
3.delete at front | ||||
4.delete at rear | ||||
5.DISPLAY | ||||
6.EXIT | ||||
enter_your_choice[1-6]:2 | ||||
enter_the element_to_insert_at_rear:10 | ||||
enter_your_choice[1-6]:2 | ||||
enter_the element_to_insert_at_rear:30 | ||||
enter_your_choice[1-6]:5 | ||||
the_element in_queue are:front->10 30 <-REAR | ||||
enter_your_choice[1-6]:2 | ||||
enter the element to insert_at_rear:40 | ||||
enter_your_choice[1-6]:5 | ||||
the_element in_queue are:front->10 30 40 <-REAR | ||||
enter_your_choice[1-6]:3 | ||||
the delete_at_front element is:10 | ||||
enter_your_choice[1-6]:5 | ||||
the_element in_queue are:front->30 40 <-REAR | ||||
enter_your choice[1-6]:1 | ||||
Enter_item to insert:40 | ||||
enter_your_choice[1-6]:5 | ||||
the_element in queue are:front->40 30 40 <-REAR | ||||
enter_your_choice[1-6]:4 | ||||
the_delete_at_rear element is:40 |
C programs for Double ended queue ADT Using Array
Share to other apps