函数题:顺序栈操作-补充顺序栈入栈函数
本题要求实现一个顺序栈的入栈函数,可将字符压入到顺序栈中,如果成功,返回1,如果失败,返回0。
### 函数接口定义:
c
int Push (SqStack * S, DataType e);
其中 S 和 e 都是用户传入的参数。 S 是指向顺序栈的指针; e 是字符变量。如果入栈成功,返回1,如果失败,返回0。
### 裁判测试程序样例:
c
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define STACKSIZE 100 // 宏定义,设栈最大容量为100
typedef char DataType; // 数据类型
/* 顺序栈 */
typedef struct // 顺序栈定义
{
DataType items[STACKSIZE];
int top; // top表示栈顶指针,取值范围-1—STACKSIZE-1
}SqStack;
/* 【 本题要求函数-入栈 】*/
int Push (SqStack * S, DataType e);
/*S为指向顺序栈的指针,e为待入栈的数据元素*/
/* 顺序栈初始化 */
void Initstack(SqStack *stack) // stack为指向顺序栈的指针
{
stack->top = -1;
}
//出栈
int Pop (SqStack * S, DataType *e) /*S指向顺序栈指针,e出栈元素*/
{
if( S->top <= -1) /*栈为空*/
return 0;
*e= S->items[S->top]; /*将栈顶元素带回来*/
S->top--; /* 修改栈顶指针 */
return 1;
}
/* 主函数 */
int main()
{
int i;
SqStack stack; // 顺序栈
DataType ch;
Initstack(&stack);
for(i=0;i<STACKSIZE;i++)
{
ch = getchar();
if(ch=='\n')
break;
if(!Push(&stack,ch))
break;
}
if(Pop(&stack,&ch))
printf("%c",ch);
return 0;
}
/* 请在这里填写答案 */
### 输入样例:
在这里给出一组输入。例如:
in
asdfbdk
### 输出样例:
在这里给出相应的输出。例如:
out
k
答案:若无答案欢迎评论
### 函数接口定义:
c
int Push (SqStack * S, DataType e);
其中 S 和 e 都是用户传入的参数。 S 是指向顺序栈的指针; e 是字符变量。如果入栈成功,返回1,如果失败,返回0。
### 裁判测试程序样例:
c
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define STACKSIZE 100 // 宏定义,设栈最大容量为100
typedef char DataType; // 数据类型
/* 顺序栈 */
typedef struct // 顺序栈定义
{
DataType items[STACKSIZE];
int top; // top表示栈顶指针,取值范围-1—STACKSIZE-1
}SqStack;
/* 【 本题要求函数-入栈 】*/
int Push (SqStack * S, DataType e);
/*S为指向顺序栈的指针,e为待入栈的数据元素*/
/* 顺序栈初始化 */
void Initstack(SqStack *stack) // stack为指向顺序栈的指针
{
stack->top = -1;
}
//出栈
int Pop (SqStack * S, DataType *e) /*S指向顺序栈指针,e出栈元素*/
{
if( S->top <= -1) /*栈为空*/
return 0;
*e= S->items[S->top]; /*将栈顶元素带回来*/
S->top--; /* 修改栈顶指针 */
return 1;
}
/* 主函数 */
int main()
{
int i;
SqStack stack; // 顺序栈
DataType ch;
Initstack(&stack);
for(i=0;i<STACKSIZE;i++)
{
ch = getchar();
if(ch=='\n')
break;
if(!Push(&stack,ch))
break;
}
if(Pop(&stack,&ch))
printf("%c",ch);
return 0;
}
/* 请在这里填写答案 */
### 输入样例:
在这里给出一组输入。例如:
in
asdfbdk
### 输出样例:
在这里给出相应的输出。例如:
out
k
答案:若无答案欢迎评论