栈的链表实现
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
struct stack_node {
int data;
struct stack_node *next;
};
typedef struct stack_node *PtrToNode;
typedef PtrToNode Stack;
Stack create_stack();
void push_stack(Stack s, int data);
void pop_stack(Stack s);
int top_stack(Stack s);
int stack_is_empty(Stack s);
int main()
{
Stack stack = create_stack();
int top_data,i;
for (i = 0;i < 10;i++) {
push_stack(stack, i);
}
pop_stack(stack);
top_data = top_stack(stack);
printf("%d\n", top_data);
system("pause");
}
Stack create_stack()
{
Stack S;
S = (Stack)malloc(sizeof(struct stack_node));
if (S == NULL)
printf("malloc fair!\n");
S->next = NULL;
return S;
}
void push_stack(Stack s,int data)
{
PtrToNode head_node = (PtrToNode)malloc(sizeof(struct stack_node));
if (head_node == NULL)
printf("malloc fair!\n");
head_node->data = data;
head_node->next = s->next;
s->next = head_node;
}
void pop_stack(Stack s)
{
PtrToNode head_node = (PtrToNode)malloc(sizeof(struct stack_node));
if (head_node == NULL)
printf("malloc fair!\n");
if (stack_is_empty(s)) {
printf("Error! Stack is empty!\n");
}
else {
head_node = s->next;
s->next = head_node->next;
free(head_node);
}
}
int top_stack(Stack s)
{
if (stack_is_empty(s)) {
printf("Error! Stack is empty!\n");
return 0;
}
else {
return s->next->data;
}
}
int stack_is_empty(Stack s)
{
return s->next == NULL;
}
栈的数组实现
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
#define MinStackSize 5
#define EmptyTOS -1
struct stack_array {
int capacity;
int top_of_stack;
int *array;
};
typedef struct stack_array *ArrayRecord;
typedef ArrayRecord Stack;
Stack create_stack(int stack_capacity);
void make_empty(Stack s);
void push_stack(Stack s, int data);
int top_stack(Stack s);
void pop_stack(Stack s);
int stack_is_empty(Stack s);
int stack_is_full(Stack s);
int main()
{
Stack stack = create_stack(100);
int topdata, i;
for (i = 0;i < 10;i++) {
push_stack(stack, i);
}
pop_stack(stack);
pop_stack(stack);
topdata = top_stack(stack);
printf("%d\n", topdata);
system("pause");
}
Stack create_stack(int stack_capacity)
{
Stack S;
if (stack_capacity < MinStackSize)
printf("Error! Stack size is too small!\n");
S = (Stack)malloc(sizeof(struct stack_array));
if (S == NULL)
printf("malloc error!\n");
S->array = (int *)malloc(sizeof(struct stack_array) * stack_capacity);
if (S->array == NULL)
printf("malloc error!\n");
S->capacity = stack_capacity;
make_empty(S);
return S;
}
void make_empty(Stack s)
{
s->top_of_stack = EmptyTOS;
}
void push_stack(Stack s, int data)
{
if (stack_is_full(s)) {
printf("Error! Stack is full!\n");
}
else {
s->top_of_stack++;
s->array[s->top_of_stack] = data;
}
}
void pop_stack(Stack s)
{
if (stack_is_empty(s)) {
printf("Error! Stack is empty!\n");
}
else {
s->top_of_stack--;
}
}
int top_stack(Stack s)
{
if (stack_is_empty(s)) {
printf("Error! Stack is empty!\n");
return 0;
}
else {
return s->array[s->top_of_stack];
}
}
int stack_is_empty(Stack s)
{
return s->top_of_stack == EmptyTOS;
}
int stack_is_full(Stack s)
{
return s->top_of_stack == --s->capacity;
}