An object of sort IntStack is a pile of genuine numbers, with the standard stack operations push(int N), pop(), and isEmpty(). A makeEmpty() operation is additionally given to expel all things from the stack.

Inside, the stack is actualized as a connected rundown.

view sourceprint?

01.

/*******************************************************

02.

* MYCPLUS Sample Code - http://www.mycplus.com ; *

03.

*

04.

* This code is made accessible as a support of our *

05.

* guests and is given entirely to the *

06.

* reason for outline. *

07.

*

08.

* Please guide all request to saqib at mycplus.com *

09.

*******************************************************/

10.

11.

12.

13.

open class NumberStack {

14.

15.

private static class Node {

16.

/An object of sort Node holds one of the

17.

/things on the stack;

18.

twofold thing;

19.

Hub next;

20.

}

21.

22.

private Node top;/Pointer to the Node that is at the highest point of

23.

/of the stack. In the event that top == invalid, then the

24.

/stack is vacant.

25.

26.

open void push( twofold N ) {

27.

/Add N to the highest point of the stack.

28.

Hub newTop = new Node();

29.

newTop.item = N;

30.

newTop.next = top;

31.

top = newTop;

32.

}

33.

34.

open twofold pop() {

35.

/Remove the top thing from the stack, and return it.

36.

/Note that this routine will toss a NullPointerException

37.

/if an attempty is made to pop a thing from a vacant

38.

/stack. (It would be better style to characterize another

39.

/sort of Exception to toss for this situation.)

40.

twofold topItem = top.item;

41.

top = top.next;

42.

return topItem;

43.

}

44.

45.

open boolean isEmpty() {

46.

/Returns genuine if the stack is void. Returns false

47.

/if there are one or more things on the stack.

48.

return top == invalid;

49.

}

50.

51.

}/end class NumberStack
 
Top