码迷,mamicode.com
首页 > 编程语言 > 详细

0.栈的C语言实现

时间:2018-11-12 15:45:12      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:语言   The   bottom   oid   remove   efi   struct   col   pop   

 1 #define max_size 10
 2 #define add_size 10
 3 
 4 
 5 #ifndef Header_h
 6 #define Header_h
 7 
 8 typedef struct{
 9     int *bottom;
10     int *top;
11     int stack_size;
12 }stack;
13 
14 void initial(stack*s){
15     s->bottom=(int*)malloc(max_size*sizeof(int));
16     s->top=s->bottom;
17     s->stack_size=max_size;
18 }
19 
20 void push(stack*s,int n){
21     if(s->top-s->bottom>=s->stack_size)
22     {
23         s->bottom=(int*)realloc(s->bottom,(s->stack_size+add_size)*sizeof(int));
24         s->stack_size+=add_size;
25     }
26     
27     *s->top=n;
28     s->top++;
29 }
30 
31 long height(stack*s){
32     
33     if(s->bottom==0)
34         return 0;
35     
36     long n;
37     n=s->top-s->bottom;
38     
39     return n;
40 }
41 
42 int size(stack*s){
43     if(s->bottom==0)
44         return 0;
45     
46     return s->stack_size;
47 }
48 
49 void pop(stack*s){
50     if(s->top==s->bottom){
51         printf("There is nothing to pop");
52     }
53     
54     s->top--;
55 }
56 
57 void clear(stack*s){
58     s->top=s->bottom;
59 }
60 
61 void remove(stack*s){
62     free(s->bottom);
63     s->bottom=0;
64 }
65 
66 #endif /* Header_h */

 

0.栈的C语言实现

标签:语言   The   bottom   oid   remove   efi   struct   col   pop   

原文地址:https://www.cnblogs.com/clclcl/p/9946562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!