-
Notifications
You must be signed in to change notification settings - Fork 0
/
单向队列的链表实现.cpp
164 lines (160 loc) · 3.79 KB
/
单向队列的链表实现.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
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
#include <iostream>
#define NIL NULL
using namespace std;
template <class T>
struct node {
T data;
node* next;
};
//所有下标均从1开始计数
template<class T>
class MyList {
protected:
int n;
node<T>* head;
node<T>* tail;
public:
MyList(){init();} //构造函数, 结点数初始化为0
void init() { //初始化链表
n = 0;
head = new node<T>;
head->next = NIL;
tail = head;
}
//only available for queue!
T Front() {
return head->next->data;
}
void push(T x) { //相当于push_back()
node<T>* p = new node<T>;
p->data = x;
p->next = NIL;
if(n==0) tail=head; /*special for queue.cpp*/
tail->next = p;
tail = p;
n++;
}
bool Insert(int pos,T val) { //插在指定位置处
if(pos>n) return false;
node<T>* p=new node<T>;
p->data=val;
node<T>* pr=prve(pos);
p->next=pr->next;
pr->next=p;
n++;
return true;
}
node<T>* prve(int pos) { //寻找前驱结点
pos--;
node<T>* p=head;
while(pos--) p=p->next;
return p;
}
bool Delete(int pos) { //删除结点
if(pos>n) return false;
node<T>* pr=prve(pos);
node<T>* p=head;
while(pos--) p=p->next;
pr->next=p->next;
delete p;
n--;
return true;
}
bool Modify(int pos,T val) { //修改结点值
if(pos>n) return false;
node<T>* p=head;
while(pos--) p=p->next;
p->data=val;
return true;
}
int Find(T val) { //按值查找
int cnt=0;
node<T>* p=head->next;
while(p!=NIL) {
cnt++;
if( p->data==val ) {
return cnt;
}
p=p->next;
}
return -1;
}
T At(int pos) { //查询pos位置上的值
node<T>* p=head;
while(pos--) p=p->next;
return p->data;
}
void Display() { //展示链表内容
node<T>* p = head->next;
while(p!=NIL) {
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int how_many() {return n;} //查询结点数
~MyList() { //析构函数
node<T>* p=head;
node<T>* q;
while(p != NIL) {
q=p->next;
delete p;
p=q;
}
n = 0;
}
};
template<class T>
class MyQueue {
protected:
MyList<T> v;
const static int MAX = 1000;
public:
MyQueue() {}
bool Push(T val) {
if(isFull()) return false;
v.push(val);
return true;
}
bool Pop() {
if(isEmpty()) return false;
v.Delete(1);
return true;
}
T Front() {
if(!isEmpty())
return v.Front();
return T("error");
}
int getSize() {return v.how_many();}
bool isEmpty() {return v.how_many()==0;}
bool isFull() {return v.how_many()==MAX;}
~MyQueue(){}
};
//********************************************************
//********************************************************
int main() {
MyQueue<string> v;
string op;
while(cin>>op) {
if(op=="push") {
string x;
cin>>x;
v.Push(x);
} else if(op=="pop") {
if(v.Pop()) cout<<"pop successfully"<<endl;
else cout<<"error"<<endl;
} else if(op=="top") {
cout<<v.Front()<<endl;
} else if(op=="empty?") {
if(v.isEmpty()) cout<<"empty"<<endl;
else cout<<"not yet"<<endl;
} else if(op=="size") {
cout<<v.getSize()<<endl;
} else if(op=="full?") {
if(v.isFull()) cout<<"full"<<endl;
else cout<<"not yet"<<endl;
}
}
return 0;
}