Data structure-Stack related operation algorithm

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>

#define STACK_INIT_SIZE 100
#define Stackincrement 10
#define OVERFLOW-2
#define OK 1
#define ERROR 0

typedef int SELEMTYPE;

Stack structure body
typedef struct {
Selemtype *base;
Selemtype *top;
int stacksize;
}sqstack;

function definition
int Initstack (Sqstack *s);
int GetTop (sqstack *s,selemtype *e);
int Push (Sqstack *s,selemtype e);
int Destorystack (Sqstack *s);
int Stackempty (Sqstack *s);
int Stacklength (Sqstack *s);
int Pop (sqstack *s,selemtype *e);
int Stacktraverse (Sqstack *s);
int main ()
{
Sqstack *sq;
int f1,f2,e,f3,i,n,a,f4,f5,len,f6,f7;


//Initialize stack
F1 = initstack (sq);
if (F1) printf ("Initialization stack succeeded!") \ n ");
else printf ("Initialization stack failed!") \ n ");

//into the stack
printf ("Please enter the number of elements in the stack:");
scanf ("%d", &n);
printf ("Please enter elements in stack:");
for (i=0; i<n; i++) {
scanf ("%d", &a);
Push (Sq,a);
}

//Get top of stack element
F2 = GetTop (sq,&e);
if (F2) printf ("Stack top element:%d\n", e);
else printf ("Stack is empty!") No stack top element! \ n ");

//Determine if the stack is empty
F5 = stackempty (sq);
if (F5) printf ("Stack is empty!") \ n ");
else printf ("Stack is not empty!") \ n ");

//Remove elements from the stack
F6 = Pop (sq,&e);
if (f6) {
printf ("Deleted elements:%d\n", e);
}

//Get the length of the stack
Len = stacklength (sq);
printf ("Stack length:%d\n", Len);

//top-down traversal stack
Stacktraverse (SQ);

//Destroy Stack
F4 = destorystack (sq);
if (F4) printf ("Destroy the Stack successfully!") \ n ");
else printf ("Destroy Stack failed!") \ n ");


return 0;
}

//Initialize stack
int Initstack (Sqstack *s) {
S->base = (Selemtype *) malloc (stack_init_size*sizeof (Selemtype));
if (! S->base) {
Exit (OVERFLOW);
}
S->top = s->base;
S->stacksize = stack_init_size;

return OK;
}

//Get top of stack element
int GetTop (Sqstack *s,selemtype *e) {
if (s->top = = s->base) {
return ERROR;
}
*e = * (s->top-1);
return OK;
}

//into the stack
int Push (Sqstack *s,selemtype e) {
if ((s->top-s->base) >=s->stacksize) {
S->base = (selemtype*) realloc (S->base, (s->stacksize+stackincrement) *sizeof (Selemtype));
if (! s->base) exit (OVERFLOW);
S->top = S->base + s->stacksize;
S->stacksize + = stackincrement;
}
*s->top++ = e;
return OK;
}

//Remove elements from the stack
int Pop (Sqstack *s,selemtype *e) {
if (s->top = = s->base) return ERROR;
*e = *--s->top;
return OK;
}

//Destroy Stack
int Destorystack (Sqstack *s) {
S->top = s->base;
Free (s->base);
S->top = NULL;
S->base = NULL;
return OK;
}

//Determine if the stack is empty
int Stackempty (Sqstack *s) {
if (s->top = = s->base) {
return OK;
}
return ERROR;
}

//Get the length of the stack
int Stacklength (Sqstack *s) {
Return s->top-s->base;
/*int i=0;
if (! Stackempty (S)) {
while (S->top!=s->base) {
i++;
s->top--;
}
}
Return i;*/
}

//top-down traversal stack
int Stacktraverse (Sqstack *s) {
Selemtype *p;
if (s->base!=s->top) {
printf ("Top-down elements in the stack are:");
p = s->top;
while (P!=s->base) {
p--;
printf ("%d", *p);
}
} else {
printf ("Stack is empty!") \ n ");
return ERROR;
}
printf ("\ n");
return OK;
}




Data structure-Stack related operation algorithm

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.