LinkStack.h
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
typedef int Elemtype;
typedef struct StackNode
{
Elemtype data;
struct StackNode* next;
}StackNode, *LinkStack;
void InitStack(LinkStack *pst);
void Push(LinkStack *s, const Elemtype x);
StackNode* BuyNode(const Elemtype x);
void Pop(LinkStack *pst);
void Show(const LinkStack pst);
LinkStack.cpp
#include "LinkStack.h"
void InitStack(LinkStack *pst)
{
assert(pst != NULL);
*pst = NULL;
}
StackNode* BuyNode(const Elemtype x)
{
StackNode* newnode = (StackNode*)malloc(sizeof(StackNode));
assert(newnode != NULL);
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void Push(LinkStack *pst, const Elemtype x)
{
assert(pst != NULL);
StackNode* newnode = BuyNode(x);
newnode->next = *pst;
*pst = newnode;
}
void Show(const LinkStack pst)
{
assert(pst != NULL);
LinkStack p = pst;
while (p != NULL)
{
printf("%d-->", p->data);
p = p->next;
}
printf("NULL\n");
}
void Pop(LinkStack *pst)
{
assert(pst != NULL);
assert(*pst != NULL);
StackNode* p = (*pst)->next;
free(*pst);
*pst = p;
}