# 数据结构与算法：栈 C语言实现

#### 1.顺序栈的C语言实现

``````#include
#include

typedef struct Stack {
int *data;//数据域
int size;//栈长度,也是栈顶数组下标-1
int max;//栈最大容量
} Stack;
//初始化
Stack *initStack(int max)
{
struct Stack *stack;
stack = (struct Stack *)malloc(sizeof(struct Stack));
stack->size = 0;
stack->max = max;
stack->data = (int*)malloc(sizeof(int)*max);
return stack;
}
//压栈
void push(Stack *stack, int item)
{
if (stack->size >= stack->max)
{
printf("stack is full! \n");
}else{
stack->data[stack->size++] = item;
}
}
//出栈
int pop(Stack *stack)
{
if (stack->size >= 0)
{
return stack->data[--stack->size];
}
}
//test
int main()
{
struct Stack *stack;
stack = initStack(3);
push(stack,1);
push(stack,2);
push(stack,3);
push(stack,4);
printf("stack out:%d \n", pop(stack));
printf("stack out:%d \n", pop(stack));
push(stack,5);
push(stack,6);
push(stack,7);
printf("stack out:%d \n", pop(stack));
printf("stack out:%d \n", pop(stack));
printf("stack out:%d \n", pop(stack));
return 0;
}``````

### 2.链栈的C语言实现

``````#include
#include

typedef struct StackNode {
int data;//数据域
struct StackNode *next;//指针域,这里用next或者pre都行，看怎么规定左右了，如果是左进左出那就是next，右进右出就是pre好理解
} StackNode;

typedef struct LinkedStack {
int size;//栈长度
int max;//栈最大容量
struct StackNode *top;//指针域

//初始化
{
stack->size = 0;
stack->max = max;
stack->top = NULL;
return stack;
}
//压栈
void push(LinkedStack *stack, int item)
{
if (stack->size >= stack->max)
{
printf("stack is full! \n");
}else{
struct StackNode *node;
node = (struct StackNode *)malloc(sizeof(struct StackNode));
node->data = item;
node->next = stack->top;
stack->size++;
stack->top = node;
}
}
//出栈
{
if (stack->size > 0)
{
struct StackNode *top;
top = stack->top;
stack->top = top->next;
stack->size--;
}
}
int main()
{
stack = initStack(3);
push(stack,1);
push(stack,2);
push(stack,3);
push(stack,4);
printf("stack out:%d \n", pop(stack));
printf("stack out:%d \n", pop(stack));
push(stack,5);
push(stack,6);
push(stack,7);
printf("stack out:%d \n", pop(stack));
printf("stack out:%d \n", pop(stack));
printf("stack out:%d \n", pop(stack));
return 0;
}``````