1. Chain-type stack
Stackli.h
typedef int ElementType;
#ifndef stackli_h_included
#define Stackli_h_included
struct Node;
typedef struct NODE *ptrtonode;
typedef ptrtonode Stack;
int IsEmpty (Stack S);
Stack Createstack ();
void Disposestack (Stack S);
void Makeempty (Stack S);
void Push (ElementType X, Stack S);
ElementType Top (Stack S);
void Pop (Stack S);
#endif//stackli_h_included
Fatal.h
#ifndef fatal_h_included
#define Fatal_h_included
#include <stdio.h>
#include <stdlib.h>
#define ERROR (str) fatalerror (str)
#define FATALERROR (str) fprintf (stderr, "%s\n", Str), exit (1)
#endif//fatal_h_included
Stackli.c
#include "stackli.h"
#include "fatal.h"
struct Node
{
ElementType Element;
Ptrtonode Next;
};
int IsEmpty (Stack S)
{
return s->next = = NULL;
}
Stack Createstack ()
{
Stack S;
S = malloc (sizeof (struct Node));
if (S = = NULL)
FatalError ("Out of the space!!!");
S->next = NULL;
return S;
}
void Makeempty (Stack S)
{
if (S = = NULL)
Error ("must use Createstack");
Else
while (! IsEmpty (S))
Pop (S);
}
void Disposestack (Stack S)
{
Makeempty (S);
Free (S);
}
void Push (ElementType X, Stack S)
{
Ptrtonode Tmpcell;
Tmpcell = malloc (sizeof (struct Node));
if (Tmpcell = NULL)
FatalError ("Out of the space!!!");
Else
{
Tmpcell->element = X;
Tmpcell->next = s->next;
S->next = Tmpcell;
}
}
ElementType Top (Stack S)
{
if (! IsEmpty (S))
Return s->next->element;
Error ("Empty stack");
return 0; /* return value used to avoid warning * *
}
void Pop (Stack S)
{
Ptrtonode FirstCell;
if (IsEmpty (S))
Error ("Empty stack");
Else
{
FirstCell = s->next;
S->next = s->next->next;
Free (FirstCell);
}
}