程序代码
自定义实现栈stack入栈和出栈(非链栈) code c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Stack {
int *data;
int size, top;
} Stack;
Stack *init(int n) {
Stack *s = (Stack *)malloc(sizeof(Stack));
s->data = (int *)malloc(sizeof(int) * n);
s->size = n;
s->top = -1;
return s;
}
int empty(Stack *s) {
return s->top == -1;
}
int top(Stack *s) {
return s->data[s->top];
}
int push(Stack *s, int val) {
if (s == NULL) return 0;
if (s->top == s->size - 1) return 0;
s->data[++s->top] = val;
return 1;
}
int pop(Stack *s) {
if (s == NULL) return 0;
if (empty(s)) return 0;
s->top -= 1;
return 1;
}
void output(Stack *s) {
printf("Stack : [");
for (int i = 0; i <= s->top; ++i) {
i && printf(" ");
printf("%d", s->data[i]);
}
printf("]\n");
return ;
}
void clear(Stack *s) {
if (s == NULL) return ;
free(s->data);
free(s);
return ;
}
int main() {
srand(time(0));
#define MAX_OP 20
Stack *s = init(MAX_OP);
for (int i = 0; i < MAX_OP; ++i) {
int val = rand() % 100;
int op = rand() % 4;
switch (op) {
case 0:
case 1:
case 2: {
printf("push %d to Stack, return %d (1true, 0false)\n", val, push(s, val));
} break;
case 3: {
if (!empty(s)) {
printf("|--------------|\n");
printf("|success to pop|\n");
printf("|--------------|\n");
printf("pop %d from the Stack\n", top(s));
pop(s);
} else {
printf("|---------------------------|\n");
printf("|pop failed, empty Stack now|\n");
printf("|---------------------------|\n");
}
} break;
}
output(s), printf("\n");
}
#undef MAX_OP
clear(s);
return 0;
}
运行结果
|---------------------------|
|pop failed, empty Stack now|
|---------------------------|
Stack : []
|---------------------------|
|pop failed, empty Stack now|
|---------------------------|
Stack : []
push 89 to Stack, return 1 (1true, 0false)
Stack : [89]
push 50 to Stack, return 1 (1true, 0false)
Stack : [89 50]
push 61 to Stack, return 1 (1true, 0false)
Stack : [89 50 61]
push 39 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39]
push 56 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56]
push 79 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79]
push 87 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 87]
|--------------|
|success to pop|
|--------------|
pop 87 from the Stack
Stack : [89 50 61 39 56 79]
push 94 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94]
push 50 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50]
push 75 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75]
push 86 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75 86]
push 43 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75 86 43]
push 56 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75 86 43 56]
push 77 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75 86 43 56 77]
push 51 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75 86 43 56 77 51]
push 75 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75 86 43 56 77 51 75]
push 35 to Stack, return 1 (1true, 0false)
Stack : [89 50 61 39 56 79 94 50 75 86 43 56 77 51 75 35]
结论
代码示例,有问题留言。