原题链接数据结构-行编辑程序 - C语言网 (dotcpp.com)
#include <stdio.h>
#include <malloc.h>
#define Elemetype char
typedef struct SNode{
int Data[1000];
int Top;
}*Stack,SNode;
void Push (Stack,Elemetype);
int Pop(Stack,Elemetype*);
int TopStack(Stack);
void ClearStack(Stack);
int main(){
//置空栈
Stack PtrS;
PtrS=(Stack)malloc(sizeof(SNode));
PtrS->Top=-1;
char ch,a;
int i;
//读入一个字符
ch=getchar();
while (ch != EOF){
while (ch != EOF && ch!='\n'){
switch (ch){
//如果是#,就Pop出最顶上的元素
case '#':
Pop(PtrS,&a); break;
//如果是@,就清空当前的堆栈
case '@':
ClearStack(PtrS); break;
//默认情况下,将当前的字符放入堆栈
default:
Push(PtrS,ch);break;
}
//读入下一个字符
ch=getchar();
}
//遍历堆栈并输出
for (i=0;i<=PtrS->Top;i++){
printf("%c",PtrS->Data[i]);
}
//清空当前的堆栈,用于下一行的使用
ClearStack(PtrS);
printf("\n");
//读入下一行的字符,进行循环处理
if (ch != EOF){
ch=getchar();
}
}
return 0;
}
void Push (Stack PtrS, Elemetype item){
if (PtrS->Top == 1000-1){
printf("堆栈满");
return;
}
else {
PtrS->Data[++(PtrS->Top)]=item;
return;
}
}
int Pop(Stack PtrS, Elemetype* locate){
if (PtrS->Top == -1){
printf("栈空");
return 0;
}
else{
*locate=PtrS->Data[PtrS->Top];
PtrS->Top=(PtrS->Top)-1;
return 1;
}
}
int TopStack(Stack PtrS){
Elemetype e;
e=PtrS->Data[PtrS->Top];
return e;
}
void ClearStack (Stack PtrS){
Elemetype t;
while (PtrS->Top !=-1){
Pop(PtrS,&t);
}
}