利用链栈的基本运算,通过输入将字符进栈,然后输出其出栈序列
2015-02-07 20:30
375 查看
头文件:函数的定义
函数的声明
函数的应用
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> typedef char ElemType; typedef struct node { ElemType data; struct node *next; }LStackNode,*LinkStack; void InitStack(LinkStack *top);//将链栈初始化 int StackEmpty(LinkStack top);//判断链栈是否为空 int GetTop(LinkStack top,ElemType *e);//取栈顶元素 int PushStack(LinkStack top,ElemType e);//进栈操作 int PopStack(LinkStack top,ElemType *e);//出栈操作 int StackLength(LinkStack top);//求表长操作 void DestroyStack(LinkStack top);//销毁链表
函数的声明
#include "链式栈.h" void InitStack(LinkStack *top)//将链栈初始化 { if((*top = (LinkStack)malloc(sizeof(LStackNode)))== NULL) { exit(-1); } (*top)->next = NULL; } int StackEmpty(LinkStack top)//判断链栈是否为空 { if(top->next == NULL) { return 1; } else { return 0; } } int GetTop(LinkStack top,ElemType *e)//取栈顶元素 { LStackNode *p; p = top->next ; if(!p) { printf("栈已空!"); return 0; } *e = p->data ; return 1; } int PushStack(LinkStack top,ElemType e)//进栈操作 { LStackNode *p; if((p = (LinkStack)malloc(sizeof(LStackNode))) == NULL) { printf("内存分配失败!"); return 0; } p->data = e; p->next = top->next; top->next = p; return 1; } int PopStack(LinkStack top,ElemType *e)//出栈操作 { LStackNode *p; p = top->next ; if(!p) { printf("栈已空!"); return 0; } top->next = p->next ; *e = p->data ; free(p); return 1; } int StackLength(LinkStack top)//求表长操作 { LStackNode *p; int count = 0; p = top; while(p->next != NULL) { p = p->next ; count++; } return count; } void DestroyStack(LinkStack top)//销毁链表 { LStackNode *p,*q; p = top; while(!p) { q = p; p = p->next ; free(q); } }
函数的应用
#include "链式栈.h" //利用链栈的基本运算,通过输入将字符进栈,然后输出其出栈序列 int main(void) { LinkStack S; ElemType ch[50],e,*p; InitStack(&S); printf("请输入进栈的字符:\n"); gets(ch); p = &ch[0]; while(*p) { PushStack(S,*p); p++; } printf("当前栈顶元素为:"); if(GetTop(S,&e) == 0) { printf("栈已空!"); return 0; } else { printf("%4c\n",e); } printf("当前栈的元素个数是:%d\n",StackLength(S)); printf("元素出栈的序列是:"); while(!StackEmpty(S)) { PopStack(S,&e); printf("%4c",e); } printf("\n"); return 0; }
相关文章推荐
- 利用”异或”运算的性质,对几个字符进行加密并输出密文,然后再解密。加密算法是:密钥是字符’8’,明文的每个字符和密钥进行异或运算,得到密文。密钥和密文的每个字符再次进行异或运算,重新得到明文。
- 利用栈的基本操作编写一个行编辑程序,当前一个字符有误时,输入#消除,当前面一行有误时,输入@消除前面行的字符序列
- 试利用栈的基本操作编写一个行编辑程序,当前一个字符有误时,输入#消除,当前面一行有误时,输入@消除前面行的字符序列
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 单链表应用举例(单链表A和单链表B的元素都是非递减排列,利用单链表的基本运算,将它们合并成一个单链表C,要求C也是非递减序列)
- 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 3.2 设计一个程序,要求用户输入 5 个字符,然后将这 5 个字符按相反的顺序显示出 来。例如,假设输入 abcde,则输出为 edcba
- 通过键盘输入一个字符串,判断字符串中出现最多的字符并输出该字符和输出出现次数
- 输入十个字符(可能是数字也可能是字母)然后输出
- 【IO流】IO流基本定义及最简单的实例输入,输出总结【主要是针对字符流】
- java 实现接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写
- 定义一个不受计算机字长限制的整数类INT,要求INT与INT以及INT与C++基本数据类型int之间能进行+、-、×、÷和=运算,并且能通过cout输出INT类型的值。(持续添加)
- 华为机试通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- 第二周第三天([输入一个字符串,然后输出字符串的运算结果] [object][异常][IO流,读取D盘某个文件])
- c语言: 键盘输入一个n个字符的串,然后按字母表顺序输出这n个字符
- 一个班有n个学生,需要把每个学生的简单资料(姓名和学号)输入计算机保存。然后可以通过输入某一学生的姓名查找其有关资料。当输入一个姓名后程序就查找该班有无此学生,如果有,则输出他的姓名和学号,如果找不到
- 通过键盘输入100 以内正整数的加、减运算式,请编写一个程序输出运算结果字符串
- (华为机考题)通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
- 80x86汇编编程:把一串字符从键盘输入,然后逆序输出