Java implementation Stack Structure

Source: Internet
Author: User
Tags int size

The definition of the stack

Stack is a linear table that restricts inserting and deleting operations at one end of a table.

(1) It is commonly said that the insertion and deletion of this end is the top of the stack, the other end is called the Stack Bottom (Bottom).

(2) when there is no element in the table, it is called an empty stack.

(3) The stack is a linear table of LIFO (last in first), abbreviated as a LIFO table.

The stack changes are based on the LIFO principle. Always delete (back stack) in the current stack

The newest element, the last element to insert (into the stack), and the first to be placed at the bottom of the stack,

You will not be able to delete until the last.

The "example" element is a1,a2,...,an in the order of the stack, the order of the retreat is an,an-1, ...,

A1.

2, the basic operation of the stack

(1) initstack (S)

Constructs an empty stack s.

(2) Stackempty (S)

The stack is empty. If S is an empty stack, returns true, otherwise it returns false.

(3) Stackfull (S)

The stack is full. Returns True if S is full, otherwise returns false.

Note: This operation applies only to the sequential storage structure of the stack.

(4) Push (s,x)

into the stack. If the stack s is dissatisfied, the element x is inserted into the top of the S stack.

(5) Pop (S)

Define Stack ADT
Stackadt
Package Stack;
Public interface Stackadt {
public void push (Object element);//Press Stack
Public Object pop ();//OUT Stack
public Boolean isempty ();
public int size ();
public object Peek ();//return a reference to the top of the stack
Public String toString ();
}

Chained implementations:


Add and remove elements in the stack, maintain a node at the top of the stack and a count variable to indicate the size of the stack:
Private Linearnode top; Point to top of stack
private int count;//tag stack size
Every time the stack and stack in the list of the table header: (also can be the end of the table, the implementation of the same way)
Top---> element 1---> element 2---> Element 3 ...
Implementation (incidental test main):
Linkedstack
Package Stack;
Import Bag.linearnode;
In order to focus on the implementation of the algorithm, the exception is printed directly out and then exit the program, no longer declare the exception class
public class Linkedstack implements Stackadt {
Private Linearnode top; Point to top of stack
private int count;//tag stack size
public static void Main (string[] args) {
Linkedstack stack = new Linkedstack ();
System.out.println ("Press stack from 0 to 10");
for (int i = 0;i < 10;i++)
Stack.push (i);
System.out.println ("Perform 5 consecutive stack operations");
for (int i = 0;i < 5;i++)
Stack.pop ();
System.out.println ("stack is empty.) : "+ stack.isempty ());
System.out.println ("Stack size is:" + stack.size ());
System.out.println ("Stack top element is:" + stack.top.getElement ());
System.out.println ("Stack top element is:" + stack.peek ());
}
Public Linkedstack ()
{
top = null;
Count = 0;
}
public int size () {
return count;
}
public Boolean IsEmpty () {
return (size () = 0);
}
public void push (Object element) {
Linearnode node = new Linearnode (element);
Node.setnext (top);
top = node;
count++;
}
Public Object pop () {
if (IsEmpty ())
{
System.out.println ("Stack is empty!");
System.exit (1);
}
Object result = Top.getelement ();
top = Top.getnext ();
count--;
return result;
}
Public Object Peek () {
Object result = Top.getelement ();
return result;
}
}
Run Result:
Press the stack 0 to 10 in turn
Perform 5 consecutive stack operations
Stack is empty. : false
The size of the stack is: 5
Top element of Stack: 4
Top element of Stack: 4


Array Implementation:


The bottom of the stack is always the position where the array is labeled 0, and the stack stack starts with the last element of the array subscript:
Private object[] contents;
private int Top;//top marks the next placement in the stack, but also indicates the size of the stack, compared to the chained implementation count ...
Implementation (incidental test main):
Arraystack
Package Stack;
public class Arraystack implements Stackadt {
Private object[] contents;
private int Top;//top marks the next placement in the stack, but also indicates the size of the stack, compared to the chained implementation count ...
private static int SIZE = 10;
Public Arraystack ()
{
Contents = new Object[size];
top = 0;
}
public void expand () {///by applying for an auxiliary space, one times the capacity per expansion
object[] larger = new Object[size () *2];
for (int index = 0;index < top;index++)
Larger[index] = Contents[index];
contents = larger;
}
public int size () {
return top;
}
public Boolean IsEmpty () {
return (size () = 0);
}
public void push (Object element) {
if (IsEmpty ())
expand ();
if (top = contents.length)
expand ();
Contents[top] = element;
top++;
}
Public Object pop () {
if (IsEmpty ())
{
System.out.println ("Stack is empty!");
System.exit (1);
}
Object result = Contents[top-1];
Contents[top-1] = null;//out stack
top--;
return result;
/* The book is easy to write::
* top--;
* Object result = Contents[top];
* Contents[top] = null;*/
}
Public Object Peek () {
Object result;
if (IsEmpty ())
result = NULL;
Else
result = Contents[top-1];
return result;
}
public static void Main (string[] args) {
Arraystack stack = new Arraystack ();
System.out.println ("will 0 to 24 sequentially press the stack, and then 10 times out of the stack");
for (int i = 0;i < 25;i++)
Stack.push (i);
for (int i = 0;i < 10;i++)
Stack.pop ();
System.out.println ("Stack size is:" + stack.size ());
System.out.println ("stack is empty.) : "+ stack.isempty ());
System.out.println ("Stack top element is:" + stack.peek ());
}
}
Run Result:
Press the stack 0 to 24 sequentially, then 10 times in a row
The size of the stack is: 15
Stack is empty. : false
Top element of Stack: 14

Related Article

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.