2 Star 0 Fork 0

mtgo/dataStructure_alogrithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
link_stack.c 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
mtgo 提交于 2022-02-22 21:51 . commit all file
//栈的链式存储企业版(即结点只维护指针域,数据域由用户自己维护)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//结点的结构体
struct stackNode{
struct stackNode *next; //只维护指针域
};
//栈的结构体
struct LStack{
struct stackNode pHeader;
int m_size;
};
typedef void * LinkStack;
//初始化链栈
//头结点端做栈顶,方便出栈入栈。因为尾端做栈顶每次出栈入栈都要遍历找尾结点。
LinkStack init_stack(){
struct LStack *mylstack=malloc(sizeof(struct LStack));
if (mylstack==NULL)
{
return NULL;
}
mylstack->pHeader.next=NULL;
mylstack->m_size=0;
return mylstack;
}
//入栈
void push_stack(LinkStack lstack,void *data){
if (lstack==NULL)
{
return;
}
if (data==NULL)
{
return;
}
struct LStack *mylstack=lstack;
//将用户数据取前四个字节用
struct stackNode *pNode=data;
pNode->next=mylstack->pHeader.next;
mylstack->pHeader.next=pNode;
//更新链表长度
mylstack->m_size++;
}
//出栈
void pop_LinkStack(LinkStack stack)
{
//出栈本质 --- 链表头删
if (stack == NULL)
{
return;
}
struct LStack * myStack = stack;
if (myStack->m_size == 0) //栈为空
{
return;
}
//更改指针指向
//缓存第一个有数据节点
struct stackNode * pFirst = myStack->pHeader.next;
myStack->pHeader.next = pFirst->next;
//更新栈大小
myStack->m_size--;
}
//返回栈顶元素
void * top_LinkStack(LinkStack stack)
{
if (stack == NULL)
{
return NULL;
}
struct LStack * myStack = stack;
if (myStack->m_size == 0)
{
return NULL;
}
return myStack->pHeader.next;
}
//返回栈个数
int size_LinkStack(LinkStack stack)
{
if (stack == NULL)
{
return -1;
}
struct LStack * myStack = stack;
return myStack->m_size;
}
//判断是否为空
int isEmpty_LinkStack(LinkStack stack)
{
if (stack == NULL)
{
return -1;
}
struct LStack * myStack = stack;
if (myStack->m_size == 0)
{
return 1;
}
return 0;
}
//销毁
void destroy_LinkStack(LinkStack stack)
{
if (stack == NULL)
{
return;
}
free(stack);
stack = NULL;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/mtgo666/data-structure-and-algorithm.git
git@gitee.com:mtgo666/data-structure-and-algorithm.git
mtgo666
data-structure-and-algorithm
dataStructure_alogrithm
master

搜索帮助