-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue1.h
91 lines (80 loc) · 1.25 KB
/
queue1.h
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
#include <iostream>
#include<conio.h>
#include<bits/stdc++.h>
using namespace std;
// Structure of Node.
struct Node
{
string cname;
Node *link;
};
Node *front = NULL;
Node *rear = NULL;
//Function to check if queue is empty or not
bool isempty()
{
if(front == NULL && rear == NULL)
return true;
else
return false;
}
//function to enter elements in queue
void enqueue ( string name )
{
Node *ptr = new Node();
ptr->cname= name;
ptr->link = NULL;
//If inserting the first element/node
if( front == NULL )
{
front = ptr;
rear = ptr;
}
else
{
rear ->link = ptr;
rear = ptr;
}
}
//function to delete/remove element from queue
void dequeue ( )
{
if( isempty() )
cout<<"Queue is empty\n";
else
//only one element/node in queue.
if( front == rear)
{
free(front);
front = rear = NULL;
}
else
{
Node *ptr = front;
front = front->link;
free(ptr);
}
}
//function to show the element at front
void showfront( )
{
if( isempty())
cout<<"Queue is empty\n";
else
cout<<"element at front is:"<<front->cname<<"\n";
}
//function to display queue
void displayQueue()
{
if (isempty())
cout<<"Queue is empty\n";
else
{
Node *ptr = front;
while( ptr !=NULL)
{
cout<<"\t"<<ptr->cname<<"\t "<<endl;
ptr= ptr->link;
}
}
}