顺序栈的基本操作c语言数据结构实验报告,数据结构 - C语言版 - 顺序栈 所有基本操作...
C语言 - 顺序栈 所有基本操作//结构体 定义为 数组 的方式#include #include #define MAXSIZE 5typedef struct Stack{int data[MAXSIZE];int top;}Stack;Stack *Init_SeqStack(){Stack *s;s=malloc(sizeof(Stack));s->top = -1;return s
C语言 - 顺序栈 所有基本操作
//结构体 定义为 数组 的方式
#include
#include
#define MAXSIZE 5
typedef struct Stack{
int data[MAXSIZE];
int top;
}Stack;
Stack *Init_SeqStack()
{
Stack *s;
s=malloc(sizeof(Stack));
s->top = -1;
return s;
}
void Push_Stack(Stack * S)
{
int i;
for(i=0; i
{
if(S->top== (MAXSIZE-1) )
{
printf("栈满,无法继续输入!\n");
break;
}
S->top++;
scanf("%d", &S->data[S->top] );
}
}
void Pop_Stack(Stack * S)
{
while(S->top != -1)
{
printf("%d ",S->data[S->top]);
S->top--;
}
}
int main(void)
{
Stack *S;
S = Init_SeqStack();
Push_Stack(S);
Pop_Stack(S);
printf("\n");
system("pause");
return 0;
}
//结构体 定义为 数组基地址 的方式
#include
#define MAXSIZE 100
typedef int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
SqStack S;
int InitStack(SqStack *S)
{
S->base= malloc(sizeof(SElemType[MAXSIZE]));
if(!S->base) return 0;
S->top = S->base;
S->stacksize=MAXSIZE;
return 1;
}
int Push(SqStack *S,SElemType e)
{
if((S->top)-(S->base) == S->stacksize)
return 0;
else
{
*S->top++=e;
return 1;
}
}
int Pop(SqStack *S,SElemType *e)
{
if(S->top==S->base) return 0;
else
{
*e = *--S->top;
return 1;
}
}
main()
{
int a;
InitStack(&S);
Push(&S,1);
Push(&S,2);
Push(&S,3);
Push(&S,4);
Push(&S,5);
Pop(&S,&a);
printf("%d\n",a);
Pop(&S,&a);
printf("%d\n",a);
Pop(&S,&a);
printf("%d\n",a);
Pop(&S,&a);
printf("%d\n",a);
Pop(&S,&a);
printf("%d\n",a);
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)