//main.cpp
#include"linkstack.h"
int main()
{
linkstack top;
int i = 0;
int data = 0;
top = creat_stack();
for (i = 5; i > 0; i--)
{
push_stack(top, i);
}
show_stack(top);
/*while (empty_stack(top))
{
printf("当前栈内元素为:%-4d", top->next->data);
}*/
data=get_stack(top);
printf("当前栈顶元素为:%d\n", data);
pop_stack(top);
show_stack(top);
data = get_stack(top);
printf("当前栈顶元素为:%d\n", data);
//printf("当前栈内元素为:%-4d", top->next->data);
celar_stack(top);
if (empty_stack(top))
{
printf("当前栈为空:\n");
}
free_stack(top);
system("pause");
return 0;
}
//linkstack.cpp
#include"linkstack.h"
/*
* @file linkstack.cpp
* @function 创建一个空链栈函数
* @author 酸菜。
* @date 2019-10-29
*/
linkstack creat_stack()
{
linkstack s = NULL;
s = (linkstack)malloc(sizeof(linknode));
if (s == NULL)
{
printf("链栈创建失败:\n");
return NULL;
}
s->data = 0;
s->next = NULL;
return s;
}
/*
* @file linkstack.cpp
* @function 释放链栈函数
* @author 酸菜。
* @date 2019-10-29
*/
void free_stack(linkstack top)
{
linkstack p = NULL;
p = top;
while (p)
{
top = top->next;
free(p);
p = top;
}
return;
}
/*
* @file linkstack.cpp
* @function 清空链栈函数
* @author 酸菜。
* @date 2019-10-29
*/
void celar_stack(linkstack top)
{
linkstack p = NULL;
p = top->next;
while (p)
{
top->next = p->next;
free(p);
p = top->next;
}
return;
}
/*
* @file linkstack.cpp
* @function 判断链栈是否为空
* @author 酸菜。
* @date 2019-10-29
*/
int empty_stack(linkstack top)
{
return (top->next == NULL ? 1 : 0);
}
/*
* @file linkstack.cpp
* @function 出栈函数
* @author 酸菜。
* @date 2019-10-29
*/
int pop_stack(linkstack top)
{
linkstack p;
int data = 0;
p = top->next;
top->next = p->next;
data = p->data;
free(p);
p = NULL;
return data;
}
/*
* @file linkstack.cpp
* @function 进栈函数
* @author 酸菜。
* @date 2019-10-29
*/
int push_stack(linkstack top, int value)
{
linkstack p = NULL;
p = (linkstack)malloc(sizeof(linknode));
if (p == NULL)
{
printf("压栈失败:\n");
return -1;
}
p->data = value;
p->next = top->next;
top->next = p;
return 1;
}
/*
* @file linkstack.cpp
* @function 获取栈顶元素
* @author 酸菜。
* @date 2019-10-29
*/
int get_stack(linkstack top)
{
if (top->next == NULL)
{
printf("栈为空:无法获取栈顶元素\n");
return -1;
}
return top->next->data;
}
/*
* @file linkstack.cpp
* @function 遍历栈函数
* @author 酸菜。
* @date 2019-10-29
*/
void show_stack(linkstack top)
{
while (top&&top->next)
{
printf("当前栈内元素为:%-4d\n", top->next->data);
top = top->next;
}
}
//linkstack.h
#ifndef linkstack_h
#define linkstack_h
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
typedef struct linknode
{
int data;
struct linknode* next;
}linknode,*linkstack;
extern linkstack creat_stack();
extern void free_stack(linkstack top);
extern void celar_stack(linkstack top);
extern int empty_stack(linkstack top);
extern int pop_stack(linkstack top);
extern int push_stack(linkstack top, int value);
extern int get_stack(linkstack top);
extern void show_stack(linkstack top);
#endif // !linkstack_h
注:和顺序栈相比,链栈没有判断栈满的操作。
顺序栈:实现需要使用数组,数组的元素在内存中的存储位置是连续的;且需要知道数组的长度才可以使用;无法避免溢出问题;当系统给数组分配了内存空间,其他的任务是不能使用这个内存空间的;存储密度=1;顺序栈的top指针指向的是栈顶的空元素处,top-1才是指向栈顶元素;不易实现插入和删除操作。
链栈:实现使用链表,链表的元素存储在不同的地址;动态申请地址,即可以以非常小的内存空间开始;当某项不使用内存时,可以将内存返还给系统;存储密度<1;链栈的top指针相当于链表中的head指针,即指向实在的元素;相比于顺序栈易实现插入和删除操作且不易出现栈满的情况。
链栈存储示意图: