An array implementation of the stack of linear tables

Source: Internet
Author: User

#include <stdio.h>

#include <malloc.h>

typedef struct NODE

{

int *pbase;

int len;//array Maximum hold length

int top;//Array Effective length

}stack,*stack;

void Initstack (Stack,int);

void push (Stack,int);

void Pop (Stack,int *);

BOOL Stackempty (Stack);

BOOL Stackfull (Stack);

void Main ()

{

Stack stack;

Initstack (&stack,6);

printf ("Start stack \ n");

Press Stack

Push (&stack,2);

Push (&stack,3);

Push (&stack,4);

Push (&stack,5);

Out of the stack

printf ("Start out stack \ n");

int Val;

Pop (&stack,&val);

Pop (&stack,&val);

Pop (&stack,&val);

Pop (&stack,&val);

}

void Initstack (Stack stack,int len)

{

stack->pbase= (int *) malloc (sizeof (int) *len);//Note This place initializes the memory to be allocated.

stack->len=len;

stack->top=-1;//Note that the local coordinates should be indicated as-1

}

void push (Stack stack,int val)

{

if (Stackfull (stack))

{

printf ("Stack is full, can't stack");

Return

}

stack->top++;

stack->pbase[stack->top]=val;

}

BOOL Stackfull (Stack stack)

{

if (stack->len-1==stack->top)

return true;

Else

return false;

}

void Pop (Stack stack,int *val)

{

if (Stackempty (stack))

{

printf ("The current stack is empty, unable to stack");

Return

}

*val=stack->pbase[stack->top];

printf ("%d", *val);

stack->top--;


}

BOOL Stackempty (Stack stack)

{

if (stack->top==-1)

return true;

Else

return false;

}


This article from the "Jane Answers Life" blog, reproduced please contact the author!

An array implementation of the stack of linear tables

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.