第五周项目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_INCLUDED  
linstack.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
原创粉丝点击