#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct NODE
{
int data;
struct Node *pnext;
}node,*pnode;
typedef struct STACK
{
Pnode ptop;
Pnode Pbottom;
}stack,*pstack;
void init (Pstack pS);
void push (Pstack ps,int val);
void Traverse (Pstack PS);
BOOL Pop (pstack ps,int * pVal);
bool Empty (Pstack PS);
void Clear (Pstack PS);
int main ()
{
STACK S;
int Val;
Init (&s);//Create an empty stack
for (int i=0;i<=15;i++)
{
Push (&s,i);
}
/*
Push (&s,1);//Press Stack
Push (&s,3);
Push (&s,4);
Push (&s,5);
Push (&s,13);
Push (&s,11);
*/
Traverse (&s);//Traversal output
Out of the stack
Pop (&s,&val);
if (pop (&s,&val))
{
printf ("The stack succeeds, the stack of elements is%d\n", Val);
}
Else
{
printf ("stack success");
}
Traverse (&s);//Traversal output
Traverse (&s);//Traversal output
Clear (&s);//Empty
if (empty (&s))
{
printf ("Stack is empty \ n");
}
Else
{
printf ("stack is not empty \ n");
}
return 0;
}
void init (Pstack PS)
{
ps->ptop= (pnode) malloc (sizeof (NODE));
if (null==ps->ptop)
{
printf ("Dynamic memory allocation failed");
Exit (-1);
}
Else
{
Ps->pbottom = ps->ptop;
Ps->ptop->pnext = NULL;
}
}
void push (Pstack ps,int val)
{
Pnode pnew= (pnode) malloc (sizeof (NODE));
pnew->data=val;
pnew->pnext=ps->ptop;
ps->ptop=pnew;
Return
}
void Traverse (Pstack PS)
{
Pnode p=ps->ptop;
while (P!=ps->pbottom)
{
printf ("%d", p->data);
p=p->pnext;
}
printf ("\ n");
}
bool Empty (Pstack PS)
{
if (Ps->ptop==ps->pbottom)
{
return true;
}
Else
{
return false;
}
}
BOOL Pop (pstack Ps,int * pVal)
{
if (empty (PS))//ps storage is the address of the stack
{
return true;
}
Else
{
Pnode r = ps->ptop;
* PVal = r->data;
Ps->ptop = r->pnext;
Free (R);
R = NULL;
return true;
}
}
void Clear (Pstack PS)
{
if (empty (PS))
{
Return
}
Else
{
Pnode p=ps->ptop;
Pnode Q=null;
while (P!=ps->pbottom)
{
q=p->pnext;
Free (p);
p=q;
}
ps->ptop=ps->pbottom;
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Stack operations (create, stack, stack, empty, traverse, etc.)