forked from akritianand9/test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuell.c
More file actions
84 lines (78 loc) · 1.4 KB
/
Copy pathqueuell.c
File metadata and controls
84 lines (78 loc) · 1.4 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
82
83
84
//queue implemented using ll
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};
typedef struct
{
struct node* front;
struct node* rear;
} QUEUE;
int insert(QUEUE* q, int val)
{
struct node* curr;
curr = (struct node*)malloc(sizeof(struct node));
if (curr == NULL)
{
return 1;
}
curr->data = val;
curr->next = NULL;
if (q->front == NULL)
{
q->front = q->rear = curr;
}
else
{
q->rear->next = curr;
q->rear = curr;
}
return 0;
}
int delete (QUEUE* q, int* del)
{
if (q->front == NULL)
{
return 1;
}
*del = q->front->data;
if (q->front == q->rear)
{
free(q->front);
q->front = q->rear = NULL;
}
else
{
struct node* prev = q->front;
q->front = q->front->next;
free(prev);
}
return 0;
}
int main()
{
QUEUE q;
q.front = q.rear = NULL;
int val = rand() % 10;
if (insert(&q, val) != 0)
{
printf("QUEUE Overflow\n");
}
else
{
printf("%d inserted successfully\n", val);
}
int del;
if (delete (&q, &del) != 0)
{
printf("QUEUE Underflow\n");
}
else
{
printf("%d deleted successfully\n", del);
}
return 0;
}