```c
//main.c
#include<stdio.h>
#include"LinkStack.h"
int main()
{
LinkStack S;
S=InitStack();
for (int i = 0; i < 5; i++) {
printf("数据;\n");
int j = 0;
scanf("%d", &j);
Push(S, j);
}
int k=0;
GetTop(S, &k);
printf("栈顶;%d\n", k);
Pop(S, &k);
printf("出栈:%d\n", k);
k = StackLength(S);
printf("元素个数为;%d\n");
PrintStack(S);
return 0;
}
//LinkStack.c
#include<stdio.h>
#include<stdlib.h>
#include"LinkStack.h"
/*
#define OK 1
#define ERROR 2
typedef int Status;
typedef struct StackNode {
int data;
struct StackNode* next;
}StackNode, * LinkStack;
*/
LinkStack InitStack() {
LinkStack S;
S = (StackNode*)malloc(sizeof(StackNode));
S = NULL;
return S;
}
Status Push(LinkStack S, int e) {
LinkStack p;
p = (StackNode*)malloc(sizeof(StackNode));
p->data = e;
p->next = S;
S = p;
return OK;
}
Status Pop(LinkStack S, int* e) {
if (S == NULL) {
printf("空栈\n");
return ERROR;
}
*e = S->data;
LinkStack p = S;
S = S->next;
free(p);
return OK;
}
Status GetTop(LinkStack S, int* e) {
if (S != NULL) {
*e = S->data;
return OK;
}
}
void DestroyStack(LinkStack S) {
LinkStack p;
while (S != NULL) {
p = S->next;
free(S);
S = p;
}
}
void ClearStack(LinkStack S) {
LinkStack p,q;
p = S->next;
S = NULL;
while (p != NULL) {
q = p->next;
free(q);
p = q;
}
}
int StackLength(LinkStack S) {
if (S == NULL) {
return ERROR;
}
LinkStack p = S;
int cnt = 0;
while (S != NULL) {
cnt++;
S = S->next;
}
return cnt;
}
void PrintStack(LinkStack S) {
LinkStack p = S;
while (p != NULL) {
printf("%-3d", p->data);
p = p->next;
}
}
//LinkStack.h
#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_
#define OK 1
#define ERROR 2
typedef int Status;
typedef struct StackNode {
int data;
struct StackNode* next;
}StackNode, * LinkStack;
LinkStack InitStack();
Status Push(LinkStack S, int e);
Status Pop(LinkStack S, int* e);
Status GetTop(LinkStack S, int* e);
void DestroyStack(LinkStack S);
void ClearStack(LinkStack S);
int StackLength(LinkStack S);
void PrintStack(LinkStack S);
#endif
@
```