C语言数据结构-链式栈

头文件:stack.h

#ifndef STACK_H

#define STACK_H

#include <stdio.h>

#include <stdlib.h>

typedef int DataType;

/* 链式栈节点类型 */

typedef struct staNode

{

DataType data;

struct staNode *pNext;

}StackNode;

/* 链式栈标签类型 */

typedef struct list

{

StackNode *pTop; //指向栈顶指针

int cLen;

}StackList;

extern StackList *createSatckList();

extern int isEmptyStackList(StackList *);

extern int pushStackList(StackList *, DataType );

extern int popStackList(StackList *, DataType *);

extern DataType getStackTop(StackList *);

extern void clearStackList(StackList *);

extern void destroyStackList(StackList **);

extern void showStackList(StackList *pList);

#endif

源文件:stack.c main.c

stack.c

#include "stack.h"

StackList *createSatckList()

{

StackList *pList = malloc(sizeof(StackList));

if (NULL == pList)

{

perror("fail to malloc");

return NULL;

}

pList->pTop = NULL;

pList->cLen = 0;

return pList;

}

int isEmptyStackList(StackList *pList)

{

return pList->cLen == 0;

}

入栈:

int pushStackList(StackList *pList, DataType data)

{

StackNode *pTmpNode = malloc(sizeof(StackNode));

if (NULL == pTmpNode)

{

perror("fail to malloc");

return -1;

}

pTmpNode->data = data;

pTmpNode->pNext = NULL;

pTmpNode->pNext = pList->pTop;

pList->pTop = pTmpNode;

pList->cLen++;

return 0;

}

遍历:

void showStackList(StackList *pList)

{

StackNode *pTmpNode = pList->pTop;

while (pTmpNode != NULL)

{

printf("%d ", pTmpNode->data);

pTmpNode = pTmpNode->pNext;

}

printf("\n");

}

出栈:

int popStackList(StackList *pList, DataType *pOutData)

{

if (isEmptyStackList(pList))

{

return 0;

}

StackNode *pTmpNode = pList->pTop;

if (NULL != pOutData)

{

*pOutData = pTmpNode->data;

}

pList->pTop = pTmpNode->pNext;

free(pTmpNode);

pList->cLen--;

return 0;

}

DataType getStackTop(StackList *pList)

{

if (!isEmptyStackList(pList))

{

return pList->pTop->data;

}

}

void clearStackList(StackList *pList)

{

while (pList->pTop != NULL)

{

popStackList(pList, NULL);

}

}

销毁:

void destroyStackList(StackList **ppList)

{

clearStackList(*ppList);

free(*ppList);

*ppList = NULL;

}

main.c

#include "stack.h"

int main(int argc, const char *argv[])

{

StackList *pList = NULL;

DataType OutData;

pList = createSatckList();

pushStackList(pList, 1);

pushStackList(pList, 2);

pushStackList(pList, 3);

pushStackList(pList, 4);

showStackList(pList);

popStackList(pList, &OutData);

printf("pop data = %d\n", OutData);

showStackList(pList);

destroyStackList(&pList);

return 0;

}

相关推荐
Sumlll_1 天前
Ubuntu系统下QEMU的安装与RISC-V的测试
linux·ubuntu·risc-v
猫头虎1 天前
2025最新OpenEuler系统安装MySQL的详细教程
linux·服务器·数据库·sql·mysql·macos·openeuler
你怎么知道我是队长1 天前
C语言---typedef
c语言·c++·算法
带土11 天前
5. enum(枚举)关键字在C/C++中的作用
c语言·c++
晚风吹人醒.1 天前
SSH远程管理及访问控制
linux·运维·ssh·scp·xshell·访问控制·远程管理
Uncertainty!!1 天前
Linux多用户情况下个别用户输入密码后黑屏
linux·远程连接
necessary6531 天前
使用Clion查看linux环境中的PG源码
linux·运维·服务器
小猪佩奇TONY1 天前
Linux 内核学习(14) --- linux x86-32 虚拟地址空间
linux·学习
Lam㊣1 天前
Centos 7 系统docker:更换镜像源
linux·docker·centos
FL16238631291 天前
win11+WSL+Ubuntu-xrdp+远程桌面闪退+黑屏闪退解决
linux·运维·ubuntu