-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_linked_list.c
More file actions
78 lines (64 loc) · 1.22 KB
/
queue_linked_list.c
File metadata and controls
78 lines (64 loc) · 1.22 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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front, *rear;
};
struct node* new(int val);
struct queue* create_queue();
void enqueue(struct queue *q, int val);
void dequeue(struct queue *q);
int main()
{
struct queue *q = create_queue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
dequeue(q);
printf("Front: %d\n", q->front->data);
printf("Rear: %d\n", q->rear->data);
return 0;
}
struct node* new(int val)
{
struct node *tmp = (struct node*) malloc(sizeof(struct node));
tmp->data = val;
tmp->next = NULL;
return tmp;
}
struct queue* create_queue()
{
struct queue* q = (struct queue*) malloc(sizeof(struct queue));
q->front = q->rear = NULL;
return q;
}
void enqueue(struct queue* q, int val)
{
struct node *tmp = new(val);
if(q->rear == NULL)
{
q->front = q->rear = tmp;
return;
}
q->rear->next = tmp;
q->rear = tmp;
}
void dequeue(struct queue* q)
{
if(q->front == NULL)
{
return;
}
struct node *tmp = q->front;
q->front = q->front->next;
if(q->front == NULL)
{
q->rear = NULL;
}
free(tmp);
}