mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 14:26:29 +08:00
Update cpp codes for the stack and the queue.
This commit is contained in:
parent
01e211c7ee
commit
3302354909
4 changed files with 12 additions and 16 deletions
|
@ -50,11 +50,10 @@ public:
|
|||
}
|
||||
|
||||
/* 出队 */
|
||||
int poll() {
|
||||
void poll() {
|
||||
int num = peek();
|
||||
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
||||
front = (front + 1) % capacity();
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -98,8 +97,8 @@ int main() {
|
|||
cout << "队首元素 peek = " << peek << endl;
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue->poll();
|
||||
cout << "出队元素 poll = " << poll << ",出队后 queue = ";
|
||||
queue->poll();
|
||||
cout << "出队元素 poll = " << peek << ",出队后 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
|
|
@ -28,10 +28,9 @@ public:
|
|||
}
|
||||
|
||||
/* 出栈 */
|
||||
int pop() {
|
||||
void pop() {
|
||||
int oldTop = top();
|
||||
stack.pop_back();
|
||||
return oldTop;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
|
@ -67,8 +66,8 @@ int main() {
|
|||
cout << "栈顶元素 top = " << top << endl;
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack->pop();
|
||||
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
|
||||
stack->pop();
|
||||
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
|
||||
PrintUtil::printVector(stack->toVector());
|
||||
|
||||
/* 获取栈的长度 */
|
||||
|
|
|
@ -47,7 +47,7 @@ public:
|
|||
}
|
||||
|
||||
/* 出队 */
|
||||
int poll() {
|
||||
void poll() {
|
||||
int num = peek();
|
||||
// 删除头结点
|
||||
ListNode *tmp = front;
|
||||
|
@ -55,7 +55,6 @@ public:
|
|||
// 释放内存
|
||||
delete tmp;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
|
@ -97,8 +96,8 @@ int main() {
|
|||
cout << "队首元素 peek = " << peek << endl;
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue->poll();
|
||||
cout << "出队元素 poll = " << poll << ",出队后 queue = ";
|
||||
queue->poll();
|
||||
cout << "出队元素 poll = " << peek << ",出队后 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
|
|
|
@ -37,14 +37,13 @@ public:
|
|||
}
|
||||
|
||||
/* 出栈 */
|
||||
int pop() {
|
||||
void pop() {
|
||||
int num = top();
|
||||
ListNode *tmp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
// 释放内存
|
||||
delete tmp;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
|
@ -86,8 +85,8 @@ int main() {
|
|||
cout << "栈顶元素 top = " << top << endl;
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack->pop();
|
||||
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
|
||||
stack->pop();
|
||||
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
|
||||
PrintUtil::printVector(stack->toVector());
|
||||
|
||||
/* 获取栈的长度 */
|
||||
|
|
Loading…
Reference in a new issue