-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDynamicQueueUsingDoublePointer
More file actions
94 lines (84 loc) · 2.49 KB
/
Copy pathDynamicQueueUsingDoublePointer
File metadata and controls
94 lines (84 loc) · 2.49 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
85
86
87
88
89
90
91
92
93
94
/**
Dynamic Queue Using double pointer
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
// function which inserts node in queue
void enQueue(struct node **front,struct node **rear,int value)
{
struct node *newNode;
// create new node and accept data and set next part as NULL by default
newNode = (struct node*)malloc(sizeof(struct node));
newNode->next = NULL;
newNode->data = value;
if ( (*rear) == NULL ) // if rear is NULL, means queue is empty
{
*front = newNode; // as queue is empty, move front as well as rear to newNode
*rear = newNode; // as queue is empty, move front as well as rear to newNode
}
else
{
(*rear)->next = newNode; // place new node next to rear
(*rear) = newNode; // move rear to new inserted node
}
}
// function which inserts node in queue
void deQueue(struct node **front,struct node **rear)
{
struct node *nodeToDelete;
if ( (*front) == NULL ) // if ront is NULL, means Queue is empty
{
printf("Queue is empty");
}
else
{
nodeToDelete = (*front);
printf("Deleted Element = %d",nodeToDelete->data); //display deleted element
if ( (*front) == (*rear) ) // If front == rear, means there is only one node
{
(*front) = NULL;
(*rear) = NULL;
}
else
{
//front != rear, means there is more than one node, just move front to next node
(*front) = (*front)->next;
}
free(nodeToDelete);
}
}
// function which displays whole Queue
void displayQueue(struct node **front,struct node **rear)
{
struct node *curr;
if ( front == NULL ) // if front is NULL, means Queue is empty
{
printf("Queue is empty."); // give error message
}
else
{
printf("Queue is : |");
curr = (*front); // Move curr to front of Queue
while ( curr != NULL ) // move till curr does not become NULL
{
printf(" %d |",curr->data); // display node's data
curr = curr->next; // Move curr to next node
}
}
}
int main()
{
struct node *front = NULL,*rear = NULL;
enQueue(&front,&rear,10);
enQueue(&front,&rear,20);
enQueue(&front,&rear,30);
displayQueue(&front,&rear);
deQueue(&front,&rear);
displayQueue(&front,&rear);
return 0;
}