话不多说,直接上图


#include<stdio.h>
#include<stdlib.h>
#define SElemType int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
void menu();
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int InitStack (SqStack &S);
void DestroyStack(SqStack &S);
void ClearStack(SqStack &S);
void StackEmpty (SqStack S);
int StackLength (SqStack S);
void GetTop (SqStack S);
int Push (SqStack &S,SElemType );
int Pop (SqStack &S, SElemType&);
void putout(SqStack S) ;
void creatintput(SqStack &S);
int main()
{
menu();
int select,m,n;
SqStack L;
while(1)
{
printf("请输入您的选择:\n");
scanf("%d",&select);
if(select<=0||select>11)
{
printf("您的选择超出范围。");
break;
}
else
{
switch(select)
{
case 1:
InitStack(L);
break;
case 2:
DestroyStack(L);
break;
case 3:
ClearStack(L);
break;
case 4:
StackEmpty(L);
break;
case 5:
m=StackLength(L);
printf("栈的长度为%d。",m);
break;
case 6:
GetTop(L);
break;
case 7:
printf("请输入插入元素:");
scanf("%d",&m);
Push(L,m);
break;
case 8:
n=Pop(L,m);
printf("被删除元素为:%d。",n);
break;
case 9:
putout(L);
break;
case 10:
creatintput(L);
break;
case 11:
return 0;
}
}
}
return 0;
}
void menu()
{
printf("***********************************************\n");
printf("********* 1.初始化为空栈 *********\n");
printf("********* 2.销毁栈 *********\n");
printf("********* 3.将栈置空 *********\n");
printf("********* 4.判断是否为空栈 *********\n");
printf("********* 5.返回栈长 *********\n");
printf("********* 6.求栈顶元素 *********\n");
printf("********* 7.插入元素设为栈顶 *********\n");
printf("********* 8.删除元素并返回 *********\n");
printf("********* 9.输出栈内元素 *********\n");
printf("********* 10.创建并输入元素 *********\n");
printf("********* 11.退出 *********\n");
printf("***********************************************\n");
}
int InitStack (SqStack &S)
{
S.base=(SElemType *) malloc (STACK_INIT_SIZE *sizeof (SElemType));
if (!S.base) {
printf("未成功分配!");
return OK;
}
S.top=S.base;
S.stacksize= STACK_INIT_SIZE;
printf("初始化成功!");
return OK;
}
void DestroyStack(SqStack &S)
{
S.top=NULL;
S.stacksize=0;
free(S.base);
printf("销毁成功!");
}
void ClearStack(SqStack &S)
{
S.top=S.base;
printf("清空成功!");
}
void StackEmpty (SqStack S)
{
if(S.top==S.base) printf("栈为空!");
else printf("栈不为空!");
}
int StackLength (SqStack S)
{
return (S.top-S.base);
}
void GetTop (SqStack S)
{
printf("栈顶元素为:%d",*(S.top-1));
}
int Push (SqStack &S,SElemType P )
{
if ( (S.top-S.base)>=S.stacksize )
{
S.base=(SElemType *) realloc ( S.base,(S.stacksize+STACKINCREMENT)*sizeof (SElemType));
if (!S.base)
{
printf("未成功再次申请空间!");
return ERROR;
}
S.top=S.base + S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top= P;
++S.top;
printf("插入完成!");
return OK;
}
int Pop (SqStack &S, SElemType& P)
{
int m;
m=*(S.top-1);
P=m;
--S.top;
return P;
}
void putout(SqStack S)
{
while(S.top!=S.base)
{
printf("%d",*(S.base));
++S.base;
}
printf("输出完成!");
}
void creatintput(SqStack &S)
{
int m,n;
InitStack(S);
printf("请输入你要输入的个数:");
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
printf("请输入你要输入的数:");
scanf("%d",&n);
Push(S,n);
}
printf("插入完成!");
}
新手上路,勿喷。
本文分享了一个使用C语言实现的栈操作程序,通过菜单形式提供了包括初始化、销毁、清空、判断空栈、获取栈长、求栈顶元素、插入元素、删除元素及输出栈内元素等功能。程序详细展示了如何利用动态内存分配来实现栈的增容。
3821





