#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_
typedef int elemtype;
typedef struct _NODE
{
int data;
struct _NODE *next;
}NODE;
typedef struct _stack
{
int len;
struct _NODE *top;
}stack;
stack* init_link_stack();
bool destory_link_stack(stack *p);
int get_length(stack*p);
bool push_link_stack(stack *p,elemtype e);
bool pop_link_stack(stack*p,elemtype *e);
bool is_empty(stack *p);
#endif
简单方法的实现
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #include"LINK_STACK.h" 5 6 7 stack *init_link_stack() 8 { 9 stack *pstack = (stack *)malloc(sizeof(stack)*1); 10 pstack->len = 0; 11 pstack->top = NULL; 12 return pstack; 13 14 15 } 16 bool destory_link_stack(stack *p) 17 { 18 elemtype e; 19 while (!is_empty(p)) 20 { 21 pop_link_stack(p,&e); 22 } 23 free(p); 24 return true; 25 } 26 27 int get_length(stack *p) 28 { 29 if (p == NULL) 30 { 31 return false; 32 } 33 34 return p->len; 35 } 36 37 bool push_link_stack(stack *p,elemtype e) 38 { 39 if (p == NULL) 40 { 41 return false; 42 } 43 NODE *tmp = (NODE *)malloc(sizeof(NODE) *1); 44 tmp->data = e; 45 46 tmp->next = p->top ; 47 p->top = tmp; 48 p->len ++; 49 return true; 50 51 } 52 53 bool pop_link_stack(stack*p,elemtype *e) 54 { 55 if (is_empty(p)) 56 { 57 return false; 58 } 59 *e = p->top->data ; 60 NODE *tmp = p->top ; 61 p->top = tmp->next ; 62 p->len --; 63 free(tmp); 64 return true; 65 66 } 67 68 bool is_empty(stack *p) 69 { 70 if (p == NULL) 71 { 72 return false; 73 } 74 75 return p->len == 0; 76 }