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;

}

相关推荐
梦帮科技5 分钟前
量子张量网络破局大模型:从矩阵乘积态 (MPS) 到张量列 (TT-SVD) 低秩收缩全推导
网络·数据结构·数据库·线性代数·矩阵·架构·模拟退火算法
android阿杜7 分钟前
Ubuntu 26.04 LTS 系统每次重启后屏幕亮度都会增加一点
linux·运维·ubuntu
懿路向前18 分钟前
【唤醒实战笔记】2026-09-29 | 智能体提示词迭代史(v1 → v8)
linux·笔记·算法
大侠归来1 小时前
C语言内存管理:从栈到堆的完整指南
c语言·开发语言·python
cu1431 小时前
细谈GM7123C的具体功能与其应用
c语言·c++·人工智能·嵌入式硬件
LOVE️YOU1 小时前
Python 数据结构的本质:位置、对象引用、Hash 与可变性
数据结构·python·哈希算法
不会就选b1 小时前
算法日常・每日刷题--<动态规划>1
java·数据结构·算法
shehuiyuelaiyuehao1 小时前
算法54,链表 两数相加
数据结构·算法·链表
路溪非溪1 小时前
Linux下Shell脚本基础知识(二)
linux·运维·服务器
嵌入式分享2 小时前
驱动之路#44:硬件 I2C 和软件 I2C 谁更坑?
linux·嵌入式硬件