栈的常见操作编程

 

 

 #ifndef _SequenceStack_H_
  2 #define _SequenceStach_H_
  3 #include <stdlib.h>
  4 #define SUCCESS  10000
  5 #define FAILURE  10001
  6 #define TRUE     10002
  7 #define FALSE    10003
  8 #define SIZE  10
  9 
 10 typedef int ElemType;
 11 
 12 typedef struct stack Stack;
 13 struct stack
 14 {
 15     int top;
 16     ElemType *data;
 17 };
 18 
 19 int StackInit(Stack **s);
 20 int StackEmpty(Stack *s);
 21 int push(Stack *s,ElemType e);
 22 int GetTop(Stack *s);
 23 int pop(Stack *s);
 24 int StackClear(Stack *s);
 25 int StackDestroy(Stack **s);
 26 
 27 #endif

 

  1 #include "SequenceStack.h"
  2 #include <stdio.h>
  3 
  4 int StackInit(Stack **s)
  5 {
  6     *s = (Stack *)malloc(sizeof(Stack) * 1);
  7     if(NULL == (*s))
  8     {
  9         return FAILURE;
 10     }
 11 
 12     (*s)->top = -1;
 13     (*s)->data = (ElemType *)malloc(sizeof(ElemType) * SIZE);
 14     if(NULL == (*s)->data)
 15     {
 16         return FAILURE;
 17     }
 18     return SUCCESS;
 19 }
 20 
 21 int StackEmpty(Stack *s)
 22 {
 23     return (s->top == -1)? TRUE:FALSE;
 24 }
 25 
 26 int push(Stack *s,ElemType e)
 27 {
 28     if(NULL == s || s->top ==9)
 29     {
 30         return FAILURE;
 31     }
 32 
 33     s->data[s->top + 1] = e;
 34     s->top++;
 35 
 36     return SUCCESS;
 37 }
 38 
 39 int GetTop(Stack *s)
 40 {
 41     if(s->top == -1)
 42     {
 43         return FAILURE;
 44     }
 45     return s->data[s->top];
 46 
 47 }
 48 
 49 int pop(Stack *s)
 50 {
 51     if(s->top == -1)
 52     {
 53         return FAILURE;
 54     }
 55     s->top--;
 56 
 57     return SUCCESS;
 58 }
 59 
 60 int StackClear(Stack *s)
 61 {
 62     if(NULL == s)
 63     {
 64         return FAILURE;
 65     }
 66     s->top = -1;
 67 
 68     return SUCCESS;
 69 }
 70 
 71 int StackDestroy(Stack **s)
 72 {
 73     if(s == NULL || *s == NULL)
 74     {
 75         return FAILURE;
 76     }
 77 
 78     free((*s)->data);
 79     free(*s);
 80     *s = NULL;
 81 
 82     return SUCCESS;
 83 }

 

  1 #include "SequenceStack.h"
  2 #include <stdio.h>
  3 
  4 int main()
  5 {
  6     Stack *stack;
  7     int ret,i;
  8 
  9     ret = StackInit(&stack);
 10     if(ret == SUCCESS)
 11     {
 12         printf("Init Sequence Stack Success!\n");
 13     }
 14     else
 15     {
 16         printf("Failure!\n");
 17     }
 18 
 19     ret = StackEmpty(stack);
 20     if(ret == FALSE )
 21     {
 22         printf("stack is not Empty");
 23     }
 24     else
 25     {
 26         printf("stack is  Empty");
 27     }
 28 
 29     for(i = 0; i < 10; i++)
 30     {
 31         ret = push(stack,i + 1);
 32         if(ret == SUCCESS)
 33         {
 34             printf("Push %d Success!\n",i + 1);
 35         }
 36         else if(ret == FAILURE)
 37         {
 38             printf("Push %d Failure!\n",i + 1);
 39         }
 40 
 41     }
 42 
 43     ret = GetTop(stack);
 44         if(FAILURE == ret)
 45         {
 46             printf("Get Top Failure!\n");
 47         }
 48         else
 49         {
 50             printf("Top is %d\n",ret);
 51         }
 52 
 53 
 54     for(i = 0;i < 5;i++)
 55     {
 56         ret = pop(stack);
 57         if(ret == FAILURE)
 58         {
 59             printf("pop Failure!\n");
 60         }
 61         else
 62         {
 63             printf("Pop Success!\n");
 64         }
 65     }
 66 
 67     ret = StackClear(stack);
 68     if(ret == FAILURE)
 69     {
 70         printf("Clear Failure!\n");
 71     }
 72     else
 73     {
 74         printf("Clear Success!\n");
 75     }
 76 
 77     ret = StackDestroy(&stack);
 78     if(ret == SUCCESS)
 79     {
 80         printf("Destroy Success!\n");
 81     }
 82     else
 83     {
 84         printf("Destroy Failure!\n");
 85     }
 86     return 0;
 87 }
~                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                               

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值