C language implementation of sequential Stack

Source: Internet
Author: User

1. Ordered stack structure

Typedef struct {SElemType data [MAXSIZE]; int top;/* for top stack pointer */} SqStack;

2. Construct an empty stack S

Status InitStack(SqStack *S){         /* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */        S->top=-1;        return OK;}

3. Set S to empty Stack

Status ClearStack(SqStack *S){         S->top=-1;        return OK;}

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

Status StackEmpty(SqStack S){         if (S.top==-1)                return TRUE;        else                return FALSE;}

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

int StackLength(SqStack S){         return S.top+1;}

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

Status GetTop(SqStack S,SElemType *e){        if (S.top==-1)                return ERROR;        else                *e=S.data[S.top];        return OK;}

7. insert element e as the new top element of the stack.

Status Push (SqStack * S, SElemType e) {if (S-> top = MAXSIZE-1)/* Full stack */{return ERROR ;} s-> top ++;/* Add a */S-> data [S-> top] = e to the top pointer of the stack; /* assign the new inserted element to the top space of the stack */return OK ;}

8. 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 (SqStack * S, SElemType * e) {if (S-> top =-1) return ERROR; * e = S-> data [S-> top]; /* the top element of the stack to be deleted is assigned to e */S-> top --;/* the top pointer of the stack minus one */return OK ;}

9. display each element in the stack from the bottom of the stack to the top of the stack.

Status StackTraverse(SqStack S){        int i;        i=0;        while(i<=S.top)        {                visit(S.data[i++]);        }        printf("\n");        return OK;}

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.