star

搜索

RSS

RSS Link

计数器

139731
队列的头文件
BrainFuck解释器

栈与队列的三个例子

Star posted @ 2010年10月05日 04:07 in C语言 with tags 队列 应用 , 2763 阅读

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;
}
Anonymous 说:
2020年8月28日 23:28

I came onto your blog while focusing just slightly submits. Nice strategy for next, I will be bookmarking at once seize your complete rises... 그래프사이트

Anonymous 说:
2020年8月30日 01:22

Hi there, I discovered your blog per Google bit searching for such kinda educational advise moreover your inform beholds very remarkable for me. sagame888

Anonymous 说:
2020年8月31日 13:58

This is important, though it's necessary to help you head over to it weblink: 성기확대수술

Anonymous 说:
2020年8月31日 14:03

You ought to basically fantastic not to mention solid advice, which means notice: 스포츠중계

Anonymous 说:
2020年9月11日 14:58

On this page you can read       my interests, write something special. ที่เด็ด

CG Board Question Pa 说:
2022年9月05日 14:52

Chhattisgarh Board Model Paper 2023 Pdf Designed and Suggested by CGBSE Raipur Board Experts to Class 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 Grade Question Paper for Hindi Medium, English Medium, Urdu Medium and others for Theory, Objective (MCQ) and Bit Questions. CG Board Question Paper New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

seo service london 说:
2024年8月08日 14:24

I can assure you that your writing is really good, easy to understand and open-minded.I'm sure you'll get better and better. Thank you so much for sharing. I understand and learn a lot. 

here 说:
2024年8月08日 14:39

Link exchange is nothing else except it is only placing the other person’s weblog link on your page at

get more info 说:
2024年8月08日 14:40

 am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work 

read more 说:
2024年8月08日 15:00

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.

Learn more 说:
2024年8月08日 15:00

This is a fabulous post I seen by virtue of offer it. It is genuinely what I expected to see look for in future you will continue subsequent to sharing such an extraordinary post.

check here 说:
2024年8月08日 15:01

not certain whether or not this post is written via him as no one else know such special about my trouble

good information 说:
2024年8月08日 15:01

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business

baccaratfriend 说:
2024年8月08日 15:02

you beloved this post and you would like to receive a lot more details regarding mamba dating kindly check out our own

information 说:
2024年8月08日 15:03

 you loved this information and you would like to receive additional facts relating to signs you are struggling with cocaine addiction kindly check out our own web

More details 说:
2024年8月08日 15:05

wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated

good information 说:
2024年8月08日 15:06

Wow, fantastic blog layout! How long have you been blogging for?

먹튀젠더 说:
2024年8月08日 15:07

Hello, the discounts and campaigns on your website are very advantageous for dermocosmetics and supplements. This allows me to shop at very affordable prices.

파워에이스 说:
2024年8月08日 15:08

of chance and strategy. Unlike traditional casino games, the Aviator Game involves a virtual plane that ascends

카지노프렌즈 说:
2024年8月08日 15:09

From 2020 year online mostbet Casino provides an opportunity to its customers near up to a hundred one-armed bandits of their own design.

here 说:
2024年8月08日 15:10

This is actually the kind of information I have been trying to find. Thank you for writing this information

here 说:
2024年8月08日 15:11

not certain whether or not this post is written via him as no one else know such special about my trouble

good contents 说:
2024年8月08日 15:12

not certain whether or not this post is written via him as no one else know such special about my trouble

tossmantoto 说:
2024年8月08日 15:13

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information

good data 说:
2024年8月08日 15:21

Bonnie He wants to talk frankly with you and show you something, you will like it. Click Here:

read more 说:
2024年8月08日 15:32

From 2020 year online mostbet Casino provides an opportunity to its customers near up to a hundred one-armed bandits of their own design.

토토사이트 说:
2024年8月08日 15:33

Hi i am kavin, its my first occasion to commenting anywhere, when i read this piece of writing i thought i could also make comment due to this sensible post.

here 说:
2024年8月08日 15:44

I was suggested this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!

information 说:
2024年8月08日 15:44

You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this

View data 说:
2024年8月08日 16:21

I blog quite often and I genuinely

Learn more 说:
2024年8月08日 16:23

Wow, fantastic blog layout! How long have you been blogging for?


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter