学习贺利坚老师, 链栈 , 构建链栈算法库
版本更新日志:
V1.0: 完成基本功能
目录
V1.0
功能函数:
//(1) 初始化栈
void Init_chainStack(chain_Stack *&init_Stack);
//(2) 销毁栈
void Destroy_chainStack(chain_Stack *&destroy_Stack);
//(3) 输出展示链栈
void Display_chainStack(chain_Stack *show_Stack);
//(4) 把一个数据压入链栈(入栈)
void Push_chainStack(chain_Stack *&push_Stack, ElemType push_value);
//(5) 把栈顶的数据弹出链栈(出栈)
bool Pop_chainStack(chain_Stack *&pop_Stack, ElemType &pop_value);
//(6) 判断链栈是否为空
bool Empty_chainStack(chain_Stack *judge_Stack);
//(7) 求链栈中元素个数--栈长度
int Length_chainStack(chain_Stack *measure_Stack);
//(8) 访问链栈的栈顶元素
bool GetTop_chainStack(chain_Stack *visited_Stack, ElemType &get_value);
chain_stack.h
#ifndef CHAIN_STACK_H_INCLUDE
#define CHAIN_STACK_H_INCLUDE
#include <stdio.h>
#include <malloc.h>
typedef char ElemType;
typedef struct stack_Node //链栈有必要有节点
{
ElemType data; //数据域
struct stack_Node *next; //指针域
}chain_Stack;
//(1) 初始化栈
void Init_chainStack(chain_Stack *&init_Stack);
//(2) 销毁栈
void Destroy_chainStack(chain_Stack *&destroy_Stack);
//(3) 输出展示链栈
void Display_chainStack(chain_Stack *show_Stack);
//(4) 把一个数据压入链栈(入栈)
void Push_chainStack(chain_Stack *&push_Stack, ElemType push_value);
//(5) 把栈顶的数据弹出链栈(出栈)
bool Pop_chainStack(chain_Stack *&pop_Stack, ElemType &pop_value);
//(6) 判断链栈是否为空
bool Empty_chainStack(chain_Stack *judge_Stack);
//(7) 求链栈中元素个数--栈长度
int Length_chainStack(chain_Stack *measure_Stack);
//(8) 访问链栈的栈顶元素
bool GetTop_chainStack(chain_Stack *visited_Stack, ElemType &get_value);
#endif
chain_stack.cpp
#include "chain_stack.h"
/**************************************************
函数名: Init_chainStack
功 能: 初始化一个链栈
参 数: chain_Stack *&init_Stack: 要进行初始化的链栈
返回值: 无
**************************************************/
void Init_chainStack(chain_Stack *&init_Stack)
{
init_Stack = (chain_Stack *)malloc(sizeof(chain_Stack));
init_Stack->next = NULL;
}
/**************************************************
函数名: Destroy_chainStack
功 能: 销毁链栈,释放空间
参 数: chain_Stack *&destroy_Stack:要销毁的链栈
返回值: 无
**************************************************/
void Destroy_chainStack(chain_Stack *&destroy_Stack)
{
chain_Stack *destroyNode,*nextNode;
destroyNode = destroy_Stack; //初始时指向头结点
nextNode = destroyNode->next;
while(nextNode != NULL)
{
free(destroyNode);
destroyNode = nextNode;
nextNode = destroyNode->next;
}
free(destroyNode);
}
/**************************************************
函数名: Display_chainStack
功 能: 从栈顶到栈底,展示栈内元素
参 数: chain_Stack *show_Stack:要展示的链栈
返回值: 无
**************************************************/
void Display_chainStack(chain_Stack *show_Stack)
{
chain_Stack *display_Node = show_Stack->next;
while(display_Node != NULL)
{
printf("%c ",display_Node->data);
display_Node = display_Node->next;
}
printf("\n");
}
/**************************************************
函数名: Push_chainStack
功 能: 把一个数据压入链栈(入栈)
参 数: (1)chain_Stack *&push_Stack:要压栈的链栈
(2)ElemType push_value:压栈元素值
返回值: 无
**************************************************/
void Push_chainStack(chain_Stack *&push_Stack, ElemType push_value)
{
//链栈不会满
chain_Stack *push_Node;
push_Node = (chain_Stack *)malloc(sizeof(chain_Stack));
push_Node->data = push_value; //新建链栈节点
push_Node->next = push_Stack->next;//插入头结点之后, 为栈顶
push_Stack->next = push_Node; //头结点后继指向 栈顶
}
/**************************************************
函数名: Pop_chainStack
功 能: 把栈顶的数据弹出链栈(出栈)
参 数: (1)chain_Stack *&pop_Stack: 要弹出栈顶元素的链栈
(2)ElemType &pop_value: 出栈值
返回值: bool:是否弹出栈顶元素? true(栈非空):false(栈空)
**************************************************/
bool Pop_chainStack(chain_Stack *&pop_Stack, ElemType &pop_value)
{
bool finished;
//头结点之后元素进行出栈
chain_Stack *pop_Node;
if(pop_Stack->next == NULL)
{
finished = false;
}
else //非空,则出栈元素
{
pop_Node = pop_Stack->next; //指向出栈节点
pop_value = pop_Node->data; //传回出栈节点数值
pop_Stack->next = pop_Node->next; //弹出栈节点
free(pop_Node); //释放pop_Node
finished = true;
}
return finished;
}
/**************************************************
函数名: Empty_chainStack
功 能: 判断链栈是否为空
参 数: (1)chain_Stack *judge_Stack:要进行判断的链栈
返回值: bool: 链栈是否为空? true(空):false(非空)
**************************************************/
bool Empty_chainStack(chain_Stack *judge_Stack)
{
return (judge_Stack->next == NULL);
}
/**************************************************
函数名: Length_chainStack
功 能: 链栈的元素个数
参 数: chain_Stack *measure_Stack:要进行计算的链栈
返回值: int: 元素个数
**************************************************/
int Length_chainStack(chain_Stack *measure_Stack)
{
int counter = 0;
chain_Stack *measure_Node;
measure_Node = measure_Stack->next;
while(measure_Node != NULL)
{
counter++;
measure_Node = measure_Node->next;
}
return counter;
}
/**************************************************
函数名: GetTop_chainStack
功 能: 得到栈顶元素的值
参 数: (1)chain_Stack *visited_Stack:要进行访问的链栈
(2)ElemType &get_value:得到的栈顶元素值
返回值: bool: 是否得到栈顶元素值 ? true(栈非空):false(栈空)
**************************************************/
bool GetTop_chainStack(chain_Stack *visited_Stack, ElemType &get_value)
{
bool finished;
if(visited_Stack->next == NULL)
{
finished = false;
}
else
{
get_value = visited_Stack->next->data;
finished = true;
}
return finished;
}
main.cpp
#include <stdio.h>
#include "chain_stack.h"
int main()
{
ElemType elem;
chain_Stack *test_1;
printf("\n(1)初始化链栈test_1\n");
Init_chainStack(test_1);
printf("\n(2)链栈为%s\n",(Empty_chainStack(test_1) ? "空":"非空"));
printf("\n(3)依次进栈元素为 a,b,c,d,e,\n");
Push_chainStack(test_1,'a');
Push_chainStack(test_1,'b');
Push_chainStack(test_1,'c');
Push_chainStack(test_1,'d');
Push_chainStack(test_1,'e');
printf("\n(4)链栈为%s\n",(Empty_chainStack(test_1) ? "空":"非空"));
printf("\n(5)链栈的长度:%d\n",Length_chainStack(test_1));
printf("(6)从链栈顶到栈底的元素:");Display_chainStack(test_1);
printf("\n(7)出链栈序列:\n");
while(!Empty_chainStack(test_1))
{
Pop_chainStack(test_1,elem);
printf("%c ",elem);
}
printf("\n(8)链栈为%s\n",(Empty_chainStack(test_1) ? "空":"非空"));
printf("(9)释放链栈\n");
Destroy_chainStack(test_1);
return 0;
}