Stack and Queue
来源:互联网 发布:linux mysql导出 编辑:程序博客网 时间:2024/05/22 14:05
Stack and Queue
标签(空格分隔): 程序设计实验 c++
本人学院
- Stack and Queue
- 标签空格分隔 程序设计实验 c
- 本人学院
- Introduction
- 读题
- my answer
- the standard answer
- 反馈
Introduction
Welcome to the c++ D&A (data structure and algorithm) topic. This topic will lead us to achieve a set of basic data structures and algorithm based on the c++ features. This time we are going to implement a simple stack as well as a simple queue using encapsulation in c++.
Knowledge
stack
In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out). Additionally, a peek operation may give access to the top without modifying the stack.
queue
In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed.
Requirement
The initialization of the class will be given(also known as a constructor), you can either ignore it or have an early learning.
For Stack:
Your job is to implement the following functions with some other limits:
// you can not push an element when the storage is full
void push(int);
// you can not pop an element when the storage is empty
void pop(void);
// when the stack is empty, you should return 0
int peek(void);
bool isEmpty(void);
bool isFull(void);
void clear(void);
For Queue:
Your job is to implement the following functions with some other limits:
// you can not push an element when the storage is full
void push(int);
// you can not pop an element when the storage is empty
void pop(void);
// when the stack is empty, you should return 0
int front(void);
// when the stack is empty, you should return 0
int back(void);
bool isFull(void);
bool isEmpty(void);
void clear(void);
Deep Thinking and Discuss
Why the class queue initialize the storage’s size to MAXSIZE+1 rather than MAXSIZE?
What’s the disadvantages of the stack and queue in this question?
More
If you have any ideas or thoughts about C++ D&A. Just leave a message below or send me a mail.
Next we will have some dynamic storage practice. I recommend you to review so basic knowledge about pointer and memory allocation about c that you learned it last semester.
Hint:
Queue 中初始化的数组的大小为 maxsize+1,真正存储的最大容量仍然为MAX_SIZE. 也就是 capability == MAX_SIZE. 思考这样初始化的用意(循环队列)
main.cpp
#include <stdio.h>#include "stack.hpp"#include "queue.hpp"void test_stack(void) { if (true) { stack st; for (int i = 0; i < 5; i++) { st.push(i); } st.isEmpty() ? printf("empty\n") : printf("not empty\n"); st.isFull() ? printf("full\n") : printf("not full\n"); for (int i = 0; i < 5; i++) { printf("%d ", st.peek()); st.pop(); } printf("\n"); st.isEmpty() ? printf("empty\n") : printf("not empty\n"); st.isFull() ? printf("full\n") : printf("not full\n"); } printf("\n"); if (true) { stack st; for (int i = 0; i < 11; i++) { st.push(i); } st.isEmpty() ? printf("empty\n") : printf("not empty\n"); st.isFull() ? printf("full\n") : printf("not full\n"); for (int i = 0; i < 10; i++) { printf("%d ", st.peek()); st.pop(); } printf("\n"); st.isEmpty() ? printf("empty\n") : printf("not empty\n"); st.isFull() ? printf("full\n") : printf("not full\n"); } printf("\n"); if (true) { stack st; for (int i = 0; i < 20; i++) { st.push(i); } st.clear(); st.isEmpty() ? printf("empty\n") : printf("not empty\n"); st.isFull() ? printf("full\n") : printf("not full\n"); for (int i = 0; i < 11; i++) { st.push(i); } st.isEmpty() ? printf("empty\n") : printf("not empty\n"); st.isFull() ? printf("full\n") : printf("not full\n"); for (int i = 0; i < 10; i++) { printf("%d ", st.peek()); st.pop(); } printf("\n"); st.isEmpty() ? printf("empty\n") : printf("not empty\n"); st.isFull() ? printf("full\n") : printf("not full\n"); }}void test_queue(void) { if (true) { queue qu; for (int i = 0; i < 5; i++) { qu.push(i); } qu.isEmpty() ? printf("empty\n") : printf("not empty\n"); qu.isFull() ? printf("full\n") : printf("not full\n"); for (int i = 0; i < 5; i++) { printf("(%d,%d) ", qu.front(), qu.back()); qu.pop(); } printf("\n"); qu.isEmpty() ? printf("empty\n") : printf("not empty\n"); qu.isFull() ? printf("full\n") : printf("not full\n"); } printf("\n"); if (true) { queue qu; for (int i = 0; i < 11; i++) { qu.push(i); } qu.isEmpty() ? printf("empty\n") : printf("not empty\n"); qu.isFull() ? printf("full\n") : printf("not full\n"); for (int i = 0; i < 11; i++) { printf("(%d,%d) ", qu.front(), qu.back()); qu.pop(); } printf("\n"); qu.isEmpty() ? printf("empty\n") : printf("not empty\n"); qu.isFull() ? printf("full\n") : printf("not full\n"); } printf("\n"); if (true) { queue qu; for (int i = 0; i < 11; i++) { qu.push(i); } qu.isEmpty() ? printf("empty\n") : printf("not empty\n"); qu.isFull() ? printf("full\n") : printf("not full\n"); qu.clear(); qu.isEmpty() ? printf("empty\n") : printf("not empty\n"); qu.isFull() ? printf("full\n") : printf("not full\n"); for (int i = 0; i < 5; i++) { qu.push(i); } for (int i = 0; i < 5; i++) { printf("(%d,%d) ", qu.front(), qu.back()); qu.pop(); } printf("\n"); qu.isEmpty() ? printf("empty\n") : printf("not empty\n"); qu.isFull() ? printf("full\n") : printf("not full\n"); } printf("\n");}int main() { test_stack(); test_queue(); return 0;}
stack.hpp
#ifndef STACK_H_#define STACK_H_#define MAX_SIZE 10class stack {private: int * storage; int max_size; int top;public: stack() { this->storage = new int[MAX_SIZE](); this->max_size = MAX_SIZE; this->top = -1; } ~stack() { delete []this->storage; } void push(int); void pop(void); int peek(void); bool isEmpty(void); bool isFull(void); void clear(void);};#endif
queue.hpp
#ifndef QUEUE_H_#define QUEUE_H_#define MAX_SIZE 10class queue {private: int * storage; int max_size; int head; int rear;public: queue() { // notice the capability is still MAX_SIZE this->storage = new int[MAX_SIZE+1](); this->max_size = MAX_SIZE+1; this->head = 0; this->rear = 0; } ~queue() { delete []storage; } void push(int); void pop(void); int front(void); int back(void); bool isFull(void); bool isEmpty(void); void clear(void);};#endif
读题
my answer
stack.hpp
#include <iostream>#include "stack.hpp"// you can not push an element when the storage is fullvoid stack::push(int x) { if (isFull() == false) { top++; storage[top] = x; }}// you can not pop an element when the storage is emptyvoid stack::pop(void) { if (isEmpty() == false) { top--; }}// when the stack is empty, you should return 0int stack::peek(void) { if (isEmpty()) { return 0; } else { return top; }}bool stack::isEmpty(void) { if (top == -1) { return true; } else { return false; }}bool stack::isFull(void) { if (top == MAX_SIZE - 1) { return true; } else { return false; }}void stack::clear(void) { top = -1;}
queue.hpp
#include <iostream>#include "queue.hpp"// you can not push an element when the storage is fullvoid queue::push(int x) { if (isFull() == false) { *(storage + rear) = x; rear++; }}// you can not pop an element when the storage is emptyvoid queue::pop(void) { if (isEmpty() == false) { head++; if (head == MAX_SIZE) { head = 0; rear = 0; } }}// when the stack is empty, you should return 0int queue::front(void) { if (isEmpty()) { return 0; } else { return head; }}// when the stack is empty, you should return 0int queue::back(void) { if (isEmpty()) { return 0; } else { return rear - 1; }}bool queue::isFull(void) { if (rear == MAX_SIZE) { return true; } else { return false; }}bool queue::isEmpty(void) { if (rear == head) { return true; } else { return false; }}void queue::clear(void) { head = 0; rear = 0;}
the standard answer
stack.hpp
#include "stack.hpp"#include <string.h>void stack::push(int data) { if (this->isFull()) { return; } this->top++; this->storage[top] = data;}void stack::pop(void) { if (this->isEmpty()) { return; } this->top--;}int stack::peek(void) { if (this->isEmpty()) { return 0; } return storage[top];}bool stack::isEmpty(void) { return (this->top == -1); }bool stack::isFull(void) { return (this->top + 1 >= this->max_size); }void stack::clear(void) { this->top = -1; }
queue.hpp
#include "queue.hpp"void queue::push(int data) { if (this->isFull()) { return; } this->storage[rear] = data; this->rear = (this->rear + 1) % this->max_size;}void queue::pop(void) { if (this->isEmpty()) { return; } this->head = (this->head + 1) % this->max_size;}int queue::front(void) { if (this->isEmpty()) { return 0; } return this->storage[head];}int queue::back(void) { if (this->isEmpty()) { return 0; } return this->storage[(rear-1+max_size) % max_size];}bool queue::isFull(void) { return ((this->rear + 1) % this->max_size) == this->head;}bool queue::isEmpty(void) { return this->head == this->rear; }void queue::clear(void) { this->rear = this->head; }
反馈
题目要求队列是循环队列,我的答案并没有完全实现,算是钻了测试的空子吧.
要学习答案的做法.
- stack and queue
- Queue and Stack
- Queue and stack
- Stack and Queue
- List, Queue and Stack
- Stack and Queue
- usage of stack and queue
- queue by one stack、two stack and three stack
- Stack and Queue Base on LinkedList
- 栈与队列(queue and stack)
- Selection Sort with stack and queue
- Insertion Sort with queue and stack
- Stack and Queue 栈和队列
- My understand of stack and queue
- stack-structured algorithm and queue-structured algorithm
- stack,queue
- Queue\Stack
- queue stack
- Python 封装读取CSV配置文件
- String, StringBuffer, StringBuilder 三者之间的区别是什么?
- 简单解析 Android N 新特性
- 全景视频拼接(三)--并查集法及源码分析
- service xxx does not support chkconfig
- Stack and Queue
- 使用 lipo -create 合并.a文件
- spring jar下载
- 解决Ubuntu 16.04中gvfsd-smb-browse占用CPU 100%的问题
- Qt 上传下载
- activiti初探
- 产品画流程图软件
- mybatis 学习
- 艺龙网分销渠道代理层代码重构方案