-->
当前位置:首页 > 题库 > 正文内容

程序填空题:栈的基本操作

Luz4年前 (2021-05-10)题库4209
实验目的:1、掌握栈和队列的基本知识 2、深入理解栈和队列的特征,掌握并灵活运用栈和队列。3、用顺序结构表示栈并实现栈的各种基本操作
将程序填写完整,实现栈的初始化、释放栈、入栈、出栈等基本操作。
```c++

例如:

#include
#include
#define MaxSize 100
typedef char ElemType;
typedef struct
{
ElemType elem[MaxSize];
int top; //栈指针
} SqStack;
void InitStack(SqStack **s) //初始化栈s
{
*s=(SqStack *)malloc(sizeof(SqStack));
(*s)->top=@@[-1](1);
}
void ClearStack(SqStack *s) //释放栈s
{
@@[ free(s);](2)
}
int StackLength(SqStack *s) //求栈s长度
{
int len=0;
while(s->top!=-1)
{
len++;
s->top--;
}
s->top+=len;
return len;
}
int StackEmpty(SqStack *s) //判断栈s是否为空栈
{
if(@@[s->top==-1](1))
return 1;
else
return 0;
}
int Push(SqStack *s,ElemType e) //进栈元素e
{
if(s->top==MaxSize-1)
return 0;
@@[s->top++;](2)
s->elem[s->top]=e;
return 1;
}
int Pop(SqStack *s,ElemType &e) //出栈一个元素
{
if(s->top==-1)
return 0;
else
{
@@[e=s->elem[s->top];](2)
s->top--;
}
return 1;
}
int GetTop(SqStack *s,ElemType *e) //取栈顶元素
{
if(s->top==-1)
return 0;
else
{
*e=s->elem[s->top];
}
return 1;
}
void DispStack(SqStack *s) //从栈顶到栈底输出元素
{
while(s->top!=-1)
{
printf("%c\n",@@[s->elem[s->top]](2));
s->top--;
}
}


```







答案:
第1空:-1

第2空: free(s);

第3空:s->top==-1

第4空:s->top++;

第5空:e=s->elem[s->top];

第6空:s->elem[s->top]

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。