linkstack.c
#include<stdio.h>
#include<stdlib.h>
#include"linkstack.h"
//1.创建一个空的栈
void CreateEpLinkStack(linkstack_t **ptop)
{*ptop = NULL;
}
//2.入栈,ptop是传入的栈针的地址,data是入栈的数据
int pushLinkStack(linkstack_t **ptop, datatype data)
{//创建新节点保存入站数据linkstack_t *penw = (linkstack_t *)malloc(sizeof(linkstack_t));if(penw == NULL){printf("pushLinkStack err\n");return -1;}//申请空间是为了存放data,并初始化penw->data = data;//插入penw->next = *ptop;*ptop = penw;return 0;
}
//3.判断栈是否为空
int isEmptyLinkStack(linkstack_t *top)
{return top==NULL;
}
//4.出栈
datatype popLinkStack(linkstack_t **ptop)
{//linkstack_t *pdel = NULL;if(isEmptyLinkStack(*ptop)){printf("popLinkStack err\n");return -1;}pdel =*ptop;datatype data = pdel->data;(*ptop)=(*ptop)->next;free(pdel);pdel = NULL;}
//5.清空栈
void ClearLinkStack(linkstack_t **ptop)
{while(!isEmptyLinkStack(*ptop))popLinkStack(*ptop);
}
//6.求栈的长度
int LengthLinkStack(linkstack_t *top)
{int len=0;while(top!=NULL){len++;top=top->next;}return len;
}
//7.获取栈顶数据,不是出栈,不需要移动main函数中的top,所以用一级指针
datatype getTopLinkStack(linkstack_t *top)
{if(isEmptyLinkStack(top)){return top->data;}else{return -1;}}
int main(int argc, char const *argv[])
{return 0;
}
linkstack.h
#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__
typedef int datatype;
typedef struct linkstack
{datatype data;struct linkstack *next;
} linkstack_t;
//1.创建一个空的栈
void createEmptyLinkStack(linkstack_t **ptop);
//2.入栈,ptop是传入的栈针的地址,data是入栈的数据
int pushLinkStack(linkstack_t **ptop, datatype data);
//3.判断栈是否为空
int isEmptyLinkStack(linkstack_t *top);
//4.出栈
datatype popLinkStack(linkstack_t **ptop);
//5.清空栈
void clearLinkStack(linkstack_t **ptop);
//6.求栈的长度
int lengthLinkStack(linkstack_t *top);
//7.获取栈顶数据,不是出栈,不需要移动main函数中的top,所以用一级指针
datatype getTopLinkStack(linkstack_t *top);
#endif