1 typedef int elemtype; 2 #define MAXSIZE 10; 3 typedef struct _stack 4 { 5 int top; 6 elemtype data[10]; 7 }stack; 8 9 stack *init_seq_stack(); 10 bool destory(stack *s); 11 12 bool init_seqstack(stack *s); 13 14 bool clear_seqstack(stack *s); 15 16 bool push(stack *s,elemtype e); 17 18 bool pop(stack *s,elemtype *e); 19 20 bool is_empty(stack *s); 21 bool is_full(stack *s); 22 23 int get_length(stack *s); 24 25 elemtype get_top(stack *s); 26 27 bool set_top(stack *s,elemtype e); 28 29 bool show(stack *s); 30 31 bool destory(stack *s);
方法的实现:
1 #include"SEQ_STACK.h" 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 stack *init_seq_stack() 6 { 7 stack *s = (stack *)malloc(sizeof(stack)*1); 8 if(s == NULL) 9 { 10 return NULL; 11 } 12 s->top = 0; 13 return s; 14 15 } 16 17 bool destory(stack *s) 18 { 19 if(s == NULL) 20 { 21 return false; 22 } 23 free(s); 24 return true; 25 } 26 27 bool init_seqstack(stack *s) 28 { 29 if(s == NULL) 30 { 31 return false; 32 } 33 s->top = 0; 34 return true; 35 } 36 37 bool clear_seqstack(stack *s) 38 { 39 if(s == NULL) 40 { 41 return false; 42 } 43 s->top = 0; 44 return true; 45 } 46 47 bool push(stack *s,elemtype e) 48 { 49 if(s == NULL) 50 { 51 return false; 52 } 53 if( is_full(s)) 54 { 55 return false; 56 } 57 58 s->data[s->top ++] = e ; 59 return true; 60 61 } 62 63 64 bool pop(stack *s,elemtype *e) 65 { 66 if(s == NULL) 67 { 68 return false; 69 } 70 if(is_empty(s)) 71 { 72 return false; 73 } 74 75 *e = s->data[--s->top]; 76 return true; 77 } 78 79 80 bool is_empty(stack *s) 81 { 82 if(s == NULL) 83 { 84 return false; 85 } 86 return s->top == 0; 87 } 88 89 bool is_full(stack *s) 90 { 91 if(s == NULL) 92 { 93 return false; 94 } 95 return s->top == MAXSIZE; 96 } 97 98 int get_length(stack *s) 99 { 100 if(s == NULL) 101 { 102 return false; 103 } 104 return s->top; 105 } 106 107 108 elemtype get_top(stack *s,elemtype e) 109 { 110 if(s == NULL) 111 { 112 return false; 113 } 114 e = s->data[s->top-1]; 115 return e; 116 117 } 118 119 120 bool show(stack *s) 121 { 122 if(s == NULL) 123 { 124 return false; 125 } 126 for(int i = s->top-1 ;i>=0;i--) 127 { 128 printf("%d ",s->data[i]); 129 } 130 printf("\n"); 131 return true; 132 } 133 134 bool set_top(stack *s,elemtype e) 135 { 136 if(s == NULL) 137 { 138 return true; 139 } 140 141 s->data [-- s->top] = e; 142 return true ; 143 }