-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.cpp
More file actions
80 lines (77 loc) · 1.17 KB
/
queue.cpp
File metadata and controls
80 lines (77 loc) · 1.17 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
#include <bits/stdc++.h>
using namespace std;
class Queue
{
Queue *root=NULL;
int val;
Queue *next=NULL;
int sz=0;
public:
void push(int n)
{
sz++;
if(root==NULL)
{
root= new Queue;
root->val=n;
root->next=NULL;
}
else
{
Queue *cu=root;
while(cu->next!=NULL)cu=cu->next;
Queue *ne= new Queue;
ne->val=n;
ne->next=NULL;
cu->next=ne;
}
}
int size(){return sz;}
void pop()
{
if(root==NULL)return;
sz--;
Queue *cu=root->next;
delete (root);
root=cu;
}
void print()
{
Queue *cu=root;
if(cu==NULL)return ;
while(1)
{
printf("%d ", cu->val);
if(cu->next==NULL)
{
printf("\n");
break;
}
else cu=cu->next;
}
}
int front()
{
if(root==NULL)return 1/0;
return root->val;
}
bool empty(){return root==NULL;}
};
int main()
{
Queue v;
v.push(10);
v.pop();
// cout<<v.front()<<endl;
cout<<v.size()<<endl;
v.push(10);
v.push(20);
v.push(100);
v.push(102);
v.pop();
v.push(101);
cout<<v.size()<<"\n\n";
v.print();
cout<<v.empty()<<endl;
cout<<v.front()<<endl;
}