forked from zjpzhao/JLU_data_structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-2queue.cpp
More file actions
164 lines (154 loc) · 2.23 KB
/
2-2queue.cpp
File metadata and controls
164 lines (154 loc) · 2.23 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
第二周第二题
1)创建链式队列;
2)插入操作:向队尾插入值为 x 的元素;
3)删除操作:删除队首元素;
4)存取操作:读取队首元素。
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<typename T>
class Node
{
public:
T data;
Node(T dat):data(dat){next=NULL;}
Node<T>*next;
};
template<typename T>
class Queue
{
Node<T>*head,*last;
int len;
public:
Queue(){head=last=NULL,len=0;}
int push(T);
int push_first(T);
int pop();
int pop_last();
vector<T> read();
bool empty();
int size();
int show();
};
template<typename T>
int Queue<T>::push(T dat)
{
if(empty())
{
push_first(dat);
return 0;
}
len++;
Node<T>*p=new Node<T>(dat);
last->next=p;
last=p;
return 0;
}
template<typename T>
int Queue<T>::push_first(T dat)
{
if(!empty())
return 1;
len++;
Node<T>*p=new Node<T>(dat);
head=last=p;
return 0;
}
template<typename T>
int Queue<T>::pop()
{
if(empty())
{
cout<<"空队列无法执行出队列操作"<<endl;
return 1;
}
if(len==1)
{
pop_last();
return 0;
}
len--;
Node<T>*p=head;
head=head->next;
delete p;
return 0;
}
template<typename T>
int Queue<T>::pop_last()
{
if(len!=1)
return 1;
len--;
delete head;
head=last=NULL;
return 0;
}
template<typename T>
vector<T> Queue<T>::read()
{
vector<T>v;
if(empty())
return v;
v.push_back(head->data);
return v;
}
template<typename T>
bool Queue<T>::empty()
{
if(size()==0)
return true;
return false;
}
template<typename T>
int Queue<T>::size()
{
return len;
}
template<typename T>
int Queue<T>::show()
{
if(empty())
{
cout<<"当前队列为空队列"<<endl;
return 1;
}
Node<T>*p=head;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
return 0;
}
int main()
{
string cmd;
class Queue<char>q;
char t;
while(cin>>cmd)
{
if(cmd=="push")
{
cin>>t;
q.push(t);
}
if(cmd=="pop")
{
q.pop();
}
if(cmd=="read")
{
vector<char>v=q.read();
if(v.empty())
cout<<"空队列无法执行取栈顶操作"<<endl;
else
cout<<" "<<v[0]<<endl;
}
q.show();
}
return 0;
}