【大一下】栈和队列实验代码总结

目录

括号匹配问题

指针做法

STL做法

顺序共用栈问题

类与对象做法

指针做法

迷宫

队列

轮船问题

指针做法

STL做法

单指针队列

杨辉三角形

栈模拟队列

 

括号匹配问题

指针做法

//Author:NathanQian
#include<iostream>
#include<stack>
#include<string>
using namespace std;
const int N = 1e5 + 10;
struct Stack
{
	char* base;
	int top;
	int stacksize;
};
void Initstack(Stack &st)
{
	st.base = new char[N];
	st.top = -1;
	st.stacksize = N;
}
void push(Stack& st,char e)
{
	st.top++;
	st.base[st.top] = e;
}
void pop(Stack& st)
{
	if (st.top == -1)
		return;
	else
		st.top--;
}
char top(Stack& st)
{
	if (st.top != -1)
	return st.base[st.top];
}
bool empty(Stack& st)
{
	if(st.top == -1)return true;
	else return false;
}
int main()
{
	cout << "请输入表达式:" << endl;
	Stack st;
	Initstack(st);
	string s;
	cin >> s;
	for (int i = 0; i < s.size(); i++)
	{
		auto c = s[i];
		if (c == '('|| c == '[')push(st,c);
		else if (c == ')')
		{
			if (!empty(st)&&top(st) == '(')
			{
				pop(st);
				continue;
			}
			else
			{
				puts("不匹配");
				return 0;
			}
		}
		else if(!empty(st)&&c==']')
		{
			if (top(st) == '[')
			{
				pop(st);
				continue;
			}
			else
			{
				puts("不匹配");
				return 0;
			}
		}
	}
	if (empty(st))
		puts("匹配!");
	else
		puts("不匹配");

}

STL做法

//Author:NathanQian
#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main()
{
	cout << "请输入表达式:" << endl;
	stack<char>st;
	string s;
	cin >> s;
	for (int i = 0; i < s.size(); i++)
	{
		auto c = s[i];
		if (c == '(' || c == '[')st.push(c);
		else if (c == ')')
		{
			if (!st.empty() && st.top() == '(')
			{
				st.pop();
				continue;
			}
			else
			{
				puts("不匹配");
				return 0;
			}
		}
		else if (!st.empty() && c == ']')
		{
			if (st.top() == '[')
			{
				st.pop();
				continue;
			}
			else
			{
				puts("不匹配");
				return 0;
			}
		}
	}
	if (st.empty())
		puts("匹配!");
	else
		puts("不匹配");

}

顺序共用栈问题

类与对象做法

#include<iostream>
#include<string>
using namespace std;
int n;class mergeStack
{
private:
	int top1;
	int top2;
	int stacksize;
	int* base = new int[stacksize];
public:
	mergeStack(int n)
	{
		stacksize = n;
		top1 = -1;
		top2 = stacksize;
	}
	void push1()
	{
		if (top1 + 1 == top2)
		{
			cout << "栈满,无法入栈" << endl;
			return;
		}
		top1++;
		cout << "成功入栈,栈顶指针增1" << endl;
		return;
	}
	void push2()
	{
		if (top2 -1 == top1)
		cout << "栈满,无法入栈" << endl;
		else
		{
			top2--;
			cout << "成功入栈,栈顶指针减1" << endl;
		}
		return;
	}
	void pop1()
	{
		if (top1 == -1)
			cout << "栈空,无法出栈" << endl;
		else
		{
			top1--;
			cout << "成功出栈,栈顶指针减1" << endl;
		}
	}
	void pop2()
	{
		if (top2 == stacksize)
			cout << "栈空,无法出栈" << endl;
		else
		{
			top2++;
			cout << "成功出栈,栈顶指针增1" << endl;
		}
	}

};
int main()
{
	int n;
	puts("请输入共用栈的容量:");
	cin >> n;
	mergeStack St(n);
	int type;
	string op;
	puts("请输入栈序号(1,2)和对应操作(in,out):");
	while (cin >> type >> op)
	{
		if (type == 1)
		{
			if (op == "in")
				St.push1();
			else if (op == "out")
				St.pop1();
			else
				cout << "op操作输入非法!" << endl;
		}
		else if (type == 2)
		{
			if (op == "in")
				St.push2();
			else if (op == "out")
				St.pop2();
			else
				cout << "op操作输入非法!" << endl;
		}
		else
			cout << "输入序号非法!" << endl;
	}
	return 0;

}

指针做法

#include<iostream>
#include<string>
using namespace std;
int n;class mergeStack
{
private:
	int top1;
	int top2;
	int stacksize;
	int* base = new int[stacksize];
public:
	mergeStack(int n)
	{
		stacksize = n;
		top1 = -1;
		top2 = stacksize;
	}
	void push1()
	{
		if (top1 + 1 == top2)
		{
			cout << "栈满,无法入栈" << endl;
			return;
		}
		top1++;
		cout << "成功入栈,栈顶指针增1" << endl;
		return;
	}
	void push2()
	{
		if (top2 -1 == top1)
		cout << "栈满,无法入栈" << endl;
		else
		{
			top2--;
			cout << "成功入栈,栈顶指针减1" << endl;
		}
		return;
	}
	void pop1()
	{
		if (top1 == -1)
			cout << "栈空,无法出栈" << endl;
		else
		{
			top1--;
			cout << "成功出栈,栈顶指针减1" << endl;
		}
	}
	void pop2()
	{
		if (top2 == stacksize)
			cout << "栈空,无法出栈" << endl;
		else
		{
			top2++;
			cout << "成功出栈,栈顶指针增1" << endl;
		}
	}

};
int main()
{
	int n;
	puts("请输入共用栈的容量:");
	cin >> n;
	mergeStack St(n);
	int type;
	string op;
	puts("请输入栈序号(1,2)和对应操作(in,out):");
	while (cin >> type >> op)
	{
		if (type == 1)
		{
			if (op == "in")
				St.push1();
			else if (op == "out")
				St.pop1();
			else
				cout << "op操作输入非法!" << endl;
		}
		else if (type == 2)
		{
			if (op == "in")
				St.push2();
			else if (op == "out")
				St.pop2();
			else
				cout << "op操作输入非法!" << endl;
		}
		else
			cout << "输入序号非法!" << endl;
	}
	return 0;

}

迷宫

这里实际上最终会考察BFS和DFS就用vector来模拟下吧!

//Author:NathanQian
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N = 550;
struct co
{
	int x;
	int y;
};
char g[N][N];
bool st[N][N];
int d[N][N];
int n, m;
vector<co>ans1;
vector<co>ans;
int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
void dfs(int x,int y)
{
	if (x == n && y == m)
	{
		for (int i = 0; i < ans.size(); i++)
			cout << "(" << ans[i].x << "," << ans[i].y << ")"<<' ';
		cout << endl;
	}
	else
	{
		for (int i = 0; i < 4; i++)
		{
			int x0 = x + dx[i], y0 = y + dy[i];
			if (!st[x0][y0] && g[x0][y0]=='0')
			{
				ans.push_back({ x0,y0 });
				st[x0][y0] = true;
				dfs(x0, y0);
				st[x0][y0] = false;
				ans.pop_back();
			}
		}
	}
}
void bfs()
{
	queue<co>q;
	memset(d, -1, sizeof d);
	q.push({ 1,1 });
	ans1.push_back({ 1,1 });
	d[1][1] = 0;
	int cnt = 0;
	while (q.size())
	{
		auto p = q.front(); q.pop();
		for (int i = 0; i < 4; i++)
		{
			int x = p.x + dx[i], y = p.y + dy[i];
			if (d[x][y]==-1 && g[x][y] == '0')
			{
				d[x][y] = d[p.x][p.y] + 1;
				q.push({ x,y });
				if (d[x][y] > cnt)
				{
					cnt = d[x][y];
					ans1.push_back({ x,y });
				}
			}
			if (x == n && y == m)
			{
				for (int i = 0; i < ans1.size(); i++)
					cout << "(" << ans1[i].x << "," << ans1[i].y << ")" << ' ';
				cout << endl;
				return;
			}
		}
	}
}
int main()
{
	puts("请输入地图的长度和宽度:");
	cin >> n >> m;
	memset(g, -1, sizeof g);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> g[i][j];
	puts("1.DFS求所有可能的路径");
	ans.push_back({ 1,1 });
	st[1][1] = 1;
	dfs(1, 1);
              puts("2.BFS求最短路径");
	bfs();
	return 0;
}

队列

轮船问题

指针做法

//Author:NATHANQIAN
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
const int N = 1e5 + 10;
int cnt = 1;
struct Car
{
	char type;
	int id;
};
struct Sqqueue
{
	Car* base;
	int front;
	int rear;
	int queuesize;
};
struct Node
{
	Car* p;
	int id;
	Node* next;
};

//轮船的存储结构
void InitNode(Node *&p)
{
	Node* s = new Node;
	s->p = new Car[10];
	s->id = cnt++;
	p = s;
}//轮船结点的初始化

void push_back(Node*& ship)
{
	Node* s;
	InitNode(s);
	s->next = ship->next;
	ship->next = s;
	ship = s;
}//将这个轮船放入链表


//队列的初始化和操作
void Initqueue(Sqqueue &Q)
{
	Q.base = new Car[N];
	Q.front = Q.rear = 0;
	Q.queuesize = N;
}
void push(Sqqueue& Q, Car e)
{
	Q.base[Q.rear] = e;
	Q.rear++;
}
void pop(Sqqueue& Q)
{
	Q.front++;
}
Car front(Sqqueue& Q)
{
	return Q.base[Q.front];
}
bool empty(Sqqueue& Q)
{
	if (Q.rear == Q.front)
		return true;
	else
		return false;

}

int main()
{
	Sqqueue car1, car2;
	Initqueue(car1), Initqueue(car2);
	int n, m, cnt1 = 0, cnt2 = 0, cnt = 0;
	Node* L;//头结点方便后期遍历整个链表
	L = new Node;
	Node* ship;
	InitNode(ship);
	ship->next = NULL;
	L->next = ship;
	int j = 0;
	puts("请依次输入进入队列的客车和货车的数量");
		while (cin >> n >> m && (n || m))
		{
			system("cls");
			for (int i = 1; i <= n; i++)
				push(car1,{ 'K',i + cnt1});
			cnt1 += n;
			for (int j = 1; j <= m; j++)
				push(car2, { 'H',j + cnt2 });
			cnt2 += m;
			while (!empty(car1) || !empty(car2))
			{
				if ((cnt >= 4 && !empty(car2)) || empty(car1))
				{
					auto now = front(car2); pop(car2);
					ship->p[j++]=now;
					cnt = 0;
				}
				else
				{
					auto now = front(car1); pop(car1);
					ship->p[j++]=now;
					cnt++;
				}
				if (j == 10)
				{
					push_back(ship);
					j = 0;
				}
			}
			int st;
			puts("是否查看当前船的载车情况 1.查看 2.继续输入");
			cin >> st;
			if (st == 1)
			{
				for (auto it = L->next; it != NULL; it = it->next)
				{
					
					for (int j = 0; j < 10; j++)
						if (it->p[j].type =='H'||it->p[j].type=='K')
						cout << it->p[j].type << it->p[j].id << ' ';
					cout << "ship" << it->id << endl;
				}
				
			}
			cout << endl;

			puts("请依次输入进入队列的客车和货车的数量,输入 0 0 结束装载");
		}
		puts("运载结束");
		





}

STL做法

#include<iostream>
#include<queue>
#include<vector>

using namespace std;
struct Car
{
	char type;
	int id;
};
vector<vector<Car>>ship;
int main()
{
	queue<Car>car1, car2;
	int n, m, cnt1 = 0, cnt2 = 0, cnt = 0;
	ship.push_back(vector<Car>(10));
	int i = 0, j = 0;
	puts("请依次输入进入队列的客车和货车的数量");
		while (cin >> n >> m && (n || m))
		{
			system("cls");
			for (int i = 1; i <= n; i++)
				car1.push({ 'K',i + cnt1});
			cnt1 += n;
			for (int j = 1; j <= m; j++)
				car2.push({ 'H',j + cnt2});
			cnt2 += m;
			while (!car1.empty() || !car2.empty())
			{
				if ((cnt >= 4 && !car2.empty()) || car1.empty())
				{
					auto now = car2.front(); car2.pop();
					ship[i][j++]=now;
					cnt = 0;
				}
				else
				{
					auto now = car1.front(); car1.pop();
					ship[i][j++]=now;
					cnt++;
				}
				if (j == 10)
				{
					ship.push_back(vector<Car>(10));
					i++;
					j = 0;
				}
			}
			int st;
			puts("是否查看当前船的载车情况 1.查看 2.继续输入");
			cin >> st;
			if (st == 1)
			{
				for (auto iter = ship.begin(); iter != ship.end(); iter++)
				{
					auto tmp = *iter;
					for (auto it = tmp.begin(); it != tmp.end(); it++)
						if (it->id)
						{
							cout << it->type << it->id << ' ';
						}
					cout << "ship" << iter - ship.begin()+1<< endl;
				}
			}
			puts("请依次输入进入队列的客车和货车的数量,输入 0 0 结束装载");
		}
		puts("运载结束");
		





}

单指针队列

这个就是一个手写队列

#include<iostream>
using namespace std;
struct queue
{
	int* base;
	int front;
	int rear;
	int queuesize;
};
void Initqueue(queue& Q, int m)
{
	Q.base = new int[m];
	Q.front = Q.rear = 0;
	Q.queuesize = m + 1;
}
void push(queue& Q, int e)
{
	if ((Q.rear +1)% Q.queuesize == Q.front)
	{
		cout << "队满,无法入队啦!" << endl;
		return;
	}
	Q.base[Q.rear] = e;
	Q.rear = (Q.rear + 1) % Q.queuesize;
	cout << "入队成功!" << endl;
}
void pop(queue& Q)
{
	if (Q.front == Q.rear)
	{
		cout << "队空,没东西出队啦!" << endl;
		return;
	}
	Q.front = (Q.front + 1) % Q.queuesize;
	cout << "出队成功!" << endl;
}
bool empty(queue& Q)
{
	return Q.rear == Q.front;
}

void show(queue& Q)
{
	if (Q.front == Q.rear)
	{
		cout << "队空,没东西看啦!" << endl;
		return;
	}

	for (int i = Q.front; i < Q.rear; i++)
		cout << Q.base[i] << ' ';
	cout << endl;
}
int main()
{
	cout << "请输入队长:" << endl;
	queue q; int m; cin >> m;
	cout << "功能表:\n1.测队空\n2.入队\n3.出队\n4.显示队长\n5.显示队元素" << endl;
	int op;
	Initqueue(q,m);
	while (cin >> op)
	{
		switch(op)
		{
		case 1:
			if (empty(q))
				cout << "队空" << endl;
			else
				cout << "队不空" << endl;
			break;
		case 2:
			cout << "请输入入队个数和元素值" << endl;
			int i; cin >> i;
			while (i--)
			{
				int x; cin >> x;
				push(q, x);
			}
			break;
		case 3:
			pop(q);
			break;
		case 4:
			if (q.rear > q.front)
				cout << q.rear - q.front << endl;
			else
				cout << q.queuesize - (q.front - q.rear);
			break;
		case 5:
			show(q);
		}
	}
	return 0;
}

杨辉三角形

队列来模拟

用作一个新的思维角度去考虑这个三角形问题挺不错

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	queue<int>q;
	int rmax; cin >> rmax;
	int cnt = 1;
	int a[4] = { 0,1,0,0 };
	for (int i = 0; i < 4; i++)
		q.push(a[i]);
	while (cnt <= rmax)
	{
		for (int i = 1; i <= cnt + 2; i++)
		{
			auto p = q.front(); q.pop();
			q.push(p + q.front());
			cout << p << ' ';
		}
		cout << endl;
		q.push(0);
		cnt++;
	}
}

栈模拟队列

#include<iostream>
using namespace std;
const int N = 4;
struct Stack
{
	int* base;
	int top;
	int stacksize;
};
void InitStack(Stack& st)
{
	st.stacksize = N;
	st.base = new int[N];
	st.top = -1;
}
int pop(Stack& st)
{
	return st.base[st.top--];
}
void push(Stack& st,int e)
{
	st.top++;
	st.base[st.top] = e;

}
bool full(Stack& st)
{
	if (st.top + 1 == st.stacksize)
		return true;
	else
		return false;
}
bool empty(Stack& st)
{
	if (st.top == -1)
		return true;
	else
		return false;
}
void show(Stack &st)
{
	if (!empty(st))
		for (int i = 0; i <= st.top; i++)
			cout << st.base[st.top] << ' ';

}
int main()
{
	Stack s1, s2;
	InitStack(s1), InitStack(s2);
	int op;
	puts("请输入操作:\n1.测队空\n2.入队\n3.显示队列元素\n4.出队");
	while (cin >> op)
	{
		if (op == 1)
		{
			if (empty(s1) && empty(s2))
				cout << "队空!" << endl;
			else
				cout << "队不空哦!" << endl;
		}
		else if (op == 2)
		{
			puts("请输入元素:");
			int e; cin >> e;
			if (!full(s1))
			{
				push(s1, e);
				cout << "入队成功!" << endl;
			}
			else if (full(s1) && empty(s2))
			{
				while (!empty(s1))
				{
					int m = pop(s1);
					push(s2, m);
				}
				push(s1, e);
				cout << "入队成功!" << endl;
			}
			else if (full(s1) && !empty(s2))
				cout << "不能入队哦!" << endl;
		}
		else if (op == 3)
		{
			if (empty(s1) && empty(s2))
				cout << "队空!" << endl;
			else 
				{
				for (int i = s2.top; i >= 0; i--)
					cout << s2.base[i] << ' ';
				for (int i = 0; i <= s1.top; i++)
					cout << s1.base[i] << ' ';
				cout << endl;
				}
		}
		else if (op == 4)
		{
			if (!empty(s2))
				pop(s2);
			else if (empty(s1))
			{
				while (!empty(s1))
					push(s2, pop(s1));
			}
			else
				cout << "不能出队哦!" << endl;
		}
	}
	return 0;

}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nathan Qian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值