Repository navigation
Expand file tree
/
Copy pathNodes
More file actions
77 lines (75 loc) · 1.55 KB
/
Copy pathNodes
File metadata and controls
77 lines (75 loc) · 1.55 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;
struct Node*rear;
};
void initQueue(struct Queue*q) {
q->front=NULL;
q->rear=NULL;
}
int isEmpty(struct Queue*q) {
return(q->front==NULL);
};
void enqueue(struct Queue*q,int value) {
struct Node*newNode=(struct Node*)malloc(sizeof(struct Node));
if (newNode==NULL) {
printf("Heap Overflow\n");
return;
}
newNode->data=value;
newNode->next=NULL;
if (q->rear==NULL){
q->front=q->rear=newNode;
return;
}
q->rear->next=newNode;
q->rear=newNode;
}
void dequeue(struct Queue*q) {
if (isEmpty(q)) {
printf("Queue Underflow\n");
return;
}
struct Node*temp=q->front;
printf("Deleted element:%d\n",temp->data);
q->front=q->front->next;
if (q->front==NULL)
q->rear=NULL;
free(temp);
}
void peek(struct Queue*q) {
if(isEmpty(q)) {
printf("Queue is empty\n");
return;
}
printf("Front element:%d\n",q->front->data);
}
void display(struct Queue*q) {
if (isEmpty(q)) {
printf("Queue is empty\n");
return;
}
struct Node*temp=q->front;
printf("Queue elements: ");
while (temp !=NULL){
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
int main() {
struct Queue q;
initQueue(&q);
enqueue(&q,10);
enqueue(&q,20);
enqueue(&q,30);
display(&q);
dequeue(&q);
display(&q);
peek(&q);
return 0;