顺序栈---以及其应用

回溯法

找迷宫路径

struct offsets
{
	int a, b;
	char* dir;
};

offsets moves[8];
int seekpath(int x, int y )  //表示坐标
{//出发点为(1,1)
	int  g, i, h = 0;
	int m = 9, int p = 10;//出口坐标
	char* d;
	if (x == m && y == p)
		return 1;
	for (i = 0; i < 8; i++)
	{
		g = x + moves[i].a;
		h = y + moves[i].b;
		d = moves[i].dir;
		if (Maze[g][h] == 0 && mark[g][h] == 0)
		{
			mark[g][h] = 1; //标记走过
			if (seekpath(g, h))
			{
				cout << "(" << g << "," << h << ")" << endl;
				return 1;
			}
		}
	}
	if (x == 1 && y == 1) return 0;//没有路径

}
#include <iostream>
using namespace std;
template<class T>
class Stack
{
private:
	T* element;
	int top;
	int maxSize;
	void overflowProcess();   //栈的溢出处理
public:
	Stack(int sz=50);
	~Stack();
	void Push(T x);
	bool Pop(T  &x);
	bool getTop(T& x);
	bool Isempty()const { return top == -1; }
	bool Isfull()const { return top == maxSize - 1; }   //比较是否栈顶指针移到最后
};
template<class T>
Stack<T>::Stack(int sz) :maxSize(sz)
{
	top = -1;
	element = new T[maxSize];
}
template<class T>
Stack<T>::~Stack()
{
	delete[]element;
}
template<class T>
void Stack<T>::overflowProcess()
{
	T* newarray = new T[2 * maxSize];
	for (int i = 0; i < maxSize; i++)
	{
		newarray[i] = element[i];
	}
	delete[]element;
	maxSize = maxSize + maxSize;
	element = newarray;
}
template<class T>
void  Stack<T>::Push(T x)
{
if (Isfull())  overflowProcess();
	//top++;
	//element[top] = x;
	element[++top] = x;
}
template<class T>
bool Stack<T>::Pop(T &x)  //带回值
{
	if (Isempty()==true) return false;
	x = element[top];
	top--;
	return true;
}
template<class T>
bool Stack<T>::getTop(T& x)
{
	if (Isempty() == true) return false;
	x = element[top];
	return true;
}
int main()
{
	Stack<int> st;
	for (int i = 0; i < 10; i++)
	{
		st.Push(i);
	}

	for (int i = 0; i < 10; i++)
	{
		st.Pop(i);
	}

	return 0;
}




一、括号匹配问题

#include <iostream>
using namespace std;
#include "mysteck.h"

//括号匹配问题(2+3)*3-(1+3)

void PrintMatchedPair(char* expression)
{
	Stack <int >s(60);
	int j = 0, length = strlen(expression);  //j是为了跟踪压栈和弹栈的位置
	cout << length << endl;
	for (int i = 1; i <= length; i++)
	{
		if (expression[i - 1] == '(')       //注意这里是单引号,不是双引号。只有一个字符 
		{
			s.Push(i); j++;
		}
		else if (expression[i - 1] == ')')
		{
			if (s.Pop(j) == true)
			{
				cout << j << "与" << i << "匹配" << endl;
				j--;
			}
			else
				cout << "没有与第" << i << "个右括号匹配的左括号";
		}
	}
	while (s.Isempty() == false)
	{
		s.Pop(j);
		cout << "没有与第" << j << "个左括号相匹配的右括号";
	}
}


int main()
{
	char ex[20];
	cin >> ex;
	PrintMatchedPair(ex);
	return 0;
}

二、进制转换问题
在这里插入图片描述

#include <iostream>
using namespace std;
#include "mysteck.h"

int main()
{
	Stack <int >s(30);
	int num = 0,e=0;
	int x = 0;
	cin >> num;
	while (num)
	{
		s.Push(num % 2);
		num = num / 2;
	}
	while (!s.Isempty())
	{
		s.Pop(e);
		cout << e;
	}

}

三、对称字符串的判断

栈判断对称字符串
这里分奇和偶数讨论

#include <iostream>
using namespace std;
template<class T>
class Stack
{
private:
	T* element;
	int top;
	int maxSize;
	void overflowProcess();   //栈的溢出处理
public:
	Stack(int sz = 50);
	~Stack();
	void Push(T x);
	bool Pop(T& x);
	bool getTop(T& x);
	bool Isempty()const { return top == -1; }
	bool Isfull()const { return top == maxSize - 1; }   //比较是否栈顶指针移到最后
};
template<class T>
Stack<T>::Stack(int sz) :maxSize(sz)
{
	top = -1;
	element = new T[maxSize];
}
template<class T>
Stack<T>::~Stack()
{
	delete[]element;
}
template<class T>
void Stack<T>::overflowProcess()
{
	T* newarray = new T[2 * maxSize];
	for (int i = 0; i < maxSize; i++)
	{
		newarray[i] = element[i];
	}
	delete[]element;
	maxSize = maxSize + maxSize;
	element = newarray;
}
template<class T>
void  Stack<T>::Push(T x)
{
	top++;
	element[top] = x;
	if (Isfull())  overflowProcess();
}
template<class T>
bool Stack<T>::Pop(T& x)  //带回值
{
	if (Isempty() == true) return false;
	x = element[top];
	top--;
	return true;
}
template<class T>
bool Stack<T>::getTop(T& x)
{
	if (Isempty() == true) return false;
	x = element[top];
	return true;
}
int main()
{
	Stack<char> st;
	char c[80];
	char s='\0';
	cin >> c;
	int m = 0;
	while (c[m] != '\0')
	{
		m++;
	}
	cout << m;  //记下字符的个数。
	int i = -1,num=0;
	int flag = 0;
	if (m % 2 != 0)
	{
		for (int i = 0; i < m / 2; i++)
			st.Push(c[i]);
		for (int i =(m / 2)+1; i < m; i++)
		{
			st.Pop(s);
			if (s != c[i])
			{
				flag = 1;
				break;
			}
		}
	}
	else
	{
		for (int i = 0; i < m / 2; i++)
			st.Push(c[i]);
		for (int i = (m / 2); i < m; i++)
		{
			st.Pop(s);
			if (s != c[i])
			{
				flag = 1;
				break;
			}
		}
	}
	if (flag==0)
	{
		cout << "yes" << endl;
	}
	else
	{
		cout << "no" << endl;
	}
}

题目:

在这里插入图片描述法一:用尾插法,并用栈重新赋值。

#include<iostream>
using namespace std;
#include "stack.h"

struct node
{
	int data;
	node* link;
};

int main()
{
	node* first,*tail,*p;
	Stack<int>s(30);
	first = tail = new node;
	int x = 1;
	while (x)
	{
		cin >> x;
		if (x == 0)
			break;
		p = new node;
		p->data = x;
		s.Push(x);
		tail->link = p;
		tail = p;
	}
	tail->link = NULL;
	p = first->link;
	while (p != NULL)
	{
		cout << p->data << " ";
		p = p->link;
	}
	cout << endl;




	p = first->link;
	while (p != NULL)
	{
		s.Pop(x);
		p->data = x;
		p = p->link;
	}

	p = first->link;
	while (p != NULL)
	{
		cout << p->data << " ";
		p = p->link;
	}
	cout << endl;
	return  0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值