数据结构2.3 - 栈和队列 例题

声明:大部分内容来自 - 《2019天勤数据结构高分笔记》

1. 两个顺序栈共享一个int存储区elem[0, … , maxSize - 1],设计算法,有入栈和出栈的功能。

初始化 + 创建 + 入栈 + 出栈 + 判满 + 遍历
完整代码:

#include<bits/stdc++.h>
#define maxSize 100 
using namespace std;

typedef struct 
{
	int elem[maxSize];
	int top[2];
}Sqtack;

void init(Sqtack &st)
{
	st.top[0] = -1;
	st.top[1] = maxSize;
	cout << "初始化成功!" << endl << endl;
}

int isFull(Sqtack st)
{
	if((st.top[0] + 1) >= st.top[1])
		return 1;
	else
		return 0;
}

void push(Sqtack &st, int n, int x) // 入栈 
{
	if(isFull(st))
		cout << "栈满,入栈操作非法!" << endl << endl;
	else
	{
		if(n == 0||n == 1)
		{
			if(n == 0) 
			{
				st.elem[++st.top[0]] = x;
				printf("元素%6d 在端口0,位置%3d入栈成功!\n", x, st.top[0]);
			}
			else
			{
				st.elem[--st.top[1]] = x;
				printf("元素%6d 在端口1,位置%3d入栈成功!\n", x, st.top[1]);
			} 
		}
		else
			cout << "端口非法!" << endl << endl;
	}
}

void pop(Sqtack &st, int n, int &x)
{
	if(n == 0)
	{
		if(st.top[0] == -1)
			cout << "端口0栈空,出栈非法!" << endl << endl;
		else
		{
			x = st.elem[st.top[0]--];
			printf("元素%6d 在端口0出栈成功!\n", x);
		}
	}
	else if(n == 1)
	{
		if(st.top[1] == maxSize-1)
			cout << "端口1栈空,出栈非法!" << endl << endl;
		else
		{
			x = st.elem[st.top[1]++];
			printf("元素%6d 在端口1出栈成功!\n", x);
		}
	}
	else
		cout << "端口有问题!" << endl << endl;
}

void creat(Sqtack &st)
{
	int s0 = rand()%maxSize, s1 = rand()%maxSize;
	int i, x ;
	
	while((s0 + 1) > s1)
	{
		s0 = rand()%maxSize;
		s1 = rand()%maxSize;
	}
	cout << endl;
	cout << "s0 = " << s0 <<endl;
	cout << "s1 = " << s1 <<endl;
	for(i = 0 ;i <= s0; i++)
	{
		x = rand();
		push(st, 0 , x);
	}
	cout << endl;
	for(i = maxSize-1 ;i >= s1; i--)
	{
		x = rand();
		push(st, 1 , x);
	}
	cout << endl;
}

void trace(Sqtack st)
{
	int i;
	cout << "s0:" << endl;
	if(st.top[0] != -1)
	{
		for(i = 0; i <= st.top[0]; i++)
			cout << st.elem[i] << " " ;
		cout << endl << endl;
	}
	else
		cout << "为空!" << endl << endl;
	
	cout << "s1:" << endl;
	if(st.top[1] != maxSize) 
	{
		for(i = maxSize-1; i >= st.top[1]; i--)
			cout << st.elem[i] << " " ;
		cout << endl << endl;
	}
	else
		cout << "为空!" << endl << endl;
}

int main()
{
	Sqtack st;
	srand(time(NULL));
	int x;
	
	cout << "栈st " ;
	init(st) ;
	
	cout << "栈st " << endl;
	trace(st); 
	
	
	cout << "栈st " ;
	creat(st);
	
	cout << "栈st " << endl;
	trace(st);
	
	pop(st, 0, x) ;
	pop(st, 1, x) ;
	
	cout <<endl;
	cout << "栈st " << endl;
	trace(st);
	
	return 0;
}


2. 给定两个栈s1、s2,push(St, x):x入栈;pop(st, &x),st出栈;isEmpty(st):判空。但用栈的特点来实现队列的操作。
#include<bits/stdc++.h>
#define num 100
using namespace std;

typedef struct 
{
	int data[num];
	int top;
}sqtack;

void init(sqtack &s)
{
	s.top = -1;
	cout << "初始化成功!" << endl;
}

void push(sqtack &s, int &x)
{
	s.data[++s.top] = x;
	printf("元素 %-5d 已入栈\n", x);
}

void pop(sqtack &s, int &x)
{
	if(s.top == -1)
		cout << "栈空,无法出栈" << endl << endl;
	else
	{
		x = s.data[s.top--];
		printf("元素 %-5d 已出栈\n", x);
	}
}

void enQueue(sqtack &s1, sqtack &s2, int x)
{
	int y;
	if(s1.top == num)//s1已满 
	{
		if(s2.top == -1)
		{
			while(s1.top != -1) 
			{
				pop(s1, y);
				push(s2, y);
			}
			push(s1, x);
		} 
		else
			cout << "队列已满!" << endl << endl ;
	}
	else
	{
		push(s1, x);
	}
}

void creat(sqtack &s1, sqtack &s2)
{
	init(s1);init(s2);
	cout << endl;
	int length = rand()%num; 
	int i, x;
	
	cout << "length = " << length << endl;
	
	for(i = 0; i< length; i++)
	{
		x = rand();
		enQueue(s1, s2, x);
	}
	
}

void deQueue(sqtack &s1, sqtack &s2)
{
	int x;
	if(s2.top != -1)
	{
		pop(s2, x);
		printf("元素 %-5d已出队\n\n", x);
	}
	else
	{
		if(s1.top != -1)
		{
			while(s1.top != -1)
			{
				pop(s1, x);
				push(s2, x);
			} 
			pop(s2, x);
			printf("元素 %-5d已出队\n\n", x);
		}
		else
		{
			cout << "队列为空,无法出队!" << endl << endl;
		}
	}
}

void trace(sqtack s)
{
	int i;
	for(i = s.top; i>=0; i--)
	{
		cout << s.data[i] << " ";
	}
}

int main()
{
	sqtack s1,s2;
	srand(time(NULL));
	
	creat(s1, s2);
	cout <<endl;
	
	cout << "栈S1:" << endl;
	trace(s1);
	cout << endl;
	cout << "栈S2:" << endl;
	trace(s2);
	cout << endl;
	
	deQueue(s1, s2);
	
	return 0;
}

3. 以带头结点的循环链表 表示队列,一个指针指向队尾,没有头指针,设计入队、出队算法

结构:头结点 -> 结点1 -> 结点2 … ->结点1 循环

#include<bits/stdc++.h> 
#define num 100
using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}*Link, LNode;

void init(Link &L)
{
	L = (Link)malloc(sizeof(LNode));
	L->next = L;
	cout << "初始化成功!" << endl << endl;
}

void enQueue(Link &L, int x)
{
	Link s = (Link)malloc(sizeof(LNode));
	s->data = x;
	s->next = L->next;
	L->next = s;
	L = s;
	printf("元素 %-5d已入队\n",x);
}

void creat(Link &L)
{
	int x;
	int length = rand()%num;
	int i;
	
	for(i = 0; i < length; i++)
	{
		x = rand();
		enQueue(L, x);
	}
	cout << endl;
}

void deQueue(Link &L)
{
	Link s;
	int x;
	if(L->next == L)
		cout << "队列为空,无法出队!" << endl << endl;
	else
	{
		s = L->next->next;
		x = s->data;
		L->next->next = s->next;
		if(s == L)
			L->next = L;
		free(s);
		printf("元素 %-5d已出队\n\n", x);
	}
}

void trace(Link L)
{
	Link s = L->next->next;
	// 头结点 -> 结点1 -> 结点2 ... ->结点1 循环 
	while(s!=L)
	{
		cout << s->data << " ";
		s = s->next;
	}
	cout << s->data << endl << endl ;
}

int main()
{
	Link L;
	srand(time(NULL));
	
	cout << "循环链表队列L";
	init(L);
	
	cout << "循环链表队列L" << endl;
	creat(L);
	
	cout << "循环链表队列L元素:" << endl;
	trace(L);
	
	cout << "循环链表队列L元素出队:" << endl;
	deQueue(L); 
	
	cout << "循环链表队列L元素:" << endl;
	trace(L);
	
	return 0;
}

4. 循环队列,front、rear作为对头和队尾指针,tag = 0为对空、tag = 1为队非空,这样 front = rear可作为判断队满的标志

初始化 + 创建 + 入队 + 出队 + 判空 + 判满 + 遍历

#include<bits/stdc++.h>
#define num 100
using namespace std;

typedef struct 
{
	int data[num];
	int front, rear;
	int tag;
}Queue;

void init(Queue &L)
{
	L.front = L.rear = 0;
	L.tag = 0;
	cout << "初始化完成!"; 
	cout << "(大小" << num << ")" << endl << endl;
}

int QueueFull(Queue L)
{
	if(L.front == L.rear && L.tag == 1)
		return 1;
	else
		return 0;
}

int QueueEmpty(Queue L)
{
	if(L.front == L.rear && L.tag == 0)
		return 1;
	else
		return 0;
}

void enQueue(Queue &L, int x)
{
	if(QueueFull(L) == 1)
		cout << "队满!" << endl << endl;
	else
	{
		L.rear = (L.rear + 1)%num;
		L.data[L.rear] = x;
		printf("元素 %-5d已入队\n", x);
		L.tag = 1;
	}
}

void deQueue(Queue &L) 
{
	int x;
	if(QueueEmpty(L) == 1)
		cout << "队空!" << endl <<endl;
	else
	{
		L.front = (L.front + 1)%num;
		x = L.data[L.front];
		L.tag = 0;
		printf("元素 %-5d已出队\n\n", x);
	} 
}

void creat(Queue &L)
{
	int x;
	int length = rand()%num;
	int i;
	
	for(i = 0; i< length; i++)
	{
		x = rand();
		enQueue(L, x);
	}
	cout << "创建完成!" << endl << endl;
}

void trace(Queue L)
{
	int i;
	if(QueueEmpty(L) == 1)
		cout << "队空!" << endl;
	else
	{
		for(i = L.front + 1; i!=L.rear; )
		{
			cout << L.data[i] <<" ";
			i = (i + 1)%num;
		}
		cout << L.data[i] << endl << endl;
	} 
}

int main()
{
	Queue L;
	srand(time(NULL));
	
	cout << "队列L" ;
	init(L);
	
	cout << "队列L" << endl;
	creat(L);
	
	cout << "队列L元素:" << endl;
	trace(L);
	
	cout << "队列L元素出队:" << endl;
	deQueue(L);
	
	cout << "队列L元素:" << endl;
	trace(L);
	
	return 0;
}

5. 将非负十进制整数转为二进制

用到了简单的栈结构

#include<bits/stdc++.h>
#define num 100
using namespace std;

void change(unsigned int x)
{
	int st[num];
	int top = -1;
	
	while(x)
	{
		st[++top] = x%2;
		x = x/2;
	}
	while(top != -1)
	{
		cout << st[top--];
	}
	cout << endl << endl;
}

int main()
{
	unsigned int x;
	
	while(scanf("%d", &x)!=EOF)
	{
		change(x);
	}
	return 0;
}

6. 括号匹配,包括花、方、圆括号,以及单、双引号必须承兑出现
#include<bits/stdc++.h>
#define num 100
using namespace std;

typedef struct 
{
	char data[num];
	int top;
}Sqtack;

void init(Sqtack &st)
{
	st.top = -1;
	cout << "初始化成功!" << endl;
}

void getTop(Sqtack st, char &x)
{
	if(st.top!=-1)
		x = st.data[st.top];
	else
		cout << "栈空!" << endl;
}

void push(Sqtack &st, char x) 
{
	if(st.top!=num-1)
		st.data[++st.top] = x;
	else
		cout << "栈满!" << endl << endl;;
}

void pop(Sqtack &st) 
{
	if(st.top!=-1)
		st.top--;
	else
		cout << "栈空!" << endl;
}

int isEmpty(Sqtack st) 
{
	if(st.top == -1)
		return 1;
	else
		return 0;
}

void check(char s[])
{
	char* p = s;
	char ch;
	Sqtack st;
	init(st);
	
	while(*p!='\0')
	{
		if(*p == 39)
		{
			++p;
			while(*p != 39)
				p++;
			p++;
		}
		else if(*p == 34)
		{
			++p;
			while(*p != 34)
				p++;
			p++;
		}
		else
		{
			switch(*p)
			{
				case '{':
				case '[':
				case '(': push(st, *p);break;
				case ')':getTop(st,ch);
					if(ch == '(')
						pop(st);
					else
						break;
				case ']':getTop(st,ch);
					if(ch == '[')
						pop(st);
					else
						break;
				case '}':getTop(st,ch);
					if(ch == '{')
						pop(st);
					else
						break;
			}
		}
		p++;
	}
	if(isEmpty(st))
		cout << "括号匹配!" << endl << endl; 
	else
		cout << "括号不匹配!" << endl << endl;
}

int main()
{
	char s[num];
	while(scanf("%s", s)!=EOF)
	{
		check(s);
	}
	
	return 0;
}

7. 求根号的迭代公式


两个方向:1迭代 与 2非迭代
迭代代码:

#include<bits/stdc++.h> 
#define num 10
using namespace std;

void sq(double A, double p, double e)
{
	if(fabs(p*p - A) < e)
		cout << "sqrt(A) = p = " << p << endl << endl;
	else
	{
		p = (p + A/p)/2;
		cout << "p = " << p << endl;
		sq(A, p, e);
	}
}

int main()
{
	double A, p, e = pow(10, -10);
	int i;
	while(scanf("%lf", &A)!=EOF)
	{
		cout << "sqrt(A) = " << sqrt(A) << endl; 
		p = 1;
		sq(A, p, e);
	}
	return 0 ;
}


非迭代代码:

#include<bits/stdc++.h> 
using namespace std;

void sq(double A, double p, double e)
{
	while(fabs(p*p - A) > e)
	{
		p = (p + A/p)/2;
	}
	cout << "p = " << p << endl << endl;
}

int main()
{
	double A, p, e = pow(10, -10);
	
	while(scanf("%lf", &A)!=EOF)
	{
		p = 1;
		sq(A, p, e);
	}
	
	return 0; 
}

8. 栈出入列举

牛客
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109?tab=answerKey

给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号,火车站只有一个方向进出,同时停靠在火车站的列车中,只有后进站的出站了,先进站的才能出站。
要求输出所有火车出站的方案,以字典序排序输出

输入:
3
1 2 3

输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 2 1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

qq_1403034144

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

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

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

打赏作者

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

抵扣说明:

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

余额充值