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