二、链式栈
构造链式栈
//链式栈的有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
//构造单链式栈的结点
typedef struct LinkedStack {
DataType_t data; //结点的数据域
struct LinkedStack *next; //结点的的指针域
} LinStack_t;
typedef struct LinkedStackTop {
LinStack_t *top; //链栈的栈顶
} StaTop_t;
//创建链栈为空
StaTop_t *LinkedStack_Create() {
StaTop_t *stackTop = (LinStack_t *) calloc(1, sizeof(StaTop_t));
if (NULL == stackTop) {
perror("calloc memory for Head is Failed");
exit - 1;
}
stackTop->top = NULL;
return stackTop;
}
//创建链栈的新结点
LinStack_t *LinStack_newNode(DataType_t data) {
LinStack_t *newNode = (LinStack_t *) calloc(1, sizeof(LinStack_t));
if (NULL == newNode) {
perror("calloc memory for Head is Failed");
exit - 1;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//入栈
bool LinStack_Push(StaTop_t *stackTop, DataType_t data) {
LinStack_t *newNode = LinStack_newNode(data);
//判断是否为空栈
if (NULL == stackTop->top) {
stackTop->top = newNode;
} else {
newNode->next = stackTop->top;
stackTop->top = newNode;
}
return true;
}
//出栈
DataType_t LinStack_Pop(StaTop_t *stackTop) {
//判断是否为空栈
if (NULL == stackTop->top) {
perror(" the LinkedStack is empty");
} else {
LinStack_t *temp = stackTop->top;
stackTop->top = temp->next;
temp->next = NULL;
//返回数据
return temp->data;
}
}
标签:NULL,LinStack,stackTop,top,链式,newNode,数据结构,data
From: https://www.cnblogs.com/Mr--Song/p/18160868