标签:Node 头文件 top Linkedstack 源程序 linkedstack 链式 NULL stack
链式栈的实现(头文件及源程序)
Linkedstack.h
#ifndef __LINKEDSTACK_H__
#define __LINKEDSTACK_H__
//元素类型定义
typedef int ElemType_stack;
//结点结构体类型定义
typedef struct Node_stack
{
ElemType_stack data;
struct Node_stack *next;
struct Node_stack *front;
}Node_stack;
//栈类型定义
typedef struct Linkedstack
{
int num;
Node_stack *top; //栈顶指针
Node_stack *bottom; //栈底指针
}Linkedstack;
//创建一个空栈
Linkedstack *Init_stack();
//创建一个新结点
Node_stack *Create_New_Node_stack(ElemType_stack value);
//入栈
void push_stack(Linkedstack *linkedstack, ElemType_stack value);
//出栈
void pop_stack(Linkedstack *linkedstack);
//销毁一个栈
void destroy_stack(Linkedstack *linkedstack);
//读取栈顶元素
ElemType_stack get_stack_top(Linkedstack *linkedstack);
//判断栈空
int empty_stack(Linkedstack *linkedstack);
//获取栈的元素个数
int length_stack(Linkedstack *linkedstack);
#endif
Linkedstack.c
#include <stdio.h>
#include <stdlib.h>
#include "linkedstack.h"
//栈初始化
Linkedstack *Init_stack( )
{
Linkedstack *linkedstack = malloc(sizeof(Linkedstack));
linkedstack->num = 0;
linkedstack->top = NULL;
linkedstack->bottom = NULL;
return linkedstack;
}
//创建一个新结点
Node_stack *Create_New_Node_stack(ElemType_stack value)
{
Node_stack *new_node = malloc(sizeof(Node_stack));
new_node->data = value;
new_node->next = NULL;
new_node->front = NULL;
return new_node;
}
//入栈
void push_stack(Linkedstack *linkedstack, ElemType_stack value)
{
if (linkedstack == NULL)
{
printf("栈不存在!\n");
return;
}
Node_stack *new_node = Create_New_Node_stack(value);
if (linkedstack->num == 0)
{
linkedstack->top = new_node;
linkedstack->bottom = new_node;
new_node->front = linkedstack->top;
}
else
{
linkedstack->top->next = new_node;
new_node->front = linkedstack->top;
linkedstack->top = new_node;
}
linkedstack->num++;
}
//出栈
void pop_stack(Linkedstack *linkedstack)
{
if (linkedstack == NULL)
{
printf("栈不存在!\n");
return;
}
if (linkedstack->num == 1)
{
linkedstack->top->front = NULL;
free(linkedstack->top);
linkedstack->top = NULL;
linkedstack->bottom = NULL;
}
else
{
linkedstack->top = linkedstack->top->front;
free(linkedstack->top->next);
linkedstack->top->next = NULL;
}
linkedstack->num--;
}
//销毁一个栈
void destroy_stack(Linkedstack *linkedstack)
{
if (linkedstack == NULL)
{
printf("栈不存在!\n");
return;
}
else
{
while (linkedstack->num != 0)
{
pop_stack(linkedstack);
}
//free(linkedstack);
linkedstack = NULL;
}
}
//读取栈顶元素
ElemType_stack get_stack_top(Linkedstack *linkedstack)
{
if (linkedstack == NULL)
{
printf("栈不存在!\n");
return 0;
}
if (linkedstack->num == 0)
{
printf("栈已空,获取栈顶元素失败!");
return 000;
}
else
{
return linkedstack->top->data;
}
}
//判断栈空
int empty_stack(Linkedstack *linkedstack)
{
if (linkedstack == NULL)
{
printf("栈不存在!\n");
return -1;
}
if (linkedstack->num == 0)
{
return 1; //栈空
}
else
{
return 0; //栈非空
}
}
//获取栈的元素个数
int length_stack(Linkedstack *linkedstack)
{
if (linkedstack == NULL)
{
printf("链表不存在!\n");
return 0;
}
return linkedstack->num;
}
标签:Node,头文件,top,Linkedstack,源程序,linkedstack,链式,NULL,stack 来源: https://blog.csdn.net/m0_48607658/article/details/118996994
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。