Data structures and algorithms-stacks and queues

Source: Internet
Author: User

First, Introduction

It is well known that linear tables are the basis of data structures and are usually implemented in two ways: arrays and linked lists. Stacks and queues are the most common data structures that are implemented based on linear tables.

Second, Stack

Definition: A stack is a linear table that restricts insert and delete operations only at the end of the table, that is, Filo.

Stacks are often likened to magazines, i.e. bullets that are first pressed into the magazine are then fired. According to the implementation of the linear table, there are two ways to implement the stack: array implementation and chain list implementation.

Array implementations of Stacks:

 PackageBasic.data_structure.cha01;/*** Stack: Advanced post-Exit (FILO), only allow the basic operation of the stack at the top of the stack: * Initialize stack, stack, stack, get stack top element, determine whether it is empty * * * with array implementation stack. * */ Public classArraystack<t> {    Privatet[] data;//Data Container    Private intMaxSize;//Stack Capacity    Private inttop =-1;//stack Top pointer             PublicArraystack () { This(10); }    //Initialize Stack     PublicArraystack (intcapacity) {        if(Capacity > 0) {Data= (t[])NewObject[capacity]; MaxSize=capacity; Top=-1; }Else{            Throw NewRuntimeException ("Stack initialization size cannot be less than or equal to 0"); }    }         Public BooleanIsEmpty () {returntop = =-1; }        //Press Stack     Public Booleanpush (T elem) {if(top = = MaxSize-1){            Throw NewRuntimeException ("Full-stack"); } data[++top] =Elem; return true; }        //gets the top element of the stack (not removed)     PublicT Peek () {if( This. IsEmpty ()) {            Throw NewRuntimeException ("Empty Stack"); }        returnData[top]; }            //Pop Stacks     PublicT Pop () {if( This. IsEmpty ()) {            Throw NewRuntimeException ("Empty Stack"); }        returndata[--top]; }}

It is easy to understand that all are actually basic operations of linear tables.

The chain list implementation of the stack:

 PackageBasic.data_structure.cha01;/*** Stack: Advanced post-FILO, only allows the basic operation of the stack on top of the stack: * Initialize stack, stack, stack, get stack top element, current stack size * * Stack list implementation **/ Public classLinkedstack<t> {    Private intCurrentSize;//Current stack size (no size limit for linked list stack)    PrivateNode<t> top;//head Node//Initialize Stack     PublicLinkedstack () {Top=NULL;//the head pointer is empty    }         Public BooleanIsEmpty () {returntop = =NULL; }         Public intLength () {returncurrentsize; }        //Press Stack     Public Booleanpush (T elem) {Node<T> node =NewNode<t>(Elem, top); Top=node; CurrentSize++; return true; }        //get top of stack element     PublicT Peek () {if( This. IsEmpty ()) {            Throw NewRuntimeException ("Empty Stack"); }                returnTop.data; }        //Pop Stacks     PublicT Pop () {if( This. IsEmpty ()) {            Throw NewRuntimeException ("Empty Stack"); } Node<T> node =top; Top=Top.next; Node.next=NULL; CurrentSize--; returnNode.data; }        //node (inner class)    Private Static classNode<t>{T data; //data fieldsNode<t> Next;//chain Domain         PublicNode () {} PublicNode (T data, node<t>next) {             This. data =data;  This. Next =Next; }    }    }

Data structures and algorithms-stacks and queues

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.