数据结构-栈有关操作算法

程序代码如下:

  1 #include <stdio.h>

  2 #include <stdlib.h>

  3 

  4 #define STACK_INIT_SIZE 100

  5 #define STACKINCREMENT 10

  6 #define OVERFLOW -2

  7 #define OK 1

  8 #define ERROR 0

  9 

 10 typedef int SElemType;

 11 

 12 //栈结构体

 13 typedef struct {

 14     SElemType *base;

 15     SElemType *top;

 16     int stacksize;

 17 }SqStack;

 18 

 19 int InitStack(SqStack *S);//初始化栈

 20 int GetTop(SqStack *S,SElemType *e);//取得栈顶元素

 21 int Push(SqStack *S,SElemType e);//入栈

 22 int Pop(SqStack *S,SElemType *e);//删除栈中的元素

 23 int DestoryStack(SqStack *S);//销毁栈

 24 int StackEmpty(SqStack *S);//判断栈是否为空

 25 int StackLength(SqStack *S);//取得栈的长度

 26 int StackTraverse(SqStack *S);//自顶向下遍历栈

 27 int ClearStack(SqStack *S);//清空栈中的元素

 28 

 29 //初始化栈

 30 int InitStack(SqStack *S) {

 31     S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));

 32     if(!S->base) {

 33         exit(OVERFLOW);

 34     }

 35     S->top = S->base;

 36     S->stacksize = STACK_INIT_SIZE;

 37 

 38     return OK;

 39 }

 40 

 41 //取得栈顶元素

 42 int GetTop(SqStack *S,SElemType *e) {

 43     if(S->top == S->base) {

 44         return ERROR;

 45     }

 46     *e = *(S->top-1);

 47     return OK;

 48 }

 49 

 50 //清空栈中的元素

 51 int ClearStack(SqStack *S) {

 52     S->top = S->base;

 53     return OK;

 54 }

 55 //入栈

 56 int Push(SqStack *S,SElemType e) {

 57     if((S->top-S->base)>=S->stacksize) {

 58         S->base = (SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));

 59         if(!S->base) exit(OVERFLOW);

 60         S->top = S->base + S->stacksize;

 61         S->stacksize += STACKINCREMENT;

 62     }

 63     *S->top++ = e;

 64     return OK;

 65 }

 66 

 67 //删除栈中的元素

 68 int Pop(SqStack *S,SElemType *e) {

 69     if(S->top  == S->base) return ERROR;

 70     *e = *--S->top;

 71     return OK;

 72 }

 73 

 74 //销毁栈

 75 int DestoryStack(SqStack *S) {

 76     S->top = S->base;

 77     free(S->base);

 78     S->top = NULL;

 79     S->base = NULL;

 80     return OK;

 81 }

 82 

 83 //判断栈是否为空

 84 int StackEmpty(SqStack *S) {

 85     if(S->top == S->base) {

 86         return OK;

 87     }

 88     return ERROR;

 89 }

 90 

 91 //取得栈的长度

 92 int StackLength(SqStack *S) {

 93     return S->top-S->base;

 94     /*int i=0;

 95     if(!StackEmpty(S)) {

 96         while(S->top!=S->base) {

 97             i++;

 98             S->top--;

 99         }

100     }

101     return i;*/

102 }

103 

104 //自顶向下遍历栈

105 int StackTraverse(SqStack *S) {

106     SElemType *p;

107     if(S->base!=S->top) {

108         printf("栈中自顶向下的元素为:");

109         p = S->top;

110         while(p!=S->base) {

111             p--;

112             printf("%d ",*p);

113         }

114     } else {

115         printf("栈为空!\n");

116         return ERROR;

117     }

118     printf("\n");

119     return OK;

120 }

121 

122 int main()

123 {

124     SqStack sq;

125     int f1,f2,e,f3,i,n,a,f4,f5,len,f6,f7,f8;

126     //初始化栈

127     f1 = InitStack(&sq);

128     if(f1) printf("初始化栈成功!\n");

129     else printf("初始化栈失败!\n");

130 

131     //入栈

132     printf("请输入栈中元素的个数:");

133     scanf("%d",&n);

134     printf("请输入栈中元素:");

135     for(i=0; i<n; i++) {

136         scanf("%d",&a);

137         Push(&sq,a);

138     }

139 

140     //取得栈顶元素

141     f2 = GetTop(&sq,&e);

142     if(f2) printf("栈顶元素为:%d\n",e);

143     else printf("栈为空!无栈顶元素!\n");

144 

145     //判断栈是否为空

146     f5 = StackEmpty(&sq);

147     if(f5) printf("栈为空!\n");

148     else printf("栈不为空!\n");

149 

150     //删除栈中的元素

151     f6 = Pop(&sq,&e);

152     if(f6) {

153         printf("删除的元素为:%d\n",e);

154     }

155 

156     //取得栈的长度

157     len = StackLength(&sq);

158     printf("栈的长度为:%d\n",len);

159 

160     //自顶向下遍历栈

161     StackTraverse(&sq);

162 

163     f8 = ClearStack(&sq);

164     if(f8) printf("清空栈成功!\n");

165     else printf("清空栈失败!\n");

166 

167     //销毁栈

168     f4 = DestoryStack(&sq);

169     if(f4) printf("销毁栈成功!\n");

170     else printf("销毁栈失败!\n");

171 

172 

173     return 0;

174 }

 

你可能感兴趣的:(数据结构)