链栈的主要操作
2017-07-28 01:26
113 查看
#ifndef _ERROR_H //error.h 头文件 #define _ERROR_H #include <stdio.h> #define ERROR -1 #define FULL_ERROR -2 #define EMPTY_ERROR -3 #define MALLOC_ERROR -4 int errno; //错误号 void myerror(char *str); char *mystrerror(int num); #endif //error.c文件 #include "error.h" void myerror(char * str) { switch(errno) { case -1: printf("%s:输入的参数错误\n",str); break; case FULL_ERROR: printf("%s:满栈状态\n",str); break; case EMPTY_ERROR: printf("%s:空栈状态\n",str); break; case MALLOC_ERROR: printf("%s: 创建失败\n",str); break; } } char *mystrerror(int num) { switch (errno) { case ERROR: return "输入参数错误"; case FULL_ERROR: return "满栈状态"; case EMPTY_ERROR: return "空栈状态"; case MALLOC_ERROR: printf("s: 创建失败"); break; } } //linkstack.h链栈头文件 #ifndef _linkstack_h_ #define _linkstack_h_ #include <stdio.h> #include "error.h" #define true 1 #define false 0 typedef int stackdata; typedef struct _node { stackdata data; struct _node *next; }Node; typedef struct _linkstack { Node *top; }linkstack; linkstack * create_stack(); // 创建栈 int stackempty(linkstack *s); //是否空栈 int push(linkstack *s,stackdata x);//进栈 int pop(linkstack *s,stackdata *x);//出栈 int get_top(linkstack *s,stackdata *x);//获取栈顶元素 int destroy(linkstack *s);//销毁链栈 #endif //linkstack.c源代码 #include <stdlib.h> #include "linkstack.h" linkstack * create_stack() //创建栈 { linkstack *s = (linkstack *)malloc(sizeof(linkstack) / sizeof(char)); if(s == NULL) { errno = MALLOC_ERROR; return NULL; } s->top = NULL; return s; } int stackempty (linkstack *s) //是否空栈 { if(s == NULL) { errno = ERROR; return false; } return s->top == NULL; } int push(linkstack *s,stackdata x)//进栈 { if(s == NULL) { errno = ERROR; return false; } Node *node = (Node *)malloc(sizeof(Node) / sizeof(char)); if(node == NULL) { errno = MALLOC_ERROR; return false; } node->data = x; node->next = s->top; s->top = node; return true; } int pop(linkstack *s,stackdata *x)//出栈 { if(s == NULL) { errno = ERROR; return false; } if(stackempty(s)) { errno = EMPTY_ERROR; return false; } Node *p = s->top; *x = p->data; s->top = p->next; free(p); return true; } int get_top(linkstack *s,stackdata *x)//获取栈顶元素 { if (s == NULL) { errno = ERROR; return false; } if (stackempty(s)) { errno = EMPTY_ERROR; return false; } *x = s->top->data; return true; } int destroy(linkstack *s)//销毁链栈 { if (s == NULL) { errno = ERROR; return false; } int x; while(stackempty(s) != true) //不是空栈继续出栈 { pop(s,&x); } free(s); return true; } //主函数main.c #include <stdio.h> #include "linkstack.h" int main() { linkstack *s = create_stack(); if(s == NULL) { return -1; } int i; int x; for(i = 0;i < 10;i++) { push(s,i); } char str[100]; for(i = 1;i < 15;i++) { if(pop (s,&x) != true) { sprintf(str,"pop第%d个元素",i); myerror(str); } printf("x: %d\n",x); } if(stackempty(s)) { printf("空栈\n"); } if(pop(s,&x) != true) { myerror("pop错误"); } if(destroy(s) != true) { myerror("destroy"); } return 0; }
相关文章推荐
- 14.线性表之链栈的基本操作
- ASP通过XMLDom在服务器端操作XML文件的主要方法和实现。
- 链栈的基本操作
- 链栈的相关操作
- [PPT-知识竞赛流程方案策划]知识竞赛现场主持人台词与系统管理员后台操作主要配合流程示范
- 数据结构_链栈的建立与相关操作_C语言源代码
- 链表,顺序表,链队,链栈,顺序队,顺序栈,二叉树的基本操作函数
- 顺序栈、链栈基本操作
- spring 基本操作总结主要是aop以及依赖注入的基本配置
- redis练习手册<十>hash主要操作函数小结
- 对xml操作的主要方法
- string 类主要函数操作笔记
- wp修改告一段多,主要完成以下修改(操作)
- Redis系列-存储篇hash主要操作函数小结
- 数据结构中栈的链式结构—链栈的基本操作
- Redis系列-存储篇list主要操作函数小结
- B+树的实现,主要讲解删除操作
- bo3-1-1.c 链栈(存储结构由c2-2.h定义)的基本操作(4个) 及验证
- memcached哈希表操作主要逻辑笔记