Stack operations (create, stack, stack, empty, traverse, etc.)

Source: Internet
Author: User

#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.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.