-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcircularqueuedynamic.c
121 lines (119 loc) · 2.4 KB
/
circularqueuedynamic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<stdio.h>
#include<stdlib.h>
int enqueue();
int dequeue();
int display();
int isEmpty();
typedef struct queue
{
int element;
struct queue *next;
}queue;
queue *head=NULL;
void main()
{
int choice,status;
while(1)
{
printf("\n*******************************");
printf("\n1.Enqueue element");
printf("\n2.Dequeue element");
printf("\n3.Display element");
printf("\n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
printf("\n");
switch(choice)
{
case 1:
status=enqueue();
if(status==1)
printf("QUEUE is full...");
else
printf("succesfully enqueue element");
break;
case 2:
status=dequeue();
if(status==1)
printf("QUEUE is empty...");
break;
case 3:
status=display();
if(status==1)
printf("Queue is empty...");
else
printf("\nSuccessfully printed");
break;
case 4:
exit(0);
default:
printf("Enter valid choice");
}
}
}
int isEmpty()
{
if(head==NULL)
return 1;
return 0;
}
int enqueue()
{
queue *newnode=NULL,*temp;
newnode=(queue*)malloc(sizeof(queue));
if(newnode==NULL)
return 1;
int data;
printf("Enter your data:");
scanf("%d",&data);
newnode->element=data;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
return 0;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
return 0;
}
int dequeue()
{
queue *temp;
int status=isEmpty();
if(status==1)
return 1;
printf("deleted item:%d",head->element);
if(head->next==NULL)
{
free(head);
head=NULL;
return 0;
}
temp=head;
head=head->next;
free(temp);
return 0;
}
int display()
{
queue *temp;
int status=isEmpty();
if(status==1)
return 1;
temp=head;
printf("Elements in Queue are:");
while(temp!=NULL)
{
printf("%d ",temp->element);
temp=temp->next;
}
return 0;
}