栈数据结构及基本运算

定义

操作受限的线性表,限定对元素的插入和删除只能在表的一端进行。

数据结构定义

#define MAXSIZE 100
typedef struct
{
	int data[MAXSIZE];
	int top;
}STACK_S;

通常把0作为栈底,栈空时,栈顶指针top = -1;栈满时,top = MAXSIZE-1;
操作:入栈时,top++;出栈时,top--;
上溢(overflow):栈满时的入栈操作;
下溢(underflow):栈空时的出栈操作;

基本运算

//1、置空栈
void set_stack_null(STACK_S *s)
{
	s->top = -1;
}
//2 判断栈空
int is_stack_empty(STACK_S *s)
{
	if(s->top == -1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
//3 入栈算法
int push_stack(STACK_S *s, int x)
{
	if(s->top == MAXSIZE - 1)
	{
		return RET_OVERFLOW_ERR;
	}
	else
	{
		s->top++;
		s->data[s->top] = x;
		return RET_SUCCESS;
	}
}
//4 出栈算法
int pop_stack(STACK_S *s)
{
	if(s->top == -1)
	{
		return RET_MULL_ERR;
	}
	else
	{
		s->top--;
		return s->data[s->top + 1];
	}
}
//5 读栈顶元素
int get_top(STACK_S *s)
{	
	if(s->top == -1)
	{
		return RET_NULL_ERR;
	}
	else
	{
		return s->data[s->top];
	}
}

示例

1、括号匹配()
遇到左括号(,则入栈,遇到右括号则出栈,遍历完字符串之后,若top != -1 则括号不匹配。
2、迷宫问题
优秀代码学习
难得在编程论坛看到一个优秀的代码, 学习一下,

作者:鬼鬼千年
#include<stdio.h> 
int maze[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,1,0,0,0,0,0,0,1}, 
        {1,1,1,1,1,1,1,1,1,1}};/*初始化迷宫*/ 
int mark[10][10] = {0};/*初始化标志位,0代表没走过,1代表走过*/ 

/*方向*/ 
typedef struct{ 
    short int vert; 
    short int horiz; 
}offsets; 

offsets move[4] = {{0,1},{1,0},{0,-1},{-1,0}};/*北,东,南,西*/ 


/*迷宫类型*/ 
typedef struct element{ 
    short int row; 
    short int col; 
    short int dir; 
}element; 

element stack[100]; 


void path(void); 
  element del(int* top); 
void add(int* top,element item); 


element del(int* top)/*出栈,top指向栈顶元素*/ 
{ 
    if(*top == -1) 
    { 
        printf("stack is empty!\n"); 
    } 
    return stack[(*top)--]; 
} 



void add(int* top,element item)/*入栈*/ 
{ 
    if(*top >= 100) 
    { 
        printf("stack is full!\n"); 
        return; 
    } 
    stack[++*top] = item; 
} 


void path(void)/*迷宫函数*/ 
{ 
    element position; 
    int i,row,col,next_row,next_col,dir,top; 
    int found = 0; 
    mark[1][8] = 1,top = 0;/*初始化标志数组元素以及栈*/ 
    stack[0].row = 1,stack[0].col = 8,stack[0].dir = 0; 
    while(top > -1 && !found) 
    { 
          position= del(&top);  /*将栈顶元素取出,*/ 
        row = position.row;            /*利用中间变量row,col,dir等候判断*/ 
        col = position.col; 
        dir = position.dir; 
        while(dir < 4 && !found) 
        { 
            next_row = row + move[dir].vert; 
            next_col = col + move[dir].horiz; 
            if(row == 7 && col == 1) 
                found = 1; 
            else if(!maze[next_row][next_col] && !mark[next_row][next_col])/*判断下一步可走并且没走过,则入栈*/ 
                    { 
                        mark[next_row][next_col] = 1; 
                        position.row = row; 
                        position.col = col; 
                        position.dir = ++dir; 
                        add(&top,position);/*合理则入栈*/ 
                        row = next_row;/*继续向下走*/ 
                        col = next_col;dir = 0; 
                    } 
                    else 
                        dir++;/*dir<4时,改变方向*/ 
       
        } 
        if(found)/*判断是否有出口*/ 
        { 
            printf("the path is:\n"); 
            printf("row col\n"); 
            for(i = 0;i <= top;++i) 
                printf("(%2d,%5d)",stack[i].row,stack[i].col);
                printf("(%2d,%5d)\n",7,1);
            
            
        } 
         
            
    }
    if(found==0)
        printf("没有路径\n");
}


int main(void) 
{   
    path(); 
     
return 0; 
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值