根据栈和队列的抽象数据类型的定义,按要求实现一个栈或一个队列。
要求:
1、实现一个共享栈
2、实现一个链栈
3、实现一个循环队列
4、实现一个链队列
编写测试main()函数测试线性表的正确性。
参考代码:
#include<iostream> #include"标头.h" using namespace std; int main() { //测试1:共享栈 cout << "测试共享栈" << endl; bothStack<int>text1; try { text1.push(99, 2); //将栈堆满 text1.push(210, 2); text1.push(250, 1); text1.push(250, 1); text1.push(50, 1); cout << "栈为:" << endl; text1.print(); cout << endl; cout << "栈1顶元素为:" << endl; int a = text1.gettop(1); cout << a << endl; cout << "删除栈1顶元素并在栈1顶加入新元素666:" << endl; int b = text1.pop(1); text1.push(666, 1); text1.print(); } catch(char* s) { cout << s << endl; } cout << endl; cout << endl; cout << endl; //测试2:链栈 cout << "测试链栈" << endl; LinkStack<char>text2; try { char z = 'z', y = 'y', x = 'x'; text2.Push(z); text2.Push(y); text2.Push(x); cout << "获取栈顶元素:" << endl; char c = text2.GetTop(); cout << c << endl; cout << "删除栈顶元素" << endl; text2.Pop(); cout << "此时栈顶元素为:" << endl; char d = text2.GetTop(); cout << d << endl; } catch(char*h) { cout << h << endl; } cout << endl; cout << endl; cout << endl; //测试3:循环队列 cout << "测试循环队列" << endl; CircleQueue<int>text3; try { text3.EnQueue(1); text3.EnQueue(2); text3.EnQueue(3); text3.EnQueue(4); text3.EnQueue(5); cout << "获取队首元素:" << endl; int e = text3.GetFront(); cout << e << endl; cout << "队首出队" << endl; text3.DeQueue(); cout << "获取队首元素:" << endl; int f = text3.GetFront(); cout << f << endl; cout << "获取队列长度:" << endl; int g = text3.GetLength(); cout << g << endl; } catch (char*h) { cout << h << endl; } cout << endl; cout << endl; cout << endl; //测试4:链队列 cout << "测试链队列" << endl; LinkQueue<char>text4; char h='h', i='i', j='j', k='k', l='l'; try { text4.EnQueue(h); text4.EnQueue(i); text4.EnQueue(j); text4.EnQueue(k); text4.EnQueue(l); cout << "获取队首元素:" << endl; char v = text4.GetFront(); cout << v << endl; cout << "队首出队" << endl; text4.DeQueue(); cout << "获取队首元素:" << endl; char o = text4.GetFront(); cout << o << endl; } catch (char*h) { cout << h << endl; } system("pause"); return 0; }
const int stacksize = 5; template <class T> class bothStack //共享栈 { public: bothStack() { top1 = -1; top2 = stacksize; } int empty(); T gettop(int a); void push(T x, int a); T pop(int a); void print(); private: T data[stacksize]; int top1, top2; }; template <class T> void bothStack<T>::print() //共享栈 打印栈 { for (int i = 0; i < stacksize; i++) cout << data[i] << " "; } template <class T> int bothStack<T>::empty() //共享栈 判断栈是否为空 { if (top2 - top1== 1) return 0; else return 1; } template <class T> T bothStack<T>::gettop(int a) //共享栈 获取栈顶元素 { if (a == 1) return data[top1]; if(a=2) return data[top2]; } template <class T> void bothStack<T>::push(T x, int a) //共享栈 入栈 { if (top2 - top1 == 1) throw"栈已满"; if (a == 1) { top1++; data[top1] = x; } if(a==2) { top2--; data[top2] = x; } } template <class T> T bothStack<T>::pop(int a) //共享栈 出栈 { if (empty()) throw"栈为空"; if (a==1) { if (top1 == -1) throw"下溢"; else { top1--; return data[top1 + 1]; } } if (a==2) { if (top2 == stacksize) throw"上溢"; else { top2++; return data[top2 - 1]; } } } template <class T> struct Node { T data; struct Node <T> * next; }; template <class T> class LinkStack //链栈 { public: LinkStack() { top = NULL; } ~LinkStack(); void Push(T x); T Pop(); T GetTop(); bool Empty() { return (NULL == top) ? true :false; //空为1,不空为0 } private: struct Node <T> * top; }; template <class T> void LinkStack<T>::Push(T x) //链栈 入栈 { struct Node <T> * p = new Node<T>; p->data = x; p->next = top; top = p; } template <class T> T LinkStack<T>::Pop() // 链栈 出栈 { if (Empty()) throw "下溢"; T x = top->data; struct Node <T> * p = top; top = top->next; delete p; return x; } template <class T> T LinkStack<T>::GetTop() //链栈 获取栈顶元素 { if (Empty()) throw"栈为空"; return top->data; } template <class T> LinkStack<T>::~LinkStack() //链栈 析构函数 { while (top) { Node<T>*p = top; top = top->next; delete p; } } const int QueueSize = 10; template <class T> class CircleQueue //循环队列 { public: CircleQueue() { front = rear = 0; } void EnQueue(T x); T DeQueue(); T GetFront(); int GetLength(); bool Empty() { return front = = rear ? true : false; } private: T data[QueueSize]; int front; int rear; }; template <class T> void CircleQueue<T>::EnQueue(T x) //循环队列 入队 { if ((rear + 1) % QueueSize == front) //(rear + 1) % QueueSize指向下一个元素 throw "队列已满"; rear = (rear + 1) % QueueSize; data[rear] = x; } template <class T> T CircleQueue<T>::DeQueue() // 循环队列 出队 { if (rear == front) throw "队列已空"; front = (front + 1) % QueueSize; return data[front]; } template <class T> T CircleQueue<T>::GetFront() // 循环队列 获取队首元素 { if (rear == front) throw "队列空"; return data[(front + 1) % QueueSize]; } template <class T> int CircleQueue<T>::GetLength() // 循环队列 获取队列长度 { return (rear - front + QueueSize) % QueueSize; } template <class T> class LinkQueue //链队列 { public: LinkQueue() { front = new Node <T>; rear = front; front->next = NULL; } ~LinkQueue(); void EnQueue(T x); T DeQueue(); T GetFront(); bool Empty() { return front == rear ? true : false; } private: Node <T> * front; Node <T> * rear; }; template <class T> void LinkQueue<T>::EnQueue(T x) //链队列 入队 { rear->next = new Node <T>; rear = rear->next; rear->data = x; rear->next = NULL; } template <class T> T LinkQueue<T>::DeQueue() //链队列 出队 { Node <T> * p = front->next; if (!p) throw "队列已空"; front->next = p->next; T x = p->data; delete p; if (!(front->next)) //如果队列变为空,修改队尾指针 rear = front; return x; } template <class T> LinkQueue<T>:: ~LinkQueue() //析构函数 { while (front != NULL) { rear = front->next; delete front; front = rear; } } template <class T> //链队列 获取队首元素 T LinkQueue<T>::GetFront() { return front->next->data; }