栈的常见操作编程

 

 

 #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 }
~                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                                                                                                                                                                                                                                                                                                                                                                                                                                           
~                                               

 

版权声明:本文为weixin_42720729原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42720729/article/details/81461387