标签:int 结构 技术 http stack error pop std 删除
工程目录结构:
Stack.h:
1 void StackStart();
Stack.c:
1 #include<stdio.h> 2 #include "common.h" 3 #include "Stack.h" 4 5 //顺序栈结构 6 typedef struct 7 { 8 ElemType data[MAXSIZE]; 9 int top; //栈顶指针 10 }SqStack; 11 12 //格式化打印数据 13 Status s_visit(ElemType e) 14 { 15 printf("%d, ", e); 16 return OK; 17 } 18 19 //创建空栈 20 Status InitStack(SqStack *S) 21 { 22 S->top = -1; 23 return OK; 24 } 25 26 //栈置为空 27 Status ClearStack(SqStack *S) 28 { 29 S->top = -1; 30 return OK; 31 } 32 33 Status StackEmpty(SqStack S) 34 { 35 if (1 == S.top) 36 return TRUE; 37 else 38 return FALSE; 39 } 40 41 int StackLength(SqStack S) 42 { 43 return S.top + 1; 44 } 45 46 //获取栈顶元素,不不删除 47 Status GetTop(SqStack S, ElemType *e) 48 { 49 if (S.top == -1) 50 { 51 return ERROR; 52 } 53 else 54 { 55 *e = S.data[S.top]; 56 } 57 return OK; 58 } 59 60 Status Push(SqStack *S, ElemType e) 61 { 62 //栈满 63 if (S->top >= MAXSIZE - 1) 64 { 65 return ERROR; 66 } 67 ++S->top; 68 S->data[S->top] = e; 69 return OK; 70 } 71 72 //获取栈顶元素,并删除 73 Status Pop(SqStack *S, ElemType *e) 74 { 75 if (S->top <= -1) 76 return ERROR; 77 78 *e = S->data[S->top]; 79 --S->top; 80 return OK; 81 } 82 83 //从栈底到栈顶依次对栈中每个元素显示 84 Status StackTraverse(SqStack S) 85 { 86 int index = 0; 87 while (index <= S.top) 88 { 89 s_visit(S.data[index++]); 90 } 91 return OK; 92 } 93 94 void StackStart() 95 { 96 int j; 97 SqStack s; 98 int e; 99 if (InitStack(&s) == OK) 100 for (j = 1; j <= 10; j++) 101 Push(&s, j); 102 printf("栈中元素依次为:\n"); 103 StackTraverse(s); 104 Pop(&s, &e); 105 printf("\n\n弹出的栈顶元素: %d\n", e); 106 printf("\n\n栈空否:%d(1:空 0:否)\n", StackEmpty(s)); 107 GetTop(s, &e); 108 printf("\n\n栈顶元素 e=%d \n栈的长度为%d\n", e, StackLength(s)); 109 ClearStack(&s); 110 printf("\n\n清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s)); 111 }
main.c:
1 #include "Stack.h" 2 3 int main() 4 { 5 StackStart(); 6 getchar(); 7 return 0; 8 }
标签:int 结构 技术 http stack error pop std 删除
原文地址:https://www.cnblogs.com/luguoshuai/p/9247929.html