链式栈的简单练习
/***********************************************************************
* Description:栈的链式存储结构
* 本次练习主要是对于栈的链式存储结构的练习
* 主要包括栈的初始化、出栈、入栈、判空栈、输出栈中的内容等等
* Author:qichaoqun
* Date:2019/08/08
***********************************************************************/
#include "stdlib.h"
#include "stdio.h"
typedef int ElemType;
/**
* 定义栈的结点
*/
typedef struct SNode {
//数值域
ElemType data;
//指针域
SNode *next;
};
/**
* 定义用于存放栈的信息的结构体
*/
typedef struct {
//栈顶指针
SNode *top;
//栈中结点的个数
int count;
} StackLink;
void showMenu();
bool initStack(StackLink &stackLink);
bool isEmptyStack(StackLink &stackLink);
bool pushStack(StackLink &stackLink,int value);
bool outStack(StackLink &stackLink);
int getTopValue(StackLink &stackLink);
void getValues(StackLink &stackLink);
int main() {
bool isExit = false;
//定义链栈
StackLink stackLink;
while (!isExit) {
showMenu();
int option;
scanf("%d", &option);
switch (option) {
case 1:
//初始化链栈
if(initStack(stackLink)){
printf("初始化成功\n");
}else{
printf("初始化失败\n");
}
break;
case 2:
//判断栈是否为空
if(isEmptyStack(stackLink)){
printf("该栈为空栈\n");
}else{
printf("该栈不为空\n");
}
break;
case 3:
//进栈
printf("请输入你要进栈的值 ");
int value;
scanf("%d",&value);
if(pushStack(stackLink,value)){
printf("进栈成功\n");
}else{
printf("进栈失败\n");
}
break;
case 4:
//出栈
if(outStack(stackLink)){
printf("出栈成功\n");
}else{
printf("出栈失败\n");
}
break;
case 5: {
//获取栈顶元素
int topValue = getTopValue(stackLink);
if(topValue != -65535){
printf("栈顶的元素为 %d\n",topValue);
}else{
printf("获取栈顶元素失败\n");
}
break;
}
case 6:
//查看栈中的所有的元素
getValues(stackLink);
break;
case 7:
//退出
isExit = true;
break;
default:
printf("输入有误请重新输入");
break;
}
}
}
/**
* 查看栈中的所有的元素
*/
void getValues(StackLink &stackLink){
//定义辅助指针
SNode* sLink = stackLink.top;
printf("栈中的元素为 \n");
while (sLink){
printf(" %d\n",sLink->data);
sLink = sLink->next;
}
}
/**
* 获取栈顶元素
*/
int getTopValue(StackLink &stackLink){
if(stackLink.top != nullptr){
//栈中非空的时候,则得到栈顶的元素
return stackLink.top->data;
}
return -65535;
}
/**
* 出栈
* 出栈的时候同样也需要考虑栈是否为空
*/
bool outStack(StackLink &stackLink){
if(stackLink.top == nullptr){
//栈中为空栈,则不能出栈
return false;
}
//栈中不为空,则可以进行出栈
//定义辅助指针
SNode* node = stackLink.top;
stackLink.top = stackLink.top->next;
free(node);
stackLink.count--;
return true;
}
/**
* 进栈操作
* 与顺序栈不同的是链栈在进栈的时候并不需要判断是否栈满
*
*/
bool pushStack(StackLink &stackLink,int value){
//定义新的栈的结点
SNode* sNode = (SNode*)malloc(sizeof(SNode));
sNode->data = value;
if(stackLink.count == 0){
//如果是第一个栈的结点
stackLink.top = sNode;
sNode->next = nullptr;
stackLink.count++;
}else{
//如果不是第一个栈的结点
sNode->next = stackLink.top;
stackLink.top = sNode;
stackLink.count++;
}
return true;
}
/**
* 判断栈是否为空
*/
bool isEmptyStack(StackLink &stackLink){
if(stackLink.top == nullptr){
return true;
}
return false;
}
/**
* 初始化链栈
*/
bool initStack(StackLink &stackLink){
stackLink.top = nullptr;
stackLink.count = 0;
return true;
}
void showMenu() {
printf(" 对于链栈的练习\n");
printf("1 初始化链栈\n");
printf("2 判断栈是否为空\n");
printf("3 进栈\n");
printf("4 出栈\n");
printf("5 获取栈顶元素\n");
printf("6 查看栈中所有元素\n");
printf("7 退出\n");
printf("请输入你的选择 ");
}