Xi. the implementation of a simple stack stack

Source: Internet
Author: User
Tags int size stack pop
first, stack of the stack and into the stack rules

--first in, after or after the first out of two, simple stack code display

/**
 * @use Custom Stack 
 * @author lattice * * *
/class Mystack {
	private object[] elements;
	private int size=0;
	Set the stack size
	private static final int mystack_length =;
	Public Mystack () {
		elements=new object[mystack_length];
	}
	into the stack public
	void push (Object obj) {
		//open new stack space for storing new data
		ensurecapacity ();
		elements[size++]=obj;
		
	}
	Out Stack public
	Object pop () {
		if (size==0)
			throw new Emptystackexception ();
		Object result=elements[size-1];
		elements[--size]=null;//empty expired references. Eleminate Absolete reference return result
		;
	}
	/**
	 * Handle full stack situation *
	 *
	private void ensurecapacity () {
		if (elements.length = size)
			elements = Arrays.copyof (elements, size+1);
	}

third, the stack of Code analysis stack rules 1, out of the stack pop () and into the stack push ()

POPs () each time the top of the stack, which is the largest element of the subscript, is removed and the corresponding element of the subscript is destroyed (set to a null object)

Push () always puts the element above the top of the stack, which is the position of the subscript of the maximum subscript +1.

So the stack and into the stack rules to determine the stack can only be first into the order, like 10 cars running towards a dead-end cul-de-sac, the car in front is either finished before the car in the back and comes out, or just wait for the car in the back to come out. 2, ensurecapacity () to ensure that every time the listing of the contents of the stack has a position to put this new element.


Four, custom stack test and results

public static void Main (string [] args) {
		string str= ' This is lattice testing ';
		System.out.println (str);
		Mystack stack=new mystack ();
		for (int i=0;i<100;i++) {
			stack.push (i);
		}
		
		for (int i=0;i<100;i++) {
			if (i%10==0) {
				System.out.println ("");
			}
			System.out.print (Stack.pop () + "");
		}
	
Run results Run Results The result is
lattice testing----------- 
The " 
49 48 47 46 45 44 43 4" of the "a"-------- 2 (a) of 
19 18 17 16 15 14 13 12 11 10 in A-G-M 

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.