C-language implementation of two-stack shared space

Source: Internet
Author: User

#include "stdio.h"
#include "Stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20/* Storage space Initial allocation */

typedef int STATUS;

typedef int SELEMTYPE; /* The Selemtype type is based on the actual situation and is assumed to be int */


/* Two-stack shared space structure */
typedef struct
{
Selemtype Data[maxsize];
int top1;/* stack 1 stack top pointer */
int top2;/* stack 2 stack top pointer */
}sqdoublestack;


Status visit (selemtype c)
{
printf ("%d", c);
return OK;
}

/* Construct an empty stack s */
Status Initstack (Sqdoublestack *s)
{
s->top1=-1;
s->top2=maxsize;
return OK;
}

/* Place S as empty stack */
Status Clearstack (Sqdoublestack *s)
{
s->top1=-1;
s->top2=maxsize;
return OK;
}

/* Returns True if Stack S is an empty stack, otherwise false */
Status stackempty (Sqdoublestack S)
{
if (s.top1==-1 && s.top2==maxsize)
return TRUE;
Else
return FALSE;
}

/* Returns the number of elements of S, that is, the length of the stack */
int Stacklength (Sqdoublestack S)
{
Return (s.top1+1) + (MAXSIZE-S.TOP2);
}

/* Insert element e As new stack top element */
Status Push (sqdoublestack *s,selemtype e,int stacknumber)
{
if (S->TOP1+1==S->TOP2)/* Stack is full, no more push new elements */
return ERROR;
if (stacknumber==1)/* Stack 1 has elements in the stack */
s->data[++s->top1]=e; /* If stack 1 is top1+1 first, assign values to the array elements. */
else if (stacknumber==2)/* Stack 2 has elements in the stack */
s->data[--s->top2]=e; /* If stack 2 is top2-1 first, assign values to the array elements. */
return OK;
}

/* If the stack is not empty, delete the top element of S, return its value with E, and return OK; return error */
Status Pop (sqdoublestack *s,selemtype *e,int stacknumber)
{
if (stacknumber==1)
{
if (s->top1==-1)
return ERROR; /* Description Stack 1 is already empty stack, overflow */
*e=s->data[s->top1--]; /* Stack top element of stack 1 out of stack */
}
else if (stacknumber==2)
{
if (s->top2==maxsize)
return ERROR; /* Description Stack 2 is already empty stack, overflow */
*e=s->data[s->top2++]; /* Stack top element of stack 2 out of stack */
}
return OK;
}

Status Stacktraverse (Sqdoublestack S)
{
int i;
i=0;
while (I<=S.TOP1)
{
Visit (s.data[i++]);
}
I=S.TOP2;
while (i<maxsize)
{
Visit (s.data[i++]);
}
printf ("\ n");
return OK;
}

int main ()
{
Int J;
Sqdoublestack s;
int e;
if (Initstack (&s) ==ok)
{
for (j=1;j<=5;j++)
Push (&s,j,1);
for (j=maxsize;j>=maxsize-2;j--)
Push (&s,j,2);
}

printf ("The elements in the stack are sequentially:");
Stacktraverse (s);

printf ("Elements in the current stack:%d \ n", Stacklength (s));

Pop (&s,&e,2);
printf ("Pop-up stack top element e=%d\n", e);
printf ("Stack null No:%d (1: Empty 0: NO) \ n", Stackempty (s));

for (j=6;j<=maxsize-2;j++)
Push (&s,j,1);

printf ("The elements in the stack are sequentially:");
Stacktraverse (s);

printf ("Stack full No:%d (1: no 0: Full) \ n", Push (&s,100,1));


Clearstack (&s);
printf ("Empty stack, stack empty No:%d (1: Empty 0: NO) \ n", Stackempty (s));

return 0;
}

C-language implementation of two-stack shared space

Related Article

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.