C语言栈的顺序存储结构操作(结构定义、初始化、入栈、出栈、长度等)

 1、顺序栈:利用顺序存储方式表示的栈。

2、栈中的数据元素可用一维数组来实现:datatype data[MAXSIZE],栈底位置可以设置数组的任意端点,栈顶则是随着入栈、出栈(插入和删除)而变化的,通常以下表0的一端表示栈底,用一个位置指针top作为栈顶。


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>  //用户通过键盘产生对应操作 
#define MAXSIZE 20

#define datatype char
//结构定义 
typedef struct
{
	datatype data[MAXSIZE];
	int top;
}SeqStack;
//初始化
void InitStack(SeqStack *s)
{
	s->top=-1;
}
//入栈
int Push(SeqStack*s,datatype x)
{
	if(s->top==MAXSIZE-1)  //栈满,不能入栈 
		return 0;
	else 
	{
		s->top++;
		s->data[s->top]=x;  //插入数据元素X赋值给栈顶空间 
		return 1;
	}
}
//出栈
int Pop(SeqStack*s,datatype*x)
{
	if(s->top==-1)     //空栈不能出栈 
		return 0;
	else
	{
		*x=s->data[s->top];  //栈顶元素存入*x 
		s->top--;
		return 1;
	}
}
//求长度
int Len(SeqStack*s)
{
	int length;
	length=s->top+1;
	return length;
} 
//取栈顶元素
int GetTop(SeqStack*s,datatype*x)
{
	if(s->top==-1)
		return 0;
	*x=s->data[s->top];
	return 1;
} 
//菜单页面
char menu(void)
{
	char ch;
	printf("    顺序栈操作\n   ");
	printf("-----------------\n");
	printf("   选择操作选项\n  ");
	printf("      0.初始化\n    ");
	printf("    1.入栈\n      ");
	printf("  2.出栈\n        ");
	printf("3.取栈顶元素\n   ");
	printf("     4.长度\n    ");
	printf("    5.退出\n        ");
	printf("-----------------\n");
	printf("输入选项:");
	ch=getchar();
	return(ch);
} 

void main()
{
	SeqStack st;
	int l,k,flag=1;
	datatype x;
	char choice;
	InitStack(&st);
	do{ choice=menu();
	switch(choice)
	{
		case'0':
			InitStack(&st);
			printf("初始化成功!\n");
			getchar();
			break;
		case'1':
			printf("请输入入栈数据:");
			scanf("%d",&x);
			k=Push(&st,x);
			if(k)  printf("入栈成功!\n");
			else printf("栈已满!\n");
			getchar();
			break;
		case'2':
			k=Pop(&st,&x);
			if(k)  printf("出栈数据=%d\n",x);
			else printf("栈为空!\n"); 
			getchar();
			break;
		case'3':
			k=GetTop(&st,&x);
			if(k)  printf("栈顶元素=%d\n",x);
			else printf("栈为空!\n"); 
			getchar();
			break; 
		case'4':
			l=Len(&st);
			printf("长度为:%d\n",l);
			getchar();
			break;
		case'5':flag=0;break;		
	 }
	
	}while(flag==1);
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
数据结构是计算机科学中非常重要的一门课程,它主要研究数据的组织、存储和管理方式。而和队列是数据结构中非常基础的两种数据结构,它们在程序设计中有着广泛的应用。下面是关于顺序栈的基本操作介绍: 1. 入栈:将元素入栈顶,即将元素入栈顶位置。 2. 出栈:将元素弹出,即将元素删除。 3. 读元素:获取元素,即获取顶位置的元素。 4. 建初始化一个空。 5. 判断满:当元素个数等于的最大容量时,即为满。 6. 判断空:当中没有元素时,即为空。 下面是关于顺序栈C语言实现: ```c #define MAXSIZE 100 // 定义的最大容量 typedef struct { int data[MAXSIZE]; // 存储元素的数组 int top; // 顶指针 } SqStack; // 初始化一个空 void InitStack(SqStack *S) { S->top = -1; } // 判断是否为空 int StackEmpty(SqStack S) { if (S.top == -1) { return 1; } else { return 0; } } // 判断是否为满 int StackFull(SqStack S) { if (S.top == MAXSIZE - 1) { return 1; } else { return 0; } } // 入栈 int Push(SqStack *S, int x) { if (StackFull(*S)) { return 0; // 满,入栈失败 } else { S->top++; S->data[S->top] = x; return 1; // 入栈成功 } } // 出栈 int Pop(SqStack *S, int *x) { if (StackEmpty(*S)) { return 0; // 空,出栈失败 } else { *x = S->data[S->top]; S->top--; return 1; // 出栈成功 } } // 读元素 int GetTop(SqStack S, int *x) { if (StackEmpty(S)) { return 0; // 空,获取元素失败 } else { *x = S.data[S.top]; return 1; // 获取元素成功 } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值