-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueLinkedList.c
115 lines (106 loc) · 2 KB
/
QueueLinkedList.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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int maxsize = 5;//max size of queue is 5
int size = 0;
struct Node* head = NULL;
void enqueue(int value)
{
if(size==maxsize)
{
printf("The queue is full\n");
return;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if(head==NULL)
{
newNode->data=value;
newNode->next=NULL;
head=newNode;
}
else
{
struct Node* temp = head;
while(temp->next!=NULL)
{
temp=temp->next;
}
newNode->data=value;
newNode->next=NULL;
temp->next=newNode;
}
size++;
printf("Enqueued %d ,now size of queue is %d\n",value,size);
}
void dequeue()
{
if(head==NULL)
{
printf("THE QUEUE IS EMPTY\n");
return;
}
struct Node* temp = head;
head=head->next;
printf("Dequeue element %d now size is %d\n",temp->data,size-1);
free(temp);
size--;
}
void display()
{
if(head==NULL)
{
printf("THE QUEUE IS EMPTY\n");
return;
}
struct Node* temp = head;
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("NULL \n");
}
void main()
{
int choice;
do
{
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int value;
printf("\nEnter the value : ");
scanf("%d",&value);
enqueue(value);
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("...EXITING...");
break;
}
default :
{
printf("INVALID OPTION");
}
}
}while(choice!=4);
return;
}