-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearqueuelinkedlist.c
More file actions
57 lines (52 loc) · 1.12 KB
/
linearqueuelinkedlist.c
File metadata and controls
57 lines (52 loc) · 1.12 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
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *front = NULL, *rear = NULL;
void enqueue(int x) {
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = x;
newnode->next = NULL;
if (front == NULL && rear == NULL) {
front = rear = newnode;
} else {
rear->next = newnode;
rear = newnode;
}
}
void dequeue() {
if (front == NULL && rear == NULL) {
printf("Underflow\n");
return;
}
struct node *temp = front;
printf("Deleted: %d\n", front->data);
if (front == rear) {
front = rear = NULL;
} else {
front = front->next;
}
free(temp);
}
void display() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
struct node *temp = front;
printf("Queue: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(15); enqueue(10); enqueue(30);
display();
dequeue();
display();
return 0;
}