C language implementation of two-stack shared space

Source: Internet
Author: User

1. Two-stack Shared Space Structure

Typedef struct {SElemType data [MAXSIZE]; int top1;/* stack 1 top pointer */int top2;/* stack 2 top pointer */} SqDoubleStack;


2. Construct an empty stack S

Status InitStack(SqDoubleStack *S){         S->top1=-1;        S->top2=MAXSIZE;        return OK;}


3. Set S to empty Stack

Status ClearStack(SqDoubleStack *S){         S->top1=-1;        S->top2=MAXSIZE;        return OK;}


4. If Stack S is empty, TRUE is returned; otherwise, FALSE is returned.

Status StackEmpty(SqDoubleStack S){         if (S.top1==-1 && S.top2==MAXSIZE)                return TRUE;        else                return FALSE;}


5. Return the number of elements of S, that is, the stack length.

int StackLength(SqDoubleStack S){         return (S.top1+1)+(MAXSIZE-S.top2);}


6. insert element e into the new stack top element.

Status Push (SqDoubleStack * S, SElemType e, int stackNumber) {if (S-> top1 + 1 = S-> top2)/* the stack is full, no more new elements can be pushed */return ERROR; if (stackNumber = 1) /* stack 1 has elements in the stack */S-> data [++ S-> top1] = e; /* if Stack 1 is used, first top1 + 1 and then assign a value to the array element. */Else if (stackNumber = 2)/* stack 2 has elements to stack */S-> data [-- S-> top2] = e; /* if Stack 2, first top2-1 and then assign a value to the array element. */Return OK ;}


7. If the stack is not empty, delete the stack top element of S, use e to return its value, and Return OK; otherwise, an ERROR is returned.

Status Pop (SqDoubleStack * S, SElemType * e, int stackNumber) {if (stackNumber = 1) {if (S-> top1 =-1) return ERROR; /* indicates that stack 1 is already an empty stack and overflows */* e = S-> data [S-> top1 --]; /* stack the top element of stack 1 out of the stack */} else if (stackNumber = 2) {if (S-> top2 = MAXSIZE) return ERROR; /* indicates that stack 2 is already empty, and overflow */* e = S-> data [S-> top2 ++]; /* exit the top element of stack 2 from the stack */} return OK ;}


8. display all elements

Status StackTraverse (SqDoubleStack S) {int I; I = 0; while (I <= S. top1) {visit (S. data [I ++]);} I = S. top2; while (I
 
  


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


Reference < <大话数据结构> >

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.