定义一个字符型链栈。
- 编写以下操作函数
- 初始化栈:函数名InitlinkStack();
- 判别栈空:函数名IsEmptyStack();
- 进栈:函数名Push();
- 出栈:函数名Pop;
- 显示栈顶元素值:函数名DispStackTop();
- 编写主函数:显示菜单,并可根据菜单命令调用相应操作函数。
- 要求:
- 不允许使用typedef char Datatype语句定义类型,即程序中不能出现Datatype。
- 函数名只能使用已给定函数名。
- 程序编译通过,给出操作结果。
提示:
- C语言是面向过程的,所以函数的作用域(可被调用的范围)从函数到程序的末尾。如果要在整个程序中都可调用某个函数,只需进行 函数声明(函数方法头加上符号“;”。形式参数可省略);
- 对于指针(地址)变量,可通过(类型 *)malloc(sizeof(类型))函数分配一个地址,sizeof(类型)返回类型所需的大小;
- 可用free(指针变量)函数释放指针变量指向的地址空间;
- 栈是一种操作受限的线性表,特点是“后进先出”。但可分多种情况,例如:ABCD顺序进入的栈,有全进后再出的情况,有进三个后再出的情况,有进两个后再出的情况,有进一个后再出的情况。区分方法是:全进时先出的是最后一个元素,进三个时先出的是第三个元素,以此类推。
- 栈的入栈方法有两种:头插法(后进先出)和尾插法(先进先出)。本程序用的是头插法;
#include#include struct linkStack{ char c; struct linkStack *next; }; linkStack *InitlinkStack(); int IsEmptyStack(struct linkStack *stack); void Push(struct linkStack *stack); void Pop(struct linkStack *stack); void DispStackTop(struct linkStack *stack); void PrintIsEmptyStack(struct linkStack *stack); void PrintStack(struct linkStack *stack); void menu(){ printf("n 链栈的各种操作"); printf("n=================================="); printf("n| 1--初始化栈 |"); printf("n| 2--判断栈空 |"); printf("n| 3--入栈 |"); printf("n| 4--出栈 |"); printf("n| 5--得到栈顶元素 |"); printf("n| 6--输出栈 |"); printf("n| 0--退出 |"); printf("n=================================="); printf("n输入选择(0~6):"); } //初始化栈 linkStack *InitlinkStack(){ struct linkStack *stack; //使用malloc()函数动态分配存储空间 stack = (struct linkStack *)malloc(sizeof(struct linkStack)); stack->next = NULL; printf("初始化栈成功n"); return stack; } //判断栈是否空 int IsEmptyStack(struct linkStack *stack){ if(stack->next == NULL) return 1; //如果栈空,返回1,否则返回0 else return 0; } //入栈操作 void Push(struct linkStack *stack){ char ch; struct linkStack *s; printf("输入要入栈的字符:"); ch = getchar(); getchar(); //吸收换行符,防止出现错误 s = (struct linkStack *)malloc(sizeof(struct linkStack)); s->c = ch; //把字符ch赋给结构体s的变量c s->next = stack->next; //使新结点的指针指向头结点的指针 stack->next = s; //使头结点的指针指向新结点s } //出栈操作 void Pop(struct linkStack *stack){ struct linkStack *s; char ch; if(stack->next == NULL){ printf("当前栈已空,无法出栈!n"); exit(1); } s = stack->next; //s指向头结点的指针 ch = s->c; //s的,也就是原头结点指针的变量c赋给ch stack->next = s->next; //头结点指向自己之后的一个结点 free(s); //释放指针变量s指向的地址空间 printf("出栈成功,出栈元素为%2cn",ch); } //得到栈顶元素 void DispStackTop(struct linkStack *stack){ printf("当前栈顶元素为%2cn",stack->next->c); } //输出栈是否为空 void PrintIsEmptyStack(struct linkStack *stack){ if(IsEmptyStack(stack)) printf("当前栈为空n"); else printf("当前栈不为空n"); } //输出栈 void PrintStack(struct linkStack *stack){ struct linkStack *s = stack->next; printf("当前栈为:n"); while(s != NULL){ printf("%3c",s->c); s = s->next; } putchar('n'); } void main(){ struct linkStack *stack; char char1 = 'y',char2; do{ menu(); char2 = getchar(); getchar(); while(char1 == 'y' || char1 == 'Y'){ switch(char2){ case '1': stack = InitlinkStack();break; case '2': PrintIsEmptyStack(stack);break; case '3': Push(stack);break; case '4': Pop(stack);break; case '5': DispStackTop(stack);break; case '6': PrintStack(stack);break; case '0': char1 = 'n';break; default:printf("选择有误,范围应为0~6!n"); } char1 = 'n'; if(char2=='3' || char2=='4'){ printf("是否继续(y/n):"); char1 = getchar(); getchar(); } } char1 = 'y'; if(char2 != '0'){ printf("按回车键继续,按任意键退出..."); char2 = getchar(); if(char2 != 'n'){ getchar(); char1 = 'n'; } }else char1 = 'n'; }while(char1 == 'Y' || char1 == 'y'); printf("n操作结束n"); system("pause"); //输出程序末尾出现的英文的中文翻译 }



