栈及链栈常用函数
来源:互联网 发布:上海同济大学网络教育 编辑:程序博客网 时间:2024/05/31 06:22
头文件:
#ifndef _LINKSTACK_H_#define _LINKSTACK_H_#define FAILURE 1000000#define SUCCESS 1000001typedef int ElemType;struct node { ElemType data; struct node *next;};typedef struct node Node;typedef Node *LinkNode;struct stack{ LinkNode top; int count;};typedef struct stack Stack;#endif
利用栈排序:
#include <stdio.h>#include "LinkStack.h"#define MAXSIZE 10int main(){ int ret,i; int a[MAXSIZE] = {0}; Stack *num,*tmp; if(StackInit(&num) != SUCCESS || StackInit(&tmp) != SUCCESS) { printf("Init Failure!\n"); } printf("Please Input 10 Numbers :\n"); for(i = 0; i < MAXSIZE; i++) { scanf("%d",&a[i]); } for(i = 0; i < MAXSIZE; i++) { if(StackLength(num) == 0) { PUSH(num,a[i]); } else { while (a[i] < GetTop(num) && StackLength(num) != 0 ) { PUSH(tmp,Pop(num)); } PUSH(num,a[i]); while(StackLength(tmp) != 0) { PUSH(num,Pop(tmp)); } } } for(i = 0; i < MAXSIZE; i ++) { printf("%d ",Pop(num)); } printf("\n"); return 0;}
链栈子函数及主函数:
#include <stdio.h>#include "LinkStack.h"#include <stdlib.h>int StackInit(Stack **S){ (*S) = (Stack *)malloc(sizeof(Node)); if(NULL == (*S)) { return FAILURE; } (*S)->top = NULL; (*S)->count = 0; return SUCCESS;}int PUSH(Stack *S,ElemType e){ Node *p = (Node *)malloc(sizeof(Node)); if(NULL == p) { return FAILURE; } p->data = e; p->next = S->top; S->top = p; S->count++; return SUCCESS;}int StackLength(Stack *S){ return (S->count);}int GetTop(Stack *S){ if(NULL == S->top) { return FAILURE; } // return SUCCESS; return (S->top->data);}int Pop(Stack *S){ int e; if(NULL == S->top) { return FAILURE; } LinkNode p = S->top; S->top = p->next; e = p->data; free(p); S->count--; return e;}int StackClear(Stack *S){ LinkNode p; while (S->top) { p = S->top; S->top = p->next; free(p); S->count--; } return SUCCESS;}int Destroy(Stack **S){ if(NULL == (*S)) { return FAILURE; } free(*S); *S = NULL; return SUCCESS;}
#include <stdio.h>#include "LinkStack.h"int main(){ int ret,i; Stack *s; ElemType e; ret = StackInit(&s); if(FAILURE == ret) { printf("Init Failure!\n"); } else { printf("Init Success!\n"); } for(i = 0; i < 5; i++) { e = i + 1; ret = PUSH(s,e); if(FAILURE == ret) { printf("Push Failure!\n"); } else { printf("Push %d Success!\n",e); } } printf("Length is %d!\n",StackLength(s)); ret = GetTop(s,&e); if(FAILURE == ret) { printf("GetTop Failure!\n"); } else { printf("Top is %d!\n",e); } for(i = 0; i < 2; i++) { ret = Pop(s,&e); if(FAILURE == ret) { printf("Pop Failure!\n"); } else printf("Pop %d Success!\n",e); } ret = StackClear(s); if(FAILURE == ret) { printf("Clear Failure!\n"); } else { printf("Clear Success!\n"); } ret = GetTop(s,&e); if(FAILURE == ret) { printf("GetTop Failure!\n"); } else { printf("Top is %d!\n",e); } ret = Destroy(&s); if(FAILURE == ret) { printf("Destroy Failure!\n"); } else { printf("Destroy Success!\n"); } printf("%p\n",s); return 0;}
阅读全文
0 0
- 栈及链栈常用函数
- 常用函数集及功能
- delphi 常用函数及方法
- VBA常用函数及记事
- javascript常用函数及语句
- execle 常用函数及功能
- JQuery常用函数及功能
- VBS常用函数及功能
- matlab常用函数及功能
- jQuery常用函数及功能
- mysql常用函数及应用
- BIEE11G常用函数及使用说明
- jQuery 常用方法及函数
- js 数据类型及常用函数
- 常用数学类型及函数
- GDAL常用函数及示例
- VBS常用函数及功能
- MySql常用函数及命令
- KindEditor-编辑器设置图标的显示隐藏
- model.py下的Meta笔记
- mac搭建python环境及tensoflow
- springboot源码分析6-springboot之PropertySource类初探
- 命令行编译C语言Hello World的过程
- 栈及链栈常用函数
- mybatis在Mapper.xml文件中使用大于,小于时要用 > <
- 从B树、B+树、B*树谈到R 树
- C# aspx获取web.config中的key值
- 自定义View的视图刷新问题
- 数理逻辑习题集(6)
- Spring构建Web应用部署至Tomcat开发过程中的数据编码问题
- 手指在屏幕上滑动,红色的小球始终跟随手指移动。
- sql 查询一列和另一列的关系是不是一对一