6-5 链栈的基本操作(10 分)
S是一个顺序栈,函数Push_L(LinkStack &S, SElemType e)是在链栈中插入一个元素e,函数Pop_L(LinkStack &S, SElemType &e)是删除链栈的栈顶元素(进行此操作是保证栈里有元素),函数StackEmpty_L(LinkStack &S)是判断栈是否为空,函数StackLength_L(LinkStack &S)返回栈内元素的个数,函数PrintStack_L(LinkStack S);是从栈顶到栈底依次属于元素的值。数据保证合法。。
函数接口定义:
void Push_L(LinkStack &S, SElemType e);
void Pop_L(LinkStack &S, SElemType &e);
bool StackEmpty_L(LinkStack &S);
int StackLength_L(LinkStack &S);
void PrintStack_L(LinkStack S);
其中 S 是链栈,e 元素。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int SElemType;
typedef struct SNode {
SElemType data;
struct SNode * next;
}SNode, *LinkStack;
void Push_L(LinkStack &S, SElemType e);
void Pop_L(LinkStack &S, SElemType &e);
bool StackEmpty_L(LinkStack &S);
int StackLength_L(LinkStack &S);
void PrintStack_L(LinkStack S);
Status InitStack_L(LinkStack &S) {
S = (LinkStack) malloc (sizeof(SNode));
if (S == NULL)
return OVERFLOW;
S->next = NULL;
return OK;
}
int main() {
LinkStack S;
if(InitStack_L(S) != OK) {
printf("InitStack_L: 初始化失败!!!\n");
return -1;
}
for(int i = 1; i <= 5; ++ i)
Push_L(S, i);
int OperationNumber;
scanf("%d", &OperationNumber);
while(OperationNumber --) {
int type;
scanf("%d", &type);
if(type == 1) {
int elem;
scanf("%d", &elem);
Push_L(S, elem);
} else if(type == 2) {
SElemType elem;
Pop_L(S, elem);
printf("%d\n", elem);
} else if(type == 3) {
if(StackEmpty_L(S)) puts("Empty!");
else puts("Not Empty");
} else if(type == 4) {
int len = StackLength_L(S);
printf("%d\n", len);
} else {
PrintStack_L(S);
}
}
return 0;
}
/* 你的代码将被嵌在这里 */
输入格式:
第一行输入一个整数OperationNumber,表示操作数,接下来OperationNumber行,每行表示一个操作(表示为“操作编号 操作内容”)。 编号为1表示入栈操作,后面一个参数表示插入的元素值 编号为2表示出栈操作 编号为3表示判断栈是否为空 编号为4表示查看栈内元素个数 编号为5表示从栈顶到栈底依次输出栈内元素
输出格式:
对于操作2,输出出栈的元素的值 对于操作3,输出”Empty!”或”Not Empty” 对于操作4,输出栈内元素个数 对于操作5,从栈顶到栈底依次输出栈内元素
输入样例:
6
1 2
1 3
2
3
4
5
输出样例:
3
Not Empty
6
2 5 4 3 2 1
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int SElemType;
typedef struct SNode {
SElemType data;
struct SNode * next;
}SNode, *LinkStack;
/*
S是一个顺序栈
函数Push_L(LinkStack &S, SElemType e)是在链栈中插入一个元素e
函数Pop_L(LinkStack &S, SElemType &e)是删除链栈的栈顶元素(进行此操作是保证栈里有'
元素
函数StackEmpty_L(LinkStack &S)是判断栈是否为空
函数StackLength_L(LinkStack &S)返回栈内元素的个数
函数PrintStack_L(LinkStack S)是从栈顶到栈底依次属于元素的值。数据保证合法。。
编号为1表示入栈操作,后面一个参数表示插入的元素值
编号为2表示出栈操作
编号为3表示判断栈是否为空
编号为4表示查看栈内元素个数
编号为5表示从栈顶到栈底依次输出栈内元素
*/
void Push_L(LinkStack &S, SElemType e)
{
LinkStack p = (struct SNode*)malloc(sizeof(struct SNode));
p->data = e;
p->next = S->next;
S->next = p;
}
void Pop_L(LinkStack &S, SElemType &e)
{
LinkStack p;
p = S->next;
int num = (p->data);
S->next = p->next;
free(p);
e = num;
}
bool StackEmpty_L(LinkStack &S)
{
if (S->next == NULL)
return 1;
return 0;
}
int StackLength_L(LinkStack &S)
{
int cnt = 0;
LinkStack p = S;//不能直接访问
while (p->next)
{
cnt++;
p =p->next;
}
return cnt;
}
void PrintStack_L(LinkStack S)
{
S = S->next;
while (S)
{
printf("%d ", S->data);
S = S->next;
}
puts("");
}
Status InitStack_L(LinkStack &S)
{
S = (LinkStack)malloc(sizeof(SNode));
if (S == NULL)
return OVERFLOW;
S->next = NULL;
return OK;
}
int main() {
LinkStack S;
if (InitStack_L(S) != OK) {
printf("InitStack_L: 初始化失败!!!\n");
return -1;
}
for (int i = 1; i <= 5; ++i)
Push_L(S, i);
int OperationNumber;
scanf("%d", &OperationNumber);
while (OperationNumber--) {
int type;
scanf("%d", &type);
if (type == 1) {
int elem;
scanf("%d", &elem);
Push_L(S, elem);
}
else if (type == 2) {
SElemType elem;
Pop_L(S, elem);
printf("%d\n", elem);
}
else if (type == 3) {
if (StackEmpty_L(S)) puts("Empty!");
else puts("Not Empty");
}
else if (type == 4) {
int len = StackLength_L(S);
printf("%d\n", len);
}
else {
/*S->next;
while (S!=NULL)
{
printf("*%d\n",S->data);
S = S->next;
}*/
PrintStack_L(S);
}
}
return 0;
}