棧模型
後進先出表
棧實現
需要實現
- 建立空棧
- 置空棧
- 入棧
- 出棧
- 獲取棧頂
- 銷燬棧
連結串列實現
結構體存放棧元素跟指向棧下一個結構體的指標
入棧 一個新的節點新增到 header->next, 這裡是棧頂
出棧 彈出 header->next 即第一個節點(棧頂
/**
* 棧-連結串列實現
*/
#include <stdio.h>
#include <stdlib.h>
#define Error(Str) FatalError(Str)
#define FatalError(Str) fprintf(stderr,"%s\n",Str),exit(1)
// #define element_type int;
typedef int element_type;
typedef struct node
{
element_type element;
struct node *next;
} * ptr_to_node;
typedef ptr_to_node stack;
int is_empty(stack s);
stack create_stack();
void dispose_stack( stack s);
void make_empty(stack s);
void push(element_type x, stack s);
element_type top(stack s);
void pop(stack s);
void test();
int main(int argc, char const *argv[])
{
test();
return 0;
}
int is_empty(stack s)
{
return s->next == NULL;
}
stack create_stack()
{
stack s;
s = (stack)malloc(sizeof(struct node));
if (NULL == s)
FatalError("out of space");
s->next = NULL;
make_empty(s);
return s;
}
void dispose_stack(stack s)
{
make_empty(s);
free(s);
}
void make_empty(stack s)
{
if (NULL == s)
Error("must crated a stack");
else
while (!is_empty(s)) {
pop(s);
}
}
void push( element_type x, stack s)
{
stack temp_cell;
temp_cell = (stack)malloc(sizeof(struct node));
if (NULL == temp_cell)
FatalError("out of space");
temp_cell->element = x;
temp_cell->next = s->next;
s->next = temp_cell;
}
element_type top(stack s)
{
if (!is_empty(s))
return s->next->element;
Error("empty stack");
return 0;
}
void pop(stack s)
{
stack temp;
temp = s->next;
s->next = temp->next;
free(temp);
}
void print_stack(stack s)
{
stack p;
p = s->next;
while (NULL != p) {
printf("\t%d\n", p->element);
p = p->next;
}
}
void test()
{
stack s;
int act;
while (1) {
printf("\n\tplease input a intger and choose a option.\n");
printf("\t\t1.create a empty stack.\n");
printf("\t\t2.push a element to stack.\n");
printf("\t\t3.pop a element from stack.\n");
printf("\t\t4.get the top element of stack.\n");
printf("\t\t5.print stack.\n");
printf("\t\t6.dispose stack.\n");
printf("\t\t0.exit\n");
scanf("%d", &act);
if (act == 0)
break;
switch(act) {
case 1:
s = create_stack();
printf("create stack success\n");
break;
case 2: {
int x;
printf("please input a intger.\n");
scanf("%d", &x);
push(x, s);
printf("after push\n");
print_stack(s);
break;
}
case 3:
pop(s);
printf("pop a element success\n");
break;
case 4:
printf("stack top:%d\n", top(s));
break;
case 5:
print_stack(s);
break;
case 6:
dispose_stack(s);
printf("dispose stack success\n");
break;
}
}
}
棧陣列實現
用一個包含棧頂在陣列下標(top)跟陣列長度以及陣列指標組成的結構體來存放棧
入棧:將元素存入陣列的 top + 1位置,棧頂 + 1,即是:arr[ ++top ]
出棧:同理 彈出棧頂元素並 top--,即是:arr[ top-- ]
/**
* 棧-陣列實現
*/
#include <stdio.h>
#include <stdlib.h>
#define error(str) fatal_error(str)
#define fatal_error(str) fprintf(stderr, "%s\n", str),exit(1)
#define MINI_STACK_SIZE ( 5 )
#define EMPTY_TOS ( -1 )
typedef int element_type;
struct stack_record;
typedef struct stack_record *stack;
struct stack_record
{
int capacity;
int top;
element_type *arr;
};
/*typedef struct stack_record
{
int capacity;
int top;
element_type *arr;
} *stack;*/
int is_empty(stack s);
int is_full(stack s);
stack create_stack(int max_len);
void dispose_stack(stack *s);
void make_empty(stack s);
void push(element_type x, stack s);
void pop(stack s);
element_type top(stack s);
element_type top_and_pop(stack s);
void print_stack(stack s);
void test();
int main(int argc, char const *argv[])
{
test();
return 0;
}
stack create_stack(int max_len)
{
stack s;
if (max_len < MINI_STACK_SIZE)
error("max_len is too small.");
s = (stack)malloc(sizeof(struct stack_record));
if (NULL == s)
fatal_error("out of space");
s->capacity = max_len;
s->arr = (element_type*)malloc(sizeof(element_type) * max_len);
if (NULL == s->arr)
fatal_error("out of space");
make_empty(s);
return s;
}
void dispose_stack(stack *s)
{
if ( NULL != (*s) ) {
free((*s)->arr);
free((*s)); //free 後必須指向 NULL
(*s)->arr = NULL;
(*s) = NULL;
}
}
int is_empty(stack s)
{
return s->top == EMPTY_TOS;
}
int is_full(stack s)
{
return s->top == s->capacity - 1;
}
void make_empty(stack s)
{
s->top = EMPTY_TOS;
}
void push(element_type x, stack s)
{
if (is_full(s))
error("Full stack");
else
s->arr[ ++s->top ] = x;
}
void pop(stack s)
{
if (is_empty(s))
error("Empty stack");
s->top--;
}
element_type top(stack s)
{
if (!is_empty(s))
return s->arr[ s->top];
error("Empty stack");
return 0;
}
element_type top_and_pop(stack s)
{
if (!is_empty(s))
return s->arr[ s->top-- ];
error("Empty stack");
return 0;
}
void print_stack(stack s)
{
if (NULL == s)
return;
int i;
for (i = s->top; i > EMPTY_TOS; i--) {
printf("\t%d\n", s->arr[i]);
}
}
void test()
{
stack s;
int act;
while (1) {
printf("\n\tplease input a intger and choose a option.\n");
printf("\t\t1.create a empty stack.\n");
printf("\t\t2.push a element to stack.\n");
printf("\t\t3.pop a element from stack.\n");
printf("\t\t4.get the top element of stack.\n");
printf("\t\t5.print stack.\n");
printf("\t\t6.dispose stack.\n");
printf("\t\t0.exit\n");
scanf("%d", &act);
if (act == 0)
break;
switch(act) {
case 1: {
int max_len;
printf("please input max_len of stack.\n");
scanf("%d", &max_len);
s = create_stack(max_len);
printf("create stack success\n");
break;
}
case 2: {
int x;
printf("please input a intger.\n");
scanf("%d", &x);
push(x, s);
printf("after push\n");
print_stack(s);
break;
}
case 3:
pop(s);
printf("pop a element success\n");
break;
case 4:
printf("stack top:%d\n", top(s));
break;
case 5:
print_stack(s);
break;
case 6:
dispose_stack(&s);
printf("dispose stack success\n");
break;
}
}
}