第五周项目2-建立链栈算法库
来源:互联网 发布:旋流器计算软件 编辑:程序博客网 时间:2024/06/09 07:31
/* * Copyright(c) 2016, 烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称: * 作 者:张相如 * 完成日期:2016年 10月 13日 * 版 本 号:v1.0 * * 问题描述: 定义链栈存储结构,实现其基本运算,并完成测试。 * 输入描述:无 * 程序输出:main函数 */
问题及代码:
listack.h:
#ifndef LISTACK_H_INCLUDED #define LISTACK_H_INCLUDED typedef char ElemType; typedef struct linknode { ElemType data; //数据域 struct linknode *next; //指针域 } LiStack; //链栈类型定义 void InitStack(LiStack *&s); //初始化栈 void DestroyStack(LiStack *&s); //销毁栈 int StackLength(LiStack *s); //返回栈长度 bool StackEmpty(LiStack *s); //判断栈是否为空 void Push(LiStack *&s,ElemType e); //入栈 bool Pop(LiStack *&s,ElemType &e); //出栈 bool GetTop(LiStack *s,ElemType &e); //取栈顶元素 void DispStack(LiStack *s); //输出栈中元素 #endif // LISTACK_H_INCLUDEDlinstack.cpp:
#include <stdio.h> #include <malloc.h> #include "listack.h" void InitStack(LiStack *&s) //初始化栈 { s=(LiStack *)malloc(sizeof(LiStack)); s->next=NULL; } void DestroyStack(LiStack *&s) //销毁栈 { LiStack *p=s->next; while (p!=NULL) { free(s); s=p; p=p->next; } free(s); //s指向尾结点,释放其空间 } int StackLength(LiStack *s) //返回栈长度 { int i=0; LiStack *p; p=s->next; while (p!=NULL) { i++; p=p->next; } return(i); } bool StackEmpty(LiStack *s) //判断栈是否为空 { return(s->next==NULL); } void Push(LiStack *&s,ElemType e) //入栈 { LiStack *p; p=(LiStack *)malloc(sizeof(LiStack)); p->data=e; //新建元素e对应的节点*p p->next=s->next; //插入*p节点作为开始节点 s->next=p; } bool Pop(LiStack *&s,ElemType &e) //出栈 { LiStack *p; if (s->next==NULL) //栈空的情况 return false; p=s->next; //p指向开始节点 e=p->data; s->next=p->next; //删除*p节点 free(p); //释放*p节点 return true; } bool GetTop(LiStack *s,ElemType &e) //取栈顶元素 { if (s->next==NULL) //栈空的情况 return false; e=s->next->data; return true; } void DispStack(LiStack *s) //输出栈中元素 { LiStack *p=s->next; while (p!=NULL) { printf("%c ",p->data); p=p->next; } printf("\n"); }
main.cpp:
#include <stdio.h> #include "listack.h" int main() { ElemType e; LiStack *s; printf("初始化链栈s\n"); InitStack(s); if(StackEmpty(s)) printf("该链栈是空栈\n"); else printf("该链栈不是空栈\n"); printf("依次进链栈元素a,b,c,d,e\n"); Push(s,'a'); Push(s,'b'); Push(s,'c'); Push(s,'d'); Push(s,'e'); if(StackEmpty(s)) printf("该链栈是空栈\n"); else printf("该链栈不是空栈\n"); printf("链栈长度为:%d\n",StackLength(s)); printf("从栈顶到栈底元素为:");DispStack(s); printf("链栈出栈序列为:"); while (!StackEmpty(s)) { Pop(s,e); printf("%c ",e); } printf("\n"); if(StackEmpty(s)) printf("该链栈是空栈\n"); else printf("该链栈不是空栈\n"); DestroyStack(s); printf("链栈已被释放\n"); return 0; }
运行结果:
知识点总结:
多文件形式建立算法库
0 0
- 第五周项目2--建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周 项目2-建立链栈算法库
- 第五周项目2 -建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周 项目2-建立链栈算法库
- 第五周项目2 -建立链栈算法库
- 第五周 项目2 - 建立链栈算法库
- 第五周--项目2-建立链栈算法库
- 第五周 项目2 - 建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2-建立链栈算法库
- 第五周项目2--建立链栈算法库
- 第五周项目2 --建立链栈算法库
- 第五周项目2—建立链栈算法库
- 第五周--项目2 建立链栈算法库
- 题目1024:畅通工程
- requirejs的用法
- 使用maven的mybatis-generator生成实例和mapping过程以及可能出现问题解决
- redsi 内存优化和存储
- 第七周 项目3 负数把正数赶出队列
- 第五周项目2-建立链栈算法库
- 65. Valid Number
- 什么是范数-学习
- 如何在Android Studio中实现不区分大小写
- 微信WEB开发者工具
- 基于summernote的富文本编辑器,粘贴时去除word格式
- RiverOfAutumn
- liunx32位和64为数据类型对比
- 一位在MIT教数学的老师总结了十条经验