Implementation of C language stack

Source: Internet
Author: User

Tag: return push EOF get stdio.h INI get function class

Static.h

#define STATIC_INIT_SIZE 100
#define Staticincrement 10
#define ERROR 0
#define OK 1
typedef struct {

int *base;//Define the bottom of the stack
int *top;//defines the top element of the stack
int staticsize;

}sqstatic;

typedef int STATUS;

Initialize an empty stack
Status initstatic (sqstatic *s);

Destroying stacks
Status destroystatic (sqstatic *s);

Empty stack
Status clearstatic (sqstatic *s);

Determine if the stack is empty
Status staticempty (sqstatic S);

Get the length of the stack
int Staticlength (sqstatic S);

Gets the value of the top element of the stack
Status GetTop (sqstatic S, int *e);

Adding elements to the stack
Status Push (sqstatic *s,int e);

Delete top of stack element and return element value
Status Pop (sqstatic *s, int *e);

Staticrealize.c

#include "Stdlib.h"
#include "stdio.h"
#include "static.h"


Status initstatic (sqstatic *s) {

Set the size of the initial stack
S->base = (sqstatic *) malloc (sizeof (sqstatic) *static_init_size);

if (! S->base) {
Exit (0);
}
S->top = s->base;//empty Stack s->top=s->base
* (s->top) = 0;//is primarily used to determine whether an empty stack
S->staticsize = The initial length of the static_init_size;//stack is 100

return OK;
}
Gets the value of the top element of the stack
Status GetTop (sqstatic S, int *e) {

Sqstatic Q;
Q = S;
Returns the top element of the stack to determine if the stack is an empty stack
if (s.base = = s.top) return ERROR;

*e = * (q.top-1);

return OK;

}

The stack of elements
Status Push (sqstatic *s,int e) {

if ((s->top-s->base) >=s->staticsize) {

S->base = (sqstatic *) realloc (s->base, (staticincrement+static_init_size) *sizeof (sqstatic));
}
* (s->top) = e;

s->top++;
return OK;

}


Status Pop (sqstatic *s,int *e) {

if (s->top = = s->base) return ERROR;

s->top--;
*e = * (S->top);

return OK;

}

Status staticempty (sqstatic S) {
if (* (s.top) = = 0) {

return OK;
}
return ERROR;
}


Returns the number of elements in the stack
int Staticlength (sqstatic S) {

return s.top-s.base;
}

Staticfunction.c

#include "stdio.h"
#include "Stdlib.h"
#include "static.h"
Main function
void Main () {

Initialize an empty stack
Sqstatic S;

int e,f;

Initstatic (&s);
printf ("Judging whether the stack is empty stack%d\n", Staticempty (S));

Push (&s,1);
GetTop (S, &e);
printf ("Stack top element is%d\n", e);

Push (&s,2);
GetTop (S, &e);
printf ("Stack top element is%d\n", e);

Push (&s,3);
GetTop (S, &e);
printf ("Stack top element is%d\n", e);

Push (&s,4);
GetTop (S, &e);
printf ("Stack top element is%d\n", e);

Pop (&s, &f);

printf ("Pop-up value is%d\n", f);

Pop (&s, &f);

printf ("Pop-up value is%d\n", f);

printf ("Judging whether the stack is empty stack%d\n", Staticempty (S));

printf ("Stack length is%d\n", Staticlength (S));


}

Implementation of C language stack

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.