链表栈-》与数组栈同接口-》更确切地说是单项链表的接口封装
2014-04-10 00:00
225 查看
摘要: 单项链表的接口封装,单项链表程序就不copy上来了
/************************************************************************* > File Name: stack.h > Author: Palmer XU > Mail: > Created Time: Wed 09 Apr 2014 09:39:55 PM EDT ************************************************************************/
#ifndef __STACK_H__ #define __STACK_H__
#include"single.h"
typedef struct stack { pnode_t head; }stack_t,*pstack_t;
int creat_stack(pstack_t*);
int stack_not_empty(pstack_t);
int push(pstack_t,int);
int pop(pstack_t,int*);
int top(pstack_t,int*);
int clear_stack(pstack_t);
int count_size(pstack_t);
int destory_stack(pstack_t*);
#endif /************************************************************************* > File Name: stack.c > Author: Palmer XU > Mail: Palmer.Xu.One@Gmail.com > Created Time: Wed 09 Apr 2014 10:55:25 PM EDT ************************************************************************/ #include<stdio.h> #include"stack.h" int main(void) { pstack_t p ; creat_stack(&p); printf("%d\n",stack_not_empty(p)); int i = 1; for(;i <= 3;i++) push(p,i); pop(p,&i); printf("%d\n",i); top(p,&i); printf("%d\n",i); printf("%d\n",stack_not_empty(p)); printf("%d\n",count_size(p)); clear_stack(p); printf("%d\n",stack_not_empty(p)); printf("%d\n",count_size(p)); pop(p,&i); i = destory_stack(&p); printf("%d\n",i); printf("%d\n",count_size(p)); return 0; } /************************************************************************* > File Name: stack_fun.c > Author: Palmer XU > Mail: Palmer.Xu.One@Gmail.com > Created Time: Wed 09 Apr 2014 10:06:42 PM EDT ************************************************************************/ #include"stack.h" #include<stdlib.h> int creat_stack(pstack_t *p_temp) { * 7fe0 p_temp = (pstack_t)malloc(sizeof(stack_t)); pstack_t p = *p_temp; if(NULL != p && NULL != (p->head = creat_list())) { return 0; } printf("error- -creat_stack fail\n"); free(p); *p_temp = NULL; return -1; } int stack_not_empty(pstack_t p) { if(NULL == p) { printf("error\n"); return -1; } if(NULL != p->head->next) return 0; else return -1; } int push(pstack_t p,int data) { if(NULL == p) { printf("error\n"); return 0; } return insert_after(p->head,data); } int pop(pstack_t p,int *data) { if(NULL == p) { printf("error\n"); return -1; } if(NULL != p->head->next) *data = p->head->next->data; return delete_after(p->head); } int top(pstack_t p,int* data) { if(NULL == p || NULL == p->head->next) { printf("error\n"); return -1; } *data = p->head->next->data; return 0; } int clear_stack(pstack_t p) { if(NULL == p) { printf("error\n"); return -1; } return destory_after(p->head); } int count_size(pstack_t p) { if(NULL == p) { printf("error\n"); return -1; } return count_size_list(p->head); } int destory_stack(pstack_t *p_flag) { pstack_t p = *p_flag; if(NULL == p) { printf("error\n"); return -1; } p->head = destory_all(p->head); free(p); *p_flag = NULL; return 0; }
相关文章推荐
- 【C/C++学院】0828-数组与指针/内存分配/数据结构数组接口与封装
- 数据结构数组接口和封装
- 链表接口的封装
- 学生类封装 , (链表)插入排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
- 20171124动态数组接口封装完成
- 可变数组的封装/链表
- python3 web接口 字典里边嵌字典和数组字典的封装
- 【C/C++学院】0828-数组与指针/内存分配/数据结构数组接口与封装
- HashMap集合源码以及底层结构解析(何时数组+单项链表变为数组+红黑二叉树)
- C# 数组集合<二> 双向链表和双向循环链表
- 强大的JQuery数组封装使用
- 基于链表、数组实现队列、循环队列
- jni封装ffmpeg接口遇到的错误修改方法
- Java基础(一):Java面向对象、面向对象封装、抽象类、接口、static、final
- 关于北京朋友的驰骋工作流引擎的是否可以封装到js接口的答复
- jQuery的extend属性(非常好的用于封装插件,给出接口用)
- 数据结构学习---堆栈的动态数组实现及链表实现
- 数组与单链表分别实现栈存储
- 数组,集合,IEnumerable接口,迭代器
- 数组和链表的区别