-
Notifications
You must be signed in to change notification settings - Fork 72
/
Queue_code.c
122 lines (120 loc) · 3.41 KB
/
Queue_code.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
122
#include <stdio.h> //Header file for input and output
#include <stdlib.h>
// Here structure is created with a name node
struct node
{
int info; // It will represent the info part of node which is of integer type
struct node *link; // It is a ponter of type struct which will point the next node
};
void linkqinsert(); // linkqinsert function is declared
void linkqdelete(); // linkqdelete function is declared
void linkqdisplay(); // linkqdisplay function is declared
static int item;
struct node *front = NULL;
struct node *rear = NULL;
int main()
{
int choice;
printf("\n**C Program to perform several operation on the queue using linked list**\n\n");
while (1)
{
printf("\n1.To Insert Element to the queue\n");
printf("2.To Delete an Element from the queue\n");
printf("3.To Display all the Elements of queue\n");
printf("4.To Exit from the Program\n\n");
printf("Enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
linkqinsert();
break;
case 2:
linkqdelete();
break;
case 3:
linkqdisplay();
break;
case 4:
exit(0);
break;
default:
printf("Wrong Choice\n");
break;
}
}
return 0;
}
// linkqinsert() operation on a queue to insert element in the queue
void linkqinsert()
{
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node)); // Giving memory to newnode
if (newnode == NULL) // If avail list is full then no memory will be allocated
{
printf("Overflown\n");
}
else
{
printf("Enter value of Item you want to insert\n");
scanf("%d", &item);
newnode->info = item;
if (front == NULL) // for the first node when the queue is empty
{
front = newnode; // initializing front
rear = newnode; // initializing rear
front->link = NULL;
rear->link = NULL;
}
else
{
rear->link = newnode; // add newnode
rear = newnode; // making the newnode as rear
rear->link = NULL;
}
}
printf("**Item inserted successfully**\n\n");
}
// linkqdelete() operation on a queue to delete elements from the queue
void linkqdelete()
{
struct node *temp; // it is used to free the first node after deleting
temp = front;
if (temp == NULL)
{
printf("underflown\n");
}
else if (front->link != NULL)
{
front = front->link; // advance front to the next node
item = front->info;
free(temp);
}
else
{
item = front->info;
front = NULL;
rear = NULL;
free(temp);
}
printf("**Item Deleted Successfully**\n\n");
}
// linkqdispaly() to display all the elements of the queue
void linkqdisplay()
{
struct node *temp;
temp = front;
if (front == NULL)
{
printf("underflown\n");//queue is empty
}
else
{
printf("**Displaying all the Item in Queue**\n\n");
while (temp != NULL)
{
printf("Item in the Queue is : %d\n", temp->info);
temp = temp->link;
}
}
}