Data structure and Algorithm learning summary 04 LINEAR table---Stack

Source: Internet
Author: User

Stack

Stacks are special linear tables, which are linear tables that are only allowed to be inserted and deleted at one end .

The top of the stack that is allowed to be inserted and deleted is the bottom of the stack.

The insert of the stack is called the stack, and the deletion is called the stack.

The attribute is: LIFO, so the stack is also called the LIFO table, or the LIFO table (last in first out).

Because the stack is a linear table, There are also sequential tables and linked lists in two forms , generally we use sequential tables.

As you can see from the code, there is actually a change in the INSERT and delete operations compared to the sequential table.

#include <iostream>using namespacestd; Const intStack_size =5; classSqstack {Private:    int*Elem; inttop; intstackSize; Public:    voidInitsqstack ();//initialization of the sequential stack    intGetTop ();//get top of stack element    intLength ();//get the capacity of the current stack    voidDestroysqstack ();//Destroy sequence Stack    voidClearsqstack ();//emptying the sequential stack    voidPush (intV);//Press Stack/stack/ into stack    intPop ();//out of the stack    voidInputsqstack (intn);//INPUT Element    voidOutputsqstack ();//Traverse Output}; voidSqstack::initsqstack () {Elem=New int[Stack_size]; Top=-1; StackSize=Stack_size;} intSqstack::gettop () {if(top==-1) cout<<"Empty Stack"<<Endl; intE =Elem[top]; returne;}intsqstack::length () {if(top==-1) cout<<"Empty Stack"<<Endl; returntop+1;} voidsqstack::D estroysqstack () {Delete[] elem; Top=-1; StackSize=0; }  voidSqstack::clearsqstack () {Top=-1;}voidSqstack::P Ush (intv) {if(top==stacksize-1)     {        int*Newstack; Newstack=New int[StackSize +1];  for(intI=0; i<=top;i++) {Newstack[i]=Elem[i]; }        Delete[] elem; Elem=Newstack; StackSize++; } elem[++top]=v; }  intsqstack::P op () {intv; if(top==-1) cout<<"Empty Stack"<<Endl; V=elem[top--]; returnv; }  voidSqstack::inputsqstack (intN) {    intA; cout<<"Enter numbers:"<<Endl;  for(inti =0; I < n; i++) {cin>>A;    Push (a); } }voidSqstack::outputsqstack () {if((Elem = = NULL) | | (top==-1)) cout<<"error occurred"<<Endl; Else     {          for(inti =0; I <= top; i++) {cout<<elem[i]<<" "; } cout<<Endl; } }voidMain () {Sqstack S;      S.initsqstack (); S.inputsqstack (6);    S.outputsqstack (); cout<<s.gettop () <<Endl;     S.pop ();  S.outputsqstack ();} 

Data structure and Algorithm learning summary 04 LINEAR table---Stack

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.