forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueUsingLinkedList.c
110 lines (103 loc) · 1.8 KB
/
QueueUsingLinkedList.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
#include <stdio.h>
struct node
{
int info;
struct node* link;
};
int isEmpty(struct node* front)
{
return (front==NULL);
}
void insert(struct node **front, struct node **rear, int item)
{
struct node* tmp;
tmp = (struct node*)malloc(sizeof(struct node));
if (tmp==NULL)
{
printf("Memory not available\n");
return;
}
tmp->info=item;
tmp->link=NULL;
if (*front==NULL)
*front=tmp;
else
(*rear)->link=tmp;
*rear=tmp;
}
int del(struct node **front, struct node **rear)
{
struct node* tmp;
int item;
if (isEmpty(*front))
{
printf("Queue Underflow\n");
exit(1);
}
tmp=*front;
item=tmp->info;
*front=(*front)->link;
free(tmp);
return item;
}
int peek(struct node* front)
{
if (isEmpty(front))
{
printf("Queue Underflow\n");
exit(1);
}
return front->info;
}
void display(struct node* front)
{
struct node* ptr;
ptr=front;
if (isEmpty(front))
{
printf("Queue is empty!\n");
return;
}
printf("Queue elements : \n");
while (ptr!=NULL)
{
printf("%d ",ptr->info);
ptr=ptr->link;
}
printf("\n\n");
}
int main()
{
struct node* front=NULL;
struct node* rear=NULL;
int choice, item;
while (1)
{
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display element at the front\n");
printf("4. Display all elements of the queue\n");
printf("5. Quit\n");
printf("Enter You Choice: ");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("\nEnter the element to be inserted: ");
scanf("%d",&item);
insert(&front,&rear,item);break;
case 2:
item=del(&front,&rear);
printf("\nThe deleted item is: %d\n\n",item);break;
case 3:
printf("\nItem at the front is: %d\n\n",peek(front));break;
case 4:
display(front);break;
case 5:
exit(1);
default:
printf("\nPlease enter a choice between 1 and 5\n\n");
}
}
return 0;
}