-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
126 lines (118 loc) · 1.73 KB
/
queue.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>
#include<stdio.h>
using namespace std;
class Queue
{
private:
struct node
{
int item;
node *next;
}*front, *rear;
public:
Queue()
{
front = NULL;
rear = NULL;
}
void insert(int data)
{
node *n= new node();
n->item = data;
n->next = NULL;
if(rear == NULL)
{
front = rear = n;
}
else
{
rear->next = n;
rear = n;
}
}
int remove()
{
if(front == NULL)
{
return 0;
}
if(front == rear) //list has got single element
{
delete front;
front = rear = NULL;
return 1;
}
node *r = front;
front = front->next;
delete r;
return 1;
}
int getFirst()
{
if (front == NULL)
{
return -1;
}
else
{
return front->item;
}
}
int count()
{
int c= 0;
if (front ==rear)
{
return c;
}
node *t = front;
while(t!=rear)
{
t=t->next;
c++;
}
c++;
return c;
}
int isEmpty()
{
if (front == NULL)
return 1;
else
return 0;
}
void print()
{
node*t=front;
if(isEmpty() == 0 )
{
while(t!=rear)
{
cout<<t->item<<"\t\t";
t=t->next;
}
cout<<endl;
}
else if(isEmpty() == 1)
{
cout<<"The list is empty\n";
}
}
};
int main()
{
Queue list1;
list1.insert(10);
list1.insert(20);
list1.insert(30);
list1.insert(40);
list1.insert(50);
list1.print();
cout<<"The number of elements is "<<list1.count()<<endl;
cout<<"The first element is "<<list1.getFirst()<<endl;
cout<<"remove : "<<list1.remove()<<"\n";
list1.print();
cout<<"remove : "<<list1.remove()<<"\n";
list1.print();
cout<<"remove : "<<list1.remove()<<"\n";
}