star

搜索

RSS

RSS Link

计数器

140171

栈与队列的三个例子

2010年10月05日 04:07 | Comments(31) | Category:C语言 | Tags:

1.小括号匹配

#define FORMATSTR "%c "
typedef char ElemType;
#define BufSize 128

#include "Stack.h"

int CheckBracket(char *str, int len)
{
	int     i = 0;
	SQSTACK S;
	char    ch;
	InitSqstack(&S, len);
	while(str[i])
	{
		ch = str[i];
		if(ch == '(')
			PushS(&S, ch);
		else if(ch == ')')
			if(!PopS(&S, &ch))
			{
				DestroySqstack(&S);
				return 0;
			}
		++i;
	}
	if(IsSqstackEmpty(S))
		;
	else
		i = 0;
	DestroySqstack(&S);
	return i;
}

int main(void)
{
	//栈示例 - 括号匹配 
	char string[BufSize];
	
	gets(string);
	if(CheckBracket(string, strlen(string)))
		puts("Match!");
	else
		puts("Mismatch!");
		
	return 0;
}

2.迷宫求解

#define FORMATSTR
typedef struct
{
	int x, y, v;	//坐标与方向 
}ElemType;
#include "Stack.h"

int Map[10][10] = 
{
1,1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,0,1,0,1,
1,0,0,1,0,0,0,1,0,1,
1,0,0,0,0,1,1,0,0,1,
1,0,1,1,1,0,0,0,0,1,
1,0,0,0,1,0,0,0,0,1,
1,0,1,0,0,0,1,0,0,1,
1,0,1,1,1,0,1,1,0,1,
1,1,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,	
};

int tox[5] = {0, 0,1,0,-1};	//偏正方向 
int toy[5] = {0,-1,0,1, 0};	//偏正方向 

int main(void)
{
	//链式栈示例 - 迷宫求解,给定的迷宫,输出所有解。 
	LINKSTACK S;
	ElemType  e;
	int x, y, v;	//当前坐标 
	int ox, oy;		//修正坐标 
	int i, j, k = 0;
	e.x = 1; e.y = 1;	//出发点 
	e.v = 0;			//方向
		 
	InitLinkstack(&S);
	PushL(S, e);
	Map[1][1] = 8;
	while(!IsLinkstackEmpty(S))
	{
		PopL(S, &e);
		x = e.x; y = e.y; v = e.v + 1;
		//back from here
			if(e.v > 0)
				Map[y+toy[e.v]][x+tox[e.v]] = 0;
		while(v <= 4)
		{
			ox = x + tox[v];
			oy = y + toy[v];
			if(ox == 8 && oy == 9)	//终点
			{
				printf("Answer %d:\n", ++k);
				for(i = 0; i < 10; ++i)
				{
					for(j = 0; j < 10; ++j)
						printf("%d ", Map[i][j]);
					putchar(10);
				}
				++v;
			}
			else if(ox > 0 && oy > 0 && !Map[oy][ox])
			{
				e.x = x; e.y = y; e.v = v;
				PushL(S, e);
				x = ox, y = oy; v = 1;
				Map[y][x] = 8;
			}
			else
				++v;
		}
	}
	
	DestroyLinkstack(&S);
	return 0;
}

3.最短路径

#define FORMATSTR
typedef struct
{
	int x, y, pre;	//坐标与方向 
}ElemType;
#include "Queue.h"

int Map[10][10] = 
{
1,1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,0,1,0,1,
1,0,0,1,0,0,0,1,0,1,
1,0,0,0,0,1,1,0,0,1,
1,0,1,1,1,0,0,0,0,1,
1,0,0,0,1,0,0,0,0,1,
1,0,1,0,0,0,1,0,0,1,
1,0,1,1,1,0,1,1,0,1,
1,1,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,	
};

int tox[5] = {0, 0,1,0,-1};	//偏正方向 
int toy[5] = {0,-1,0,1, 0};	//偏正方向 

int main(void)
{
	//队列示例 - 迷宫的最短路径 
	SQQUEUE   Q;
	ElemType  e;
	int x, y, v;	//当前坐标 
	int ox, oy;		//修正坐标 
	int i, j, k = 0, step = 2;		//从02开始 
	e.x = 1; e.y = 1;	//出发点 
	e.pre = 2;			//前驱 
		 
	InitSqqueue(&Q, 10*10);
	EnSqqueue(&Q, e);
	Map[1][1] = step;
	while(!IsSqqueueEmpty(Q))		//BFS 
	{
		DeSqqueue(&Q, &e);
		x = e.x; y = e.y; v = e.pre += 1;
		for(i = 1; i <= 4; ++i)
		{
			e.x = x + tox[i];	e.y = y + toy[i];
			if(Map[e.y][e.x] == 0)
			{
				EnSqqueue(&Q, e);
				Map[e.y][e.x] = e.pre;
				if(e.x == 8 && e.y == 8)	//终点 
				{
					printf("The Shortest Path is:\n");
					for(i = 0; i < 10; ++i)
					{
						for(j = 0; j < 10; ++j)
							printf("%02d ", Map[i][j]);
						putchar(10);
					}
					DestroySqqueue(&Q);
					return 0;			//找到就停 
				}
			}
		}
	}
	
	DestroySqqueue(&Q);
	return 0;
}