forked from garimasingh128/CP-DSA-Cpp-C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlistrepresentationofqueue.cpp
More file actions
78 lines (62 loc) · 1.18 KB
/
linkedlistrepresentationofqueue.cpp
File metadata and controls
78 lines (62 loc) · 1.18 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
#include <stdio.h>
#include <stdlib.h>
/* List Structure */
typedef struct Node
{
int data;
struct Node *link;
}node;
node *head=NULL; // Head node to keep track of list
/* Driver Functions */
void EnQueque(int data);
int DeQueue();
void print(node *p);
/* Main Method */
int main()
{
EnQueque(0);
EnQueque(1);
EnQueque(2);
EnQueque(3);
DeQueue(); // Delete 0 from list
DeQueue(); // Delete 1 from list
EnQueque(4); // Add 4 at the end of list
print(head); // Print element of queue
return 0;
}
/* Insert Element */
void EnQueque(int data)
{
// Declaring node
node *temp = (node*)calloc(1,sizeof(node));
temp->data = data;
temp->link = NULL;
// If head is NULL or first node
if(!head)
{
head = temp;
return;
}
node *traverse=head;
// Traverse list upto end
while(traverse->link)
traverse = traverse->link;
traverse->link = temp;
}
/* Delete Element */
int DeQueue()
{
node* temp = head;
head = head->link;
int data = temp->data;
free(temp);
return data;
}
/* Print queue */
void print(node *p)
{
printf(" %d",p->data);
if(!p->link)
return;
print(p->link);
}