题目描述
拷贝下面的代码,按要求完成链栈的Push,Pop,StackEmpty和GetTop操作,其他地方不得改动。
/***链栈的实现***/
#include<iostream>
#include<fstream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW - 2
typedef int Status;
typedef int SElemType;
typedef struct StackNode {
SElemType data;
struct StackNode *next;
} StackNode, *LinkStack;
//算法3.5 链栈的初始化
Status InitStack(LinkStack &s) // 构造一个空栈 S,栈顶指针置空
{
s = NULL;
return OK;
}
void DestroyStack(LinkStack &s)
{
LinkStack next;
while(s){
next = s->next;
delete s;
s = next;
}
}
//算法3.6 链栈的入栈
Status Push(LinkStack &s, SElemType e) //在栈顶插入元素e
{
/****在此下面完成代码***************/
/***********************************/
}
//算法3.7 链栈的出栈
Status Pop(LinkStack &s, SElemType &e) //删除S的栈顶元素,用e返回其值
{
/****在此下面完成代码***************/
/***********************************/
}
//算法3.8 取链栈的栈顶元素
SElemType GetTop(LinkStack s) //返回S的栈顶元素,不修改栈顶指针
{
/****在此下面完成代码***************/
/***********************************/
}
bool StackEmpty(LinkStack s)
{
/****在此下面完成代码***************/
/***********************************/
}
void PrintStack(LinkStack s)
{
for(LinkStack p = s; p; p = p->next) {
cout << p->data;
if(p->next)
cout << ' ';
}
cout << endl;
}
int main()
{
LinkStack s;
SElemType e;
string op;
InitStack(s);
while(cin >> op) {
if(op == "Push") {
cin >> e;
if(Push(s, e) == ERROR)
cout << "Push failure\n";
else
PrintStack(s);
} else if(op == "Pop") {
if(Pop(s, e) == ERROR)
cout << "Pop failure\n";
else
cout << e << " is poped\n";
} else if(op == "Empty") {
cout << "Stack is " << (StackEmpty(s) ? "empty" : "not empty") << endl;
} else if(op == "GetTop") {
if(StackEmpty(s)) {
cout << "GetTop failure\n";
} else
cout << "The top element is " << GetTop(s) << endl;
}
}
DestroyStack(s);
return 0;
}
输入
为链栈的操作系列,每个操作一行,具体见样例。
输出
如果输入为"Push e", 栈已满输出"Push failure",否则将e入栈后再输出自栈顶到栈底的所有元素。
如果输入为"Pop", 栈为空输出"Pop failure",否则弹出并输出栈顶的元素。
如果输入为"Empty",则输出栈是否为空。
如果输入为"GetTop",栈为空则输出"GetTop failure",否则输出栈顶的元素。
具体见样例。
样例输入 Copy
Empty GetTop Pop Push 1 Push 2 Push 3 Empty GetTop Pop Push 4 Push 5 Push 6 Push 7 Pop
样例输出 Copy
Stack is empty GetTop failure Pop failure 1 2 1 3 2 1 Stack is not empty The top element is 3 3 is poped 4 2 1 5 4 2 1 6 5 4 2 1 7 6 5 4 2 1 7 is poped
/***链栈的实现***/
#include<iostream>
#include<fstream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW - 2
typedef int Status;
typedef int SElemType;
typedef struct StackNode {
SElemType data;
struct StackNode *next;
} StackNode, *LinkStack;
//算法3.5 链栈的初始化
Status InitStack(LinkStack &s) // 构造一个空栈 S,栈顶指针置空
{
s = NULL;
return OK;
}
void DestroyStack(LinkStack &s)
{
LinkStack next;
while(s){
next = s->next;
delete s;
s = next;
}
}
//算法3.6 链栈的入栈
Status Push(LinkStack &s, SElemType e) //在栈顶插入元素e
{
/****在此下面完成代码***************/
LinkStack p;
p=new StackNode;
p->data=e;
p->next=s;
s=p;
return OK;
/***********************************/
}
//算法3.7 链栈的出栈
Status Pop(LinkStack &s, SElemType &e) //删除S的栈顶元素,用e返回其值
{
/****在此下面完成代码***************/
if(s==NULL){
return ERROR;
}
LinkStack p;
e=s->data;
p=s;
s=s->next;
delete p;
return OK;
/***********************************/
}
//算法3.8 取链栈的栈顶元素
SElemType GetTop(LinkStack s) //返回S的栈顶元素,不修改栈顶指针
{
/****在此下面完成代码***************/
if(s!=NULL){
return s->data;
}
/***********************************/
}
bool StackEmpty(LinkStack s)
{
/****在此下面完成代码***************/
if(s==NULL)return OK;
else return ERROR;
/***********************************/
}
void PrintStack(LinkStack s)
{
for(LinkStack p = s; p; p = p->next) {
cout << p->data;-
if(p->next)
cout << ' ';
}
cout << endl;
}
int main()
{
LinkStack s;
SElemType e;
string op;
InitStack(s);
while(cin >> op) {
if(op == "Push") {
cin >> e;
if(Push(s, e) == ERROR)
cout << "Push failure\n";
else
PrintStack(s);
} else if(op == "Pop") {
if(Pop(s, e) == ERROR)
cout << "Pop failure\n";
else
cout << e << " is poped\n";
} else if(op == "Empty") {
cout << "Stack is " << (StackEmpty(s) ? "empty" : "not empty") << endl;
} else if(op == "GetTop") {
if(StackEmpty(s)) {
cout << "GetTop failure\n";
} else
cout << "The top element is " << GetTop(s) << endl;
}
}
DestroyStack(s);
return 0;
}