Chain implementation:
Add and delete elements in a section of the stack, maintain a node to the top of the stack in the stack and the size of a count variable indicator:
Private linearnode top; // point to the top of the stack
Private int count; // The size of the mark stack is out of the stack and the stack at the linked list: (can also be tail, the implementation method is different)
TOP ---> Element 1 ----> Element 2 ---> Element 3 .........
Implementation (with test main):
Linkedstack
Package Stack; Import Bag.linearNode; // In order to focus on the algorithm, print out the abnormal situation and exit the program, no longer declare the abnormal class linkedstack iMplements Stackadt {Private LinearNode Top; // Point to the top of the top of the stack; // Public static void main (string [] args) {linkedStack Stack = new linkedstack (); system.out.println ("will be crushed in order"); for (int i = 0; i <10; I ++) stack.push (i); System.out.println ("" continuous execution 5 times out of stack operation "); for (int i = 0; i <5; i ++) stack.pop (); system. out.println ("Is the stack empty?:" + stack.isempty ()); System.out.println ("The size of the stack is:" + stack.size ()); The element is: " + stack.top.Getelement ()); System.out.println (" The top element of the stack is: " + stack.peek ()); Public int size () {Return Count;} Public Boolean Isempty () {Return (SIZE () == 0);} Public Void Push (Object Element) {linearNode Node = New Lin Earnode (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.getNext (); Count-; Return Result;} Public object peek () {Object Result = TOP.Getelement (); Return Result;}
Run results:
Does the 0 to 10 stack continue the stack of the stack for 5 consecutive times? : False
The size of the stack is: 5
The top element of the stack is: 4
The top element of the stack is: 4
Array implementation:
The bottom of the stack is always the position of 0 in the array.
Private Object [] Contents; Private Int Top; // Top marks the next position to enter the stack. It also indicates the capacity of the stack, compared with chain implementation! Intersection Intersection
Implementation (with test main):
Arraystack
Package Stack; Public Class Array IMPLEMELEMENTS Stackadt {Private Object [] Contents; Private int; // Top marks the next stack position. NT compare! Intersection Intersection Private Static Int Size = 10; Public ArrayStack () {Contents = New Object [SIZE]; TOP = 0;} Public void expand () {// ject [] larger = new object [size ()*2]; for (int index = 0; index <top; index ++) larger [index] = contents [index]; contents = larger;} public int size () urn 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!"); result = contents [TOP-1]; Contents [TOP-1] = NULL; // Out of the stack Top-; Return Result;/ * Writing in the book is simpler :: * TOP-; * Object result = contents [TOP] ; * Contents [Top] = null; */} Public object people () {object result; if (isempty ()) result = null; else result = contents [TOPE and Res]; ULT;} Public Static Void Main ( String [] args) {arrayStack Stack = New ArrayStack (); System.out.println ("Press 0 to 24, then out of the stack for 10 consecutive times"); for (int i = 0; i <25; i ++ ) stack.push (i); for (int i = 0; i <10; i ++) stack.pop (); System.out.println ("The size of the stack is:"+stack.size ())); System. Out.println ("Is the stack empty? : " + Stack.isempty ()); System.out.println (" The top element of the stack is: " + stack.peek ());}
Run results:
Press 0 to 24, and then the size of the stack 10 times in a row: 15
Is the stack empty? : False
The top element of the stack is: 14
Use set linkedList to simulate the stack
method
Java's generics allow LinkedList to simulate storage stacks of various data types, including int, double, string, object, etc., introduce several API interfaces used:
Enter the stack
void addfirst (e e); // Insert the specified element into the beginning of this list
Get the top element of the stack
E getfirst (); // Back to the first element of this list
Out of the stack
E removefirst (); // Remove and return this list of the first element
Jeopardize
boolean isempty (); // Judge the stack empty
For example code
Import Java.util.linkedList; Import Java.util.nosucheLementedException; Public Class Simulatack {Private LINKEDList <Integer> st <infleger> (); Public Boolean Isempty () {Return This.Stack.isempty ();} Public void push (int data) {This.stack.addfirst (data);} Public int Pop () Throws NosuchelementedException {Return This.Stack.removefirst ();} Public in T Gettop () Throws NosucheLementedException {Return This.Stack.getFirst ( );} Public Static void main (string args []) {simulatingstack s = new simulatestack (); s.push (1); s.Push (2); while (! )) {int data = s.gettop (); system.out.println (data); s.pop ();}}