栈 Stack 介绍
栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。
向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;
从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
// 实现栈的入栈/出栈/便利/清空和判断栈是否为空。
#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define TRUE 1
#define FALSE 0
typedef int BOOL;
//定义节点
typedef struct node
{
int data;
struct node *next;
}NODE;
//定义栈结构体
typedef struct stack
{
NODE *top;
NODE *bottom;
}STACK;
void push(STACK *); //入栈
void pop(STACK *); //出栈
void print(STACK *); //栈的便利
BOOL isEmpty(STACK *); //判断栈是否为空
void clear(STACK *); //栈的清空
int main(void)
{
STACK s;
s.top = (NODE *)malloc(sizeof(NODE));
s.top->next = NULL;
s.top->data = 0;
s.bottom = s.top;
push(&s,100);
push(&s,200);
push(&s,300);
push(&s,400);
push(&s,500);
push(&s,600);
print(&s);
pop(&s);
print(&s);
pop(&s);
print(&s);
isEmpty(&s) ? printf("true\n") : printf("false\n");
clear(&s);
isEmpty(&s) ? printf("true\n") : printf("false\n");
free(s.top);
return 0;
}
void push(STACK *pStack,int i)
{
NODE *p = (NODE *)malloc(sizeof(NODE));
p->data = i;
p->next = pStack->top;
pStack->top = p;
return ;
}
void pop(STACK *pStack)
{
NODE *p = pStack->top;
if(isEmpty(pStack))
return ;
pStack->top = p->next;
free(p);
return ;
}
void print(STACK *pStack)
{
NODE *p = pStack->top;
if(isEmpty(pStack))
return ;
printf("%d\n",p->data);
while(p != pStack->bottom)
{
p = p->next;
printf("%d\n",p->data);
}
return ;
}
BOOL isEmpty(STACK *pStack)
{
return pStack->top == pStack->bottom ? TRUE : FALSE;
}
void clear(STACK *pStack)
{
NODE *p = pStack->top,*q;
if(isEmpty(pStack))
return ;
while(p != pStack->bottom)
{
q = p;
p = q->next;
free(q);
}
pStack->top = pStack->bottom;
return ;
}
知乎: @张赐荣
赐荣博客: www.prc.cx