-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_implementation_using_linked_list.c
More file actions
81 lines (74 loc) · 1.59 KB
/
Queue_implementation_using_linked_list.c
File metadata and controls
81 lines (74 loc) · 1.59 KB
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
// Queue Implementation using Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front = NULL, *rear = NULL, *newnode = NULL, *temp = NULL;
void enqueue();
void dequeue();
void display();
int main()
{
enqueue();
display();
dequeue();
display();
return 0;
}
void enqueue()
{
int choice = 1, n;
while(choice)
{
newnode = (struct node *)malloc(sizeof(struct node));
if(newnode == NULL)
printf("Memory Not allocate.");
else
{
printf("Enter data of node\n");
scanf("%d", &n);
newnode->data = n;
newnode->next = NULL;
if(front == NULL && rear == NULL)
front = rear = newnode;
else
{
rear->next = newnode;
rear = newnode;
}
printf("Do you want to add another node in Queue, press 1 for continue or 0 for exit\n");
scanf("%d", &choice);
}
}
}
void dequeue()
{
if(front == NULL && rear == NULL)
printf("Queue is Empty.");
else
{
temp = front;
printf("\nThe Dequeued element is: %d\n", temp->data);
front = front->next;
free(temp);
temp = NULL;
}
}
void display()
{
if (front == NULL && rear == NULL)
printf("Queue is Empty.");
else
{
temp = front;
printf("\nThe elements of Queues are\n");
while(temp!=NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
}