"Java" Big Talk Data structure (6) Stack of linear tables

Source: Internet
Author: User
Tags arithmetic

According to the book of "Big Talk data Structure", this paper realizes the Java version of the stack of the sequential storage structure, two stacks of shared space, the stack of chain storage institutions .

Stack : a linear table that restricts insert and delete operations only at the end of the table.

Insert (stack) and delete (stack) operations as shown in the stack.

1. Sequential storage structure of stacks

The data is stored in an array, and the top variable indicates the position of the top element in the array (the top pointer of the stack). A stack of length 5 is as follows:

Implementation program:

/** * Stack's sequential storage structure *  problem: In the constructor, is there a better way to create a generic array? * @author Yongh * */public class Sqstack<e> {private e[] data;private int top;  Stack top pointer, top=-1 when empty stack private int maxsize;private static final int default_size= 10;public sqstack () {this (default_size);} Public sqstack (int maxSize) {//Cannot create a generic array data=new e[maxsize];d ata= (e[]) New object[maxsize];top=-1;this.maxsize= MaxSize;} public void push (E e) {if (top==maxsize-1) throw new RuntimeException ("The stack is full and cannot be pushed into the stack!") "); top++;d ata[top]=e;} Public E Pop () {if (top==-1) throw new RuntimeException ("Empty stack, cannot be out of the stack!") "); e e=data[top];top--;return e;} public void Printstack () {if (top==-1) {System.out.println ("empty Stack");} else {for (int i=0;i<=top;i++) {System.out.println (data[i]);}}}}

  

Test code:

public class Stacktest {public static void main (string[] args) {sqstack<student> sqstack=new sqstack<student > ();    Student[] students= {new Student ("Small A", "one"), New Student ("Small B", "New" ("Small C"), New Student ("small d", +            ), new Student ("Small e", 151)};    for (int i=0;i<5;i++) {    sqstack.push (students[i]);    }    Sqstack.printstack ();    for (int i=0;i<5;i++) {    sqstack.pop ();    }    Sqstack.printstack ();    }       } Class student{public    Student (String name, Int. age) {        this.name=name;        this.age=age;    }    String name;    int age;    Public String toString () {    return name;    }}

  

Small a small B small C small D small e empty stack
stacktest

2. Two stacks of shared space

By storing two stacks in an array, the space can be used well. The stack top pointers for stacks 1 and 2 are represented by the TOP1 and TOP2 variables, and the stack bottoms of two stacks are located at the head and tail of the array, respectively.

Implementation program (on the basis of the Sqstack program can be modified slightly):

/** * stack of sequential storage structure (two stack space) * * Note: The stack full condition is top1+1==top2 * * @author Yongh * */public class Sqdoublestack<e> {private e[]  data;private int top1;  Stack 1 stack top pointer, top=-1 when empty stack private int top2; Stack 2 stack top pointer, top=maxsize-1 when empty stack private int maxsize;private static final int default_size= 10;public sqdoublestack () {This ( default_size);} Public sqdoublestack (int maxSize) {//Cannot create a generic array data=new e[maxsize];d ata= (e[]) New object[maxsize];top1=-1;top2= Maxsize-1;this.maxsize=maxsize;} /* * In-stack operation, Stacknumber represents the stack number to be entered */public void push (int stacknumber,e E) {if (TOP1+1==TOP2) throw new RuntimeException ("Stack is full, cannot Into the Stack! "); if (stacknumber==1) {data[++top1]=e;} else if (stacknumber==2) {data[--top2]=e;} else {throw new RuntimeException ("Stack number Error! ");}} /* * Out of stack operation */public e pop (int stacknumber) {E e;if (stacknumber==1) {if (top1==-1) throw new RuntimeException ("Empty stack 1, cannot be out of the stack! "); e=data[top1--];} else if (stacknumber==2) {if (top2==maxsize-1) throw new RuntimeException ("Empty stack 2, cannot be out of the stack!") "); e=data[top2++];} else {throw new RuntimeException ("Stack number Error! ");} return e;}}

  

3. The chain storage structure of the stack

Stacks that are implemented through a one-way list are placed on the head of a single-linked list (note that the stack operation is not inserted behind the linked list, but is inserted from the head).

The chain stack is as follows.

Insert and DELETE operations:

  

Implementation program:

/** * * Stack of chained storage structure * * @author Yongh */public class Linkstack<e> {private stacknode<e> top;private int count;p Rivate class stacknode<e>{e data; Stacknode<e> next;public Stacknode (E data,stacknode<e> next) {This.data=data;this.next=next;}} Public Linkstack () {top=new stacknode<e> (null, null); count=0;} public void push (e e) {stacknode<e> node=new stacknode<e> (e, top); top=node;count++;} Public E Pop () {if (count==0) throw new RuntimeException ("Empty stack, cannot be out of the stack!") "); Stacknode<e> node;node=top;top=top.next;count--; e e=node.data;node=null;return e;} public void Printstack () {if (count==0) {System.out.println ("empty Stack");} else {stacknode<e> node=top;for (int i=0;i<count;i++) {System.out.println (node.data); node=node.next;}}}    /* * Test code */public static void Main (string[] args) {linkstack<student> linkstack=new linkstack<student> (); Student[] students= {new Student ("Small A", one-to-one), new Student ("Small B", "New" ("Small C"), New Student ("Small d", 14), New Student ("Small e", 151)};    for (int i=0;i<5;i++) {Linkstack.push (students[i]);    } linkstack.printstack ();    SYSTEM.OUT.PRINTLN ("----");    for (int i=0;i<5;i++) {System.out.println (Linkstack.pop ());    } linkstack.printstack (); }}

  

Small e Small D small C small B small a---- small e Small D small C small B small a empty stack
Linkstack

4. Application of Stacks

(1) Implementing recursion

Some problems (such as the solution of Fibonacci numbers) can be obtained by recursive functions, and recursive functions are implemented by stacks.

A typical Fibonacci sequence.

(2) Arithmetic expression evaluation

The solution of arithmetic expression can be realized by using suffix expression (inverse Polish notation) combined with stack. And through the stack, we can convert our usual infix expression to the suffix expression.

"Java" Big Talk Data structure (6) Stack of linear tables

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.