(一)栈的链式结构体
typedef int Status;
typedef int EleType;
typedef struct StackNode {
EleType data;
struct StackNode* next;
}StackNode,* LinkStackPoi;
typedef struct LinkStack {
LinkStackPoi top;
int count;
}LinkStack;
(二)创建一个空栈
Status InitLinkStack(LinkStack* stack)
{
if (!stack)
{
return ERROR;
}
stack->top = NULL;
stack->count = 0;
return OK;
}
(三)清空栈
Status ClearLinkStack(LinkStack* stack)
{
if (!stack||!stack->count)
{
return ERROR;
}
while (stack->count)
{
StackNode* node = stack->top;
stack->top = node->next;
free(node);
stack->count--;
}
return OK;
}
(四)判空
Status EmptyLinkStack(LinkStack* stack) {
if (!stack)
{
return ERROR;
}
return stack->count == 0 ? 1 : 0;
}
(五)获取元素个数
int GetLengthLinkStack(LinkStack* stack)
{
if (!stack )
{
return -1;
}
return stack->count;
}
(六)入栈
Status push(LinkStack* stack,EleType e)
{
if (!stack)
{
return ERROR;
}
StackNode* node = (StackNode*)malloc(sizeof(StackNode));
node->next = stack->top;
node->data = e;
stack->top = node;
stack->count++;
return OK;
}
(七)出栈
Status pop(LinkStack* stack,EleType *e)
{
if (!stack && stack->count)
{
return ERROR;
}
StackNode* node = stack->top;
*e = node->data;
stack->top = node->next;
free(node);
stack->count--;
return OK;
}
(八)输出
void PrintfLinkStack(LinkStack* stack)
{
if (!stack&&stack->count)
{
return;
}
StackNode* node = stack->top;
while (node)
{
printf("%d,", node->data);
node = node->next;
}
puts("");
return;
}
(八)调用的主函数
int main(int argc, char *argv[])
{
LinkStack stack;
InitLinkStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
push(&stack, 4);
push(&stack, 5);
puts("链栈元素:");
PrintfLinkStack(&stack);
printf("链栈元素个数:%d\n", GetLengthLinkStack(&stack));
EleType e1,e2;
pop(&stack, &e1);
printf("弹出第一个元素:%d\n", e1);
pop(&stack, &e2);
printf("弹出第二个元素:%d\n", e2);
puts("链栈元素:");
PrintfLinkStack(&stack);
printf("链栈元素个数:%d", GetLengthLinkStack(&stack));
printf("\n");
return 0;
}
全部的组合代码
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int EleType;
typedef struct StackNode {
EleType data;
struct StackNode* next;
}StackNode,* LinkStackPoi;
typedef struct LinkStack {
LinkStackPoi top;
int count;
}LinkStack;
Status InitLinkStack(LinkStack* stack)
{
if (!stack)
{
return ERROR;
}
stack->top = NULL;
stack->count = 0;
return OK;
}
Status ClearLinkStack(LinkStack* stack)
{
if (!stack||!stack->count)
{
return ERROR;
}
while (stack->count)
{
StackNode* node = stack->top;
stack->top = node->next;
free(node);
stack->count--;
}
return OK;
}
Status EmptyLinkStack(LinkStack* stack) {
if (!stack)
{
return ERROR;
}
return stack->count == 0 ? 1 : 0;
}
int GetLengthLinkStack(LinkStack* stack)
{
if (!stack )
{
return -1;
}
return stack->count;
}
Status GetTop(LinkStack* stack, StackNode** stackNode)
{
if (!stack)
{
return ERROR;
}
*stackNode = stack->top;
return OK;
}
Status pop(LinkStack* stack,EleType *e)
{
if (!stack && stack->count)
{
return ERROR;
}
StackNode* node = stack->top;
*e = node->data;
stack->top = node->next;
free(node);
stack->count--;
return OK;
}
Status push(LinkStack* stack,EleType e)
{
if (!stack)
{
return ERROR;
}
StackNode* node = (StackNode*)malloc(sizeof(StackNode));
node->next = stack->top;
node->data = e;
stack->top = node;
stack->count++;
return OK;
}
void PrintfLinkStack(LinkStack* stack)
{
if (!stack&&stack->count)
{
return;
}
StackNode* node = stack->top;
while (node)
{
printf("%d,", node->data);
node = node->next;
}
puts("");
return;
}
int main(int argc, char *argv[])
{
LinkStack stack;
InitLinkStack(&stack);
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
push(&stack, 4);
push(&stack, 5);
puts("链栈元素:");
PrintfLinkStack(&stack);
printf("链栈元素个数:%d\n", GetLengthLinkStack(&stack));
EleType e1,e2;
pop(&stack, &e1);
printf("弹出第一个元素:%d\n", e1);
pop(&stack, &e2);
printf("弹出第二个元素:%d\n", e2);
puts("链栈元素:");
PrintfLinkStack(&stack);
printf("链栈元素个数:%d", GetLengthLinkStack(&stack));
printf("\n");
return 0;
}