C 栈的基本操作
C 栈的顺序存储结构
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 6 /*存储空间初始分配量 */
typedef struct SeqStack
{
int top;
int data[MAX_SIZE];
}SeqStack;
SeqStack* Init_Stack()
{
SeqStack* stack = (struct SeqStack*)malloc(sizeof(SeqStack));
stack->top = -1;
return stack;
}
void Push_Stack(SeqStack *stack, int value)
{
if (stack->top == MAX_SIZE - 1)
{
printf("the stack is full.");
return;
}
stack->top++;
stack->data[stack->top] = value;
return;
}
int Pop_Stack(SeqStack *stack)
{
int value;
if (stack->top == -1)
{
printf("\nthe SeqStack is empty.");
return -1;
}
value = stack->data[stack->top];
stack->top--;
return value;
}
void FreeMemory_Stack(SeqStack* stack)
{
if (stack->top != -1)
{
return;
}
free(stack);
}
int main()
{
SeqStack *stack = Init_Stack();
Init_Stack(stack);
for (int i = 0; i < MAX_SIZE; i++)
{
Push_Stack(stack, i);
}
printf("出栈元素为:");
for (int i = 0; i < MAX_SIZE; i++)
{
int data = Pop_Stack(stack);
printf("%d ", data);
}
FreeMemory_Stack(stack);
return 0;
}
C 栈的链式存储结构
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkNode
{
int data;
struct LinkNode* next;
}LinkNode;
typedef struct LinkStack
{
LinkNode *top;
int size;
}LinkStack;
struct LinkStack* Init_Stack()
{
LinkStack *stack= (struct LinkStack*)malloc(sizeof(LinkStack));
stack->size = 0;
stack->top= (LinkNode*)malloc(sizeof(LinkNode));
stack->top = NULL;
}
void Push_Stack(LinkStack *stack,int value)
{
LinkNode *newNode= (LinkNode*)malloc(sizeof(LinkNode));
newNode->data =value;
newNode->next = stack->top;
stack->top = newNode;
stack->size++;
return;
}
int Pop_Stack(LinkStack *stack)
{
if (stack->size == 0)
{
return -1;
}
LinkNode *p;
int value = stack->top->data;
p = stack->top;
stack->top = stack->top->next;
free(p);
stack->size--;
return value;
}
void FreeMemory_Stack(LinkStack *stack)
{
if (stack->size!=0)
{
return;
}
free(stack);
}
int main()
{
LinkStack *stack = Init_Stack();
for (int i = 0; i < 6; i++)
{
Push_Stack(stack, i);
}
printf("出栈元素为");
for (int i = 0; i < 6; i++)
{
int data = Pop_Stack(stack);
printf("%d ", data);
}
FreeMemory_Stack(stack);
return 0;
}
C 栈的链式存储结构(简化)
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkStack
{
int data;
struct LinkStack* next;
}LinkStack;
LinkStack* Push_Stack(LinkStack *top, int value)
{
LinkStack *newNode = (LinkStack*)malloc(sizeof(LinkStack));
newNode->data = value;
newNode->next = top;
top = newNode;
return top;
}
LinkStack* Pop_Stack(LinkStack *top)
{
if (top)
{
LinkStack *p = top;
top = top->next;
printf("弹栈元素:%d\n", p->data);
free(p);
if (top == NULL)
{
printf("栈已空\n");
}
}
else {
printf("栈内没有任何元素\n");
}
return top;
}
void Print_Stack(LinkStack *top)
{
if(top == NULL)
{
printf("栈内没有任何元素\n");
return;
}
LinkStack *p;
while(top!=NULL)
{
p = top;
printf("%d ", p->data);
top = top->next;
free(p);
}
printf("\n栈已空\n");
}
int main()
{
LinkStack *top=NULL;
top = Push_Stack(top, 1);
top = Push_Stack(top, 5);
top = Push_Stack(top, 7);
top = Push_Stack(top, 2);
/*
top = Pop_Stack(top);
top = Pop_Stack(top);
top = Pop_Stack(top);
top = Pop_Stack(top);
*/
printf("出栈顺序为:");
Print_Stack(top);
return 0;
}