Stack栈 链式存储 实现
来源:互联网 发布:诺基亚按键机java游戏 编辑:程序博客网 时间:2024/05/29 02:18
//// Stack.h// Algorithms&Data_structures//// Created by TTc on 15-2-2.// Copyright (c) 2015年 TTc. All rights reserved.//#ifndef __Algorithms_Data_structures__Stack__#define __Algorithms_Data_structures__Stack__#include <stdlib.h>#include "list.h"/* Implement stacks as linked lists .*/typedef List Stack;/* Public Interface */#define stack_init list_init#define stack_destroy list_destroyint stack_push(Stack *stack,const void*data);int stack_pop(Stack *stack, void **data);#define stack_peek(stack) ((stack)->head == NULL ? NULL:(stack)->head->data)#define stack_size list_size#endif /* defined(__Algorithms_Data_structures__Stack__) */
//// Stack.c// Algorithms&Data_structures//// Created by TTc on 15-2-2.// Copyright (c) 2015年 TTc. All rights reserved.//#include "stack.h"#include <stdio.h>#include <string.h>intstack_push(Stack *stack,const void*data){ // Push the data onto the stack return list_ins_next(stack, NULL, data);}intstack_pop(Stack *stack, void **data){ // Pop the data off the stack return list_rem_next(stack, NULL, data);}
//// test_stack_main.c// //// Created by TTc on 15/5/25.////#include <string.h>#include <stdlib.h>#include <stdio.h>#include "list.h"#include "stack.h"/* destroy */voiddestroy(void *data){ printf("in destroy\n"); free(data); return;}/* main */intmain(int argc, char **argv){ Stack stack; int *int_ptr = NULL; int ret = 0; int i; stack_init(&stack, destroy); for(i = 0; i < 5; i++ ) { int_ptr = (int *)malloc(sizeof(int)); if( int_ptr == NULL ) return -1; *int_ptr = i; printf("push the data: %d\n",i); ret = stack_push(&stack, (void *)int_ptr); if( ret != 0 ) return -1; } printf("size of the stack is : %d\n", stack_size(&stack)); //pop the data from top to the bottom for(i = stack_size(&stack); i > 0; i-- ) { int_ptr = NULL; stack_pop(&stack, (void **)&int_ptr); printf("i = %d, pop the data = %d\n",i,*int_ptr); free(int_ptr); } printf("after pop size of the stack is :%d\n", stack_size(&stack)); return 0;}
0 0
- Stack栈 链式存储 实现
- 栈---链式存储实现
- Algorithms: 栈(Stack) 链式与数组实现
- 栈的链式存储实现
- 栈的链式存储实现
- 栈的链式存储实现
- 链式栈(Linked Stack)
- Stack(2)Stack的链式实现
- 通用的链式stack实现
- 栈(链式存储) C++模板实现
- 链式栈存储实现及操作
- 栈的链式存储结构的实现
- 栈的链式存储结构C++实现
- 栈的链式存储 - API实现
- 栈的链式存储结构及实现
- 栈的链式存储结构及实现
- 线性结构--->栈的链式存储实现
- 栈的链式存储结构以及实现
- 显示指定文件中出现在另一个文件中的所有句子
- oracle编程
- 为什么程序员需要关心顺序一致性(Sequential Consistency)而不是Cache一致性(Cache Coherence?)
- Android学习笔记开篇
- 提高工作效率的16条Android开发小经验
- Stack栈 链式存储 实现
- hdoj 2046 骨牌铺方格(递推)
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] -- B. "Or" Game (容斥定理)
- Strust+hibernate+strust2的整合方案(1)
- 【Halcon】Halcon与OpenCV介绍、比较
- 第十三周实践项目2.3-动物这样叫
- vim配置及插件安装管理(超级详细)
- 数据清洗的一些梳理
- HTML获取触发事件的元素id