栈的特点是后进先出或先进后出,简称LIFO或FILO,通常top时刻表示栈顶的位置序号,一般空栈时top=-1;入栈栈顶指针加1,s->top++;出栈栈顶指针减1,s->top--
【顺序栈】
定义:
typedef struct
{
int date[MAX];
int top;
} SeqStack,*Stack;
置空栈:
Stack Init_SeqStack()//置空栈
{
Stack s;
s=(SeqStack*)malloc(sizeof(SeqStack));
if(!s)
{
printf("空间不足\n");
return NULL;
}
else
{
s->top = -1;
return s;
}
}
判断是否为空栈:
int Empty_Stack(Stack s)//判断是否空栈
{
if (s->top == -1)
return 1;
else
return 0;
}
进栈:
int Push_SeqStack(SeqStack* s,int x)//进栈
{
if (s->top == MAX - 1)
{
printf("栈满!");
return 0;
}
else
{
s->top++;
s->date[s->top] = x;
return 1;
}
}
出栈:
int Pop_SeqStack(SeqStack* s,int* x)//出栈
{
if (s->top == -1)
return 0;
else
{
*x = s->date[s->top];//(上面定义了一个指针,用指针来接受数据)
s->top--;
return 1;
}
}
输出栈中元素:
int printStack(Stack s)//输出
{
int top = s->top;
while (top != -1)
printf("%4d", s->date[top--]);
printf("\n");
return 0;
}
int GetTop(Stack s)
{
int top = s->top;
return s->date[top];
}
输出栈顶:
int GetTop(Stack s)
{
int top = s->top;
return s->date[top];
}
以下为一个顺序栈,实现栈的入栈、出栈并将其中元素输出功能
cs
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
typedef struct
{
int date[MAX];
int top;
} SeqStack,*Stack; //SeqStack*==Stack
Stack Init_SeqStack()//置空栈
{
Stack s;
s=(SeqStack*)malloc(sizeof(SeqStack));
if(!s)
{
printf("空间不足\n");
return NULL;
}
else
{
s->top = -1;
return s;
}
}
int Empty_Stack(Stack s)//判断是否空栈
{
if (s->top == -1)
return 1;
else
return 0;
}
int Push_SeqStack(Stack s,int x)//进栈
{
if (s->top == MAX - 1)
{
printf("栈满!");
return 0;
}
else
{
s->top++;
s->date[s->top] = x;
return 1;
}
}
int Pop_SeqStack(Stack s,int* x)//出栈
{
if (s->top == -1)
return 0;
else
{
*x = s->date[s->top];//(上面定义了一个指针,用指针来接受数据)
s->top--;
return 1;
}
}
int printStack(Stack s)//输出
{
int top = s->top;
while (top != -1)
printf("%4d", s->date[top--]);
printf("\n");
return 0;
}
int GetTop(Stack s)
{
int top = s->top;
return s->date[top];
}
int main()
{
Stack s;
int x;
s=Init_SeqStack();
Empty_Stack(s);
printf("请输入入栈数字:\n");
scanf("%d", &x);
while (x != 0)
{
Push_SeqStack(s,x);
printf("请输入入栈数字:\n");
scanf("%d", &x);
}
printStack(s);
printf("栈顶:%d\n",GetTop(s));
printf("出栈测试:");
Pop_SeqStack(s,&x);//这个函数上面x定义的为指针,所以要加取地址符
printStack(s);
return 0;
}
【链栈】
定义:
typedef struct node
{
int date;
struct node* next;
}StackNode, * LinkStack;
实现链栈的入栈、出栈并将其中元素输出功能
cs
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int date;
struct node* next;
}StackNode, * LinkStack;
void Init_LinkStack(LinkStack &p)//初始化
{
p = (LinkStack)malloc(sizeof(StackNode));
p->date = 0;
p->next = NULL;
}
int Empty_LinkStack(LinkStack top)
{
if (top == NULL)
return 1;
else
return 0;
}
void Push_LinkStack(LinkStack &top, int x)//入栈
{
LinkStack s;
s = (LinkStack)malloc(sizeof(StackNode));
s->date = x;
s->next = top->next;
top->next = s;
}
LinkStack Pop_LinkStack(LinkStack &top, int &x)//出栈
{
LinkStack p;
if (top->next == NULL)
return NULL;
else
{
p = top->next;
x = p->date;
top->next = p->next;
free(p);
return top;
}
}
int GetTop(LinkStack s, int x)//栈顶
{
if (s->next == NULL)
return NULL;
x = s->next->date;
return x;
}
void printStack(LinkStack top)//输出
{
while (top != NULL)
{
printf("%4d", top->date);
top = top->next;
}
printf("\n");
}
int main()
{
LinkStack top;
int x=0;
Init_LinkStack(top);
Empty_LinkStack(top);
printf("请输入入栈元素:\n");
scanf("%d", &x);
while (x != 0)
{
Push_LinkStack(top, x);
printf("请输入入栈元素:\n");
scanf("%d", &x);
}
printStack(top);
printf("栈顶为:%d\n", GetTop(top, x));
printf("出栈测试:\n");
Pop_LinkStack(top, x);
if (top != NULL)
{
printStack(top);
}
else
printf("栈空,无法输出!");
return 0;
}
注意:链栈中的top是我所定义的一个指针,与顺序栈不同
求助:在链栈的代码中,输出栈中元素时,一直会在输出栈中元素的前端出现一个0(我在初始化函数中,将p->date赋初值为0),如何修改代码