C language for implementing the sequence Stack

Source: Internet
Author: User

There is nothing to talk about. In the structure of the sequential stack, the header pointer, the tail pointer, And the stack capacity. When building a stack, you need to find a suitable memory space and point it to the space with the tail pointer in the struct. The header pointer also points to this space. Stack pressure is: first determine whether the space is sufficient? If it is not enough, continue to allocate space. If it is enough, assign a value to the node pointed to by the header pointer and move the first node to the back (that is, the data in the space that the header pointer always points to does not have any value ). Stack play is: first move the header pointer back to a unit, and then return the value at the top of the stack with E.

#include <stdio.h> #include <stdlib.h>#define STACK_INIT_SIZE 100#define STACKINCREAMENT 10#define OVERFLOW    -1#define ERROR  0#define OK 1typedef int Status;typedef int ElemType;typedef struct {        ElemType *base;        ElemType *top;        int  stacksize;} st;Status InitStack(st &ss){       ss.base = (ElemType*)malloc(STACK_INIT_SIZE * sizeof(ElemType));       if (!ss.base) exit(OVERFLOW);       ss.top = ss.base;       ss.stacksize = STACK_INIT_SIZE;       return OK;}Status GetTop(st &ss, ElemType &e){       if (ss.base == ss.top)       return ERROR;       e = *(ss.top - 1);       return OK;}Status Push(st &ss, ElemType e){       if (ss.top - ss.base >= ss.stacksize)       {          ss.base = (ElemType*)realloc(ss.base, ss.stacksize + STACKINCREAMENT * sizeof(ElemType));          if (!ss.base) exit(OVERFLOW);          ss.top = ss.base + ss.stacksize;          ss.stacksize = ss.stacksize + STACKINCREAMENT * sizeof(int);       }       *ss.top = e;       ss.top++;       return OK;}Status Pop(st &ss, ElemType &e){       if (ss.top == ss.base) return ERROR;       e = *(--ss.top);       return OK;}Status DestroyStack(st &ss){       free(ss.base);       ss.base = NULL;       ss.top = NULL;       ss.stacksize = 0;       return OK;} Status CleatStack(st &ss){       ss.top = ss.base;       ss.stacksize = STACK_INIT_SIZE;       return OK;}int StackEmpty(st &ss){       if (ss.top == ss.base)       return 1;       return 0;}int StackLen(st &ss){    return (ss.top - ss.base);}void print(st &ss){     int m = StackLen(ss);     int i;     for (i = 0; i < m; i++)     {         ElemType e;         Pop(ss,e);         printf("%d ", e);     }     printf("\n");     return ;}int main(){    st ss;     InitStack(ss);     int m, i;    if(StackEmpty(ss)) printf("empty\n");    printf("how many nums do you want to printf\n");    scanf("%d", &m);    for (i = 0; i < m; i++)    {        int e;        scanf("%d", &e);        Push(ss, e);    }    printf("%d\n", StackLen(ss));    print(ss);    system ("pause");    return 0;    } 

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.