Data structure (c language) stack--sequential implementation

Source: Internet
Author: User

Tag: Kong storage space des out EFI space structure language data structures

Stack: Stack is a special kind of linear table. The peculiarity is that the manipulation of inserting and deleting data elements can only be done at one end of the linear table.

/**** 2017.11.3** ahthor:799** Stack is implemented *****/#include <bits/stdc++.h>using namespace std; #define STACK_INIT_SIZE 1 00///Storage space Initial allocation # Stack_increment 10///Storage Allocation Increment # define Elemtype int#define Status int#define OK 1#define error-1#d    Efine overflow-2typedef struct{elemtype *base;    Elemtype *top; int stacksize;} Sqstack;    Status Initstack (Sqstack &s) {s.base = (Elemtype *) malloc (stack_init_size * sizeof (elemtype)); if (!    s.base) exit (OVERFLOW);    S.top = S.base;    S.stacksize = stack_init_size; return OK;}    Status Destroystack (Sqstack &s) {for (Elemtype *index = s.base; Index! = s.top; index + +) {free (index);    } free (&s); return OK;}    Status Clearstack (Sqstack &s) {for (Elemtype *index = s.base+1; Index! = s.top; index + +) {free (index);    } s.top = S.base; return OK;}    Status Isemptystack (Sqstack &s) {if (s.base = = S.top) return true; else return false;} Status Lengthstack (Sqstack &s) {RetuRN S.top-s.base;}    Elemtype GetTop (Sqstack &s) {if (s.top = = s.base) return ERROR;    Elemtype e = * (s.top-1); return e;} Status Push (sqstack &s,elemtype &e) {if (s.top-s.base >= s.stacksize) {s.base = (Elemtype *) re        Alloc (S.base, (s.stacksize + stack_increment) * sizeof (elemtype)); if (!        s.base) exit (OVERFLOW);        S.top = S.base + s.stacksize;    S.stacksize + = stack_increment;    } *s.top = e;    S.top + +; return OK;}    Status Pop (Sqstack &s, Elemtype &e) {if (s.top = = s.base) return ERROR;    e = *--S.TOP; return OK;} Status Trversestack (Sqstack &s) {for (Elemtype *index = s.base; Index! = s.top; index + +) {cout<<*    index<< "";    } cout<<endl; return OK;}    int main () {Sqstack * St = (Sqstack *) malloc (sizeof (sqstack));    Initstack (*ST);    int n;    cin>>n;        while (n--) {int tmp;        cin>>tmp;    Push (*ST,TMP);    } int num; Pop (*st,nUM);    cout<< "num" <<num<<endl;    Trversestack (*ST);    cout<< "Top" <<gettop (*st) <<endl;    cout<< "Len:" <<lengthstack (*st) <<endl;    Clearstack (*ST);    if (Isemptystack (*st)) cout<< "Kong" <<endl;    else cout<< "No" <<endl; return 0;}

  

Data structure (c language) stack--sequential implementation

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.