用链表实现堆栈数据结构
2011-05-14 16:55
302 查看
包含主要的功能,压栈、出栈、读数据~~~~~~~~~~~~~
头文件 stack.h
源文件 stack.c
/************************************************************************/ /* author : thomas E-mail: chenhua308@gmail.com */ /************************************************************************/
头文件 stack.h
#include <stdio.h> #include <malloc.h> /* **interface of stack data-structure module */ typedef int T; //you can change int into whatever data type you want typedef struct tag { T element; struct tag *link; } node; typedef node *STACK; /* ** push ** push a new value into the stack, the parameter is the value needed to push */ void push( STACK *top, T num ); /* ** pop ** just delete the top node */ void pop( STACK *top ); /* ** access ** return the top node's value */ T access( STACK top ); /* ** is_empty ** judge the state of stack ---empty? full? or …… */ int is_empty( STACK top ); /* ** destroy ** destroy the stack */ void destroy( STACK *top ); /* ** creat ** create stack */ STACK creat( T num ); /* ** print ** print the value of stack */ void print( STACK top );
源文件 stack.c
#include "stack.h" /*****************************************************************************/ STACK creat( T num ) { node *p; p = ( node * )malloc( sizeof( node ) ); p -> element = num; p -> link = NULL; return p; } void push( STACK *top, T num ) { node *p; p = ( node * )malloc( sizeof( node ) ); p -> element = num; if ( *top == NULL ) { *top = p; p -> link = NULL; } else { p -> link = *top; *top = p; } } T access( STACK top ) { return top -> element; } int is_empty( STACK top ) { return top == NULL; } void pop( STACK *top ) { node *p; p = *top; *top = (*top) -> link; free(p); } void destroy( STACK *top ) { node *temp; while ( *top != NULL ) { temp = *top; *top = (*top) -> link; free(temp); } // return top; } void print( STACK top ) { int i = 0; printf("****************************************************************/n"); if ( top == NULL ) { printf("the stack is empty! /n"); } else { while ( top != NULL ) { i++; printf("the %d number is: %d /n", i, top -> element); top = top -> link; } } }; //test the functions void main(int argc, char *argv[]) { STACK stack1 = NULL; print(stack1); stack1 = creat( 4 ); print(stack1); push( &stack1, 6 ); print(stack1); push( &stack1, 2 ); push( &stack1, 8 ); print(stack1); push( &stack1, 1 ); }
相关文章推荐
- Java中链表、堆栈、队列、二叉树、散列表等数据结构的实现
- java数据结构-链表实现堆栈 控制插入的方式,成为有序链表
- 数据结构-堆栈-链表实现
- 数据结构学习---堆栈的动态数组实现及链表实现
- 数据结构学习---堆栈的动态数组实现及链表实现
- 数据结构链表功能的实现
- [java数据结构]--java双向链表LinkedList的简单实现
- 数据结构-----链表的实现
- 链表数据结构的实现
- 不依赖堆栈的链表反转——java实现
- javascript的数组实现数据结构中的堆栈和队列
- 数据结构与算法:单链表(利用万能指针实现对任意类型数据进行操作)
- 数据结构:单向链表的C语言实现
- 【c++版数据结构】之用带头节点的单链表实现一元多项式(C语言版)
- 数据结构-链表逆置(c++模板类实现)
- 【数据结构】栈的链表实现
- 基础数据结构:顺序表、链表——Python实现
- 链表堆栈C实现
- 数据结构链表(java版)的实现和思考
- 数据结构模版----单链表SimpleLinkList[不带头结点&&伪OO](C语言实现)