链栈,共享栈,循环队列和链队列的基本实现。
链栈:
#pragma once
//定义节点
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 (top == NULL) ? true : false;; }//判断是否为空
private:
struct Node<T>* top;
};
template<class T>
LinkStack<T>::~LinkStack() {//析构函数
while (top) {
struct Node<T>* p = top;//工作指针
top = top->next;
delete[] p;
}
}
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;
}
共享栈:
#pragma once
//共享栈模板类
const int STACKSIZE = 1024;//共享栈的最大空间
template<class T>
class SharedStack{
public:
SharedStack();
void Push1(T x);//栈1入栈
void Push2(T x);//栈2入栈
T Pop1();//栈1出栈
T Pop2();//栈2出栈
T GetTop1();//查找栈顶元素1
T GetTop2();//查找栈顶元素2
bool Empty1();//判断栈1是否为空
bool Empty2();//判断栈2是否为空
private:
int top1;//栈1指针
int top2;//栈2指针
T data[STACKSIZE];//定义栈的大小
};
template<class T>
SharedStack<T>::SharedStack() {//初始化空栈
top1 = -1;
top2 = STACKSIZE;
}
template<class T>
void SharedStack<T>::Push1(T x) {//栈1入栈
if (top1 + 1 >= top2)throw"栈1上溢";
top1++;
data[top1] = x;
}
template<class T>
void SharedStack<T>::Push2(T x) {//栈2入栈
if (top2 <= top1 + 1)throw"栈2上溢";
top2--;
data[top2] = x;
}
template<class T>
T SharedStack<T>::Pop1() {//栈1出栈
if (Empty1())throw"栈1下溢";
top1--;
return data[top1 + 1];
}
template<class T>
T SharedStack<T>::Pop2() {//栈2出栈
if (Empty2())throw"栈2下溢";
top2++;
return data[top2 - 1];
}
template<class T>
T SharedStack<T>::GetTop1() {//get栈1顶端元素
if (top1 != -1)
return data[top];
else throw"栈1空";
}
template<class T>
T SharedStack<T>::GetTop2() {//get栈2顶端元素
if (top2 != STACKSIZE)
return data[top2];
else throw"栈2空";
}
template<class T>
bool SharedStack<T>::Empty1() {//判断栈1是否为空
if (top1 == -1)
return true;
return false;
}
template<class T>
bool SharedStack<T>::Empty2() {//判断栈2是否为空
if (top2 == STACKSIZE)
return true;
return false;
}
循环队列:
#pragma once
const int QUEUESIZE = 1000;
//循环队列模板类
template<class T>
class LoopQueue {
public:
LoopQueue() { front = rear = 0; }//构造函数
void EnterQueue(T x);//入队
T OutQueue();//出队
T GetFront();
int GetLength();
bool Empty() { return front == rear ? true : false; }
private:
int front;
int rear;
T data[QUEUESIZE];
};
template<class T>
void LoopQueue<T>::EnterQueue(T x){//入队
if ((rear + 1) % QUEUESIZE == front)throw"队满";
rear = (rear + 1) % QUEUESIZE;
data[rear] = x;
}
template <class T>
T LoopQueue<T>::OutQueue() {//出队
if (rear == front)throw"队空";
front = (front + 1) % QUEUESIZE;
return data[front];
}
template<class T>
T LoopQueue<T>::GetFront() {//查找队头元素
if (rear == front)throw"队空";
return data[(front + 1) % QUEUESIZE];
}
template<class T>
int LoopQueue<T>::GetLength() {
return (QUEUESIZE + rear - front) % QUEUESIZE;
}
链队列:
#pragma once
//链队列目标 类
template<class T>
class LinkQueue {
public:
LinkQueue() {//构造函数
front = rear = new Node<T>;//初始化指针
front->next = NULL;
}
~LinkQueue();
void EnterQueue(T x);
T OutQueue();
T GetFront();
bool Empty() { return front == rear ? true : false; }
private:
struct Node<T>* front;
struct Node<T>* rear;
};
template<class T>
void LinkQueue<T>::EnterQueue(T x) {//入队
rear->next = new Node<T>;
rear = rear->next;
rear->data = x;
rear->next = NULL;
}
template<class T>
T LinkQueue<T>::OutQueue() {//出队
struct Node<T> * p = front->next;//保存队头元素指针
if (!p)throw"队空";
front->next = p->next;//队头元素出队
T x=p->data;//保存队头元素数据
delete[] p;//释放队头元素
return x;//返回出队元素
}
template<class T>
T LinkQueue<T>::GetFront() {//查找队头元素
if (!front->next)throw"队空";
return front->next->data;
}
template<class T>
LinkQueue<T>::~LinkQueue() {//析构函数
while (front) {
rear = front->next;
delete[] front;
front = rear;
}
}
测试:
// 实验1-栈和队列.cpp: 定义控制台应用程序的入口点。
//测试main()函数
#include "stdafx.h"
#include<iostream>
#include"SharedStack.h"
#include"LinkStack.h"
#include"LoopQueue.h"
#include"LinkQueue.h"
using namespace std;
int main()
{
//测试共享栈
cout << "测试共享栈:" << endl;
cout << "请输入两个数(int)分别入栈1和栈2:" << endl;
int test1, test2;
cin >> test1 >> test2;
SharedStack<int> s1;
s1.Push1(test1);//栈1入栈
s1.Push2(test2);//栈2入栈
cout << "出栈结果:"<<s1.Pop1() << " " << s1.Pop2()<<endl<<endl;//栈1 栈2 出栈
//测试链栈
cout << "测试链栈:" << endl;
cout << "输入两个入栈数据(int):" << endl;
int test3, test4;
cin >> test3 >> test4;
LinkStack<int> s2;
s2.Push(test3);
s2.Push(test4);
cout << "栈顶元素为:" << s2.GetTop() << endl;
cout << "出栈结果:" << s2.Pop() << " " << s2.Pop() << endl<<endl;//出栈
//测试循环队列
cout << "测试循环队列:" << endl;
cout << "输入两个int数据入队:" << endl;
int test5, test6;
cin >> test5 >> test6;
LoopQueue<int> s3;
s3.EnterQueue(test5);
s3.EnterQueue(test6);
cout << "测试队列长度:" << s3.GetLength() << endl;
cout << "测试队头元素:" << s3.GetFront() << endl;
cout << "出栈结果:" << s3.OutQueue() << " " << s3.OutQueue() << endl << endl;
//测试链队列
cout << "测试链队列:" << endl;
cout << "输入两个int数据入队:" << endl;
int test7, test8;
cin >> test7 >> test8;
LinkQueue<int> s4;
s4.EnterQueue(test7);
s4.EnterQueue(test8);
cout << "测试队头元素:" << s4.GetFront() << endl;
cout << "出队结果为:" << s4.OutQueue() << " " << s4.OutQueue() << endl;
return 0;
}