1+ /**
2+ * This class implements a Stack using a regular array.
3+ *
4+ * A stack is exactly what it sounds like. An element gets added to the top of
5+ * the stack and only the element on the top may be removed. This is an example
6+ * of an array implementation of a Stack. So an element can only be added/removed
7+ * from the end of the array. In theory stack have no fixed size, but with an
8+ * array implementation it does.
9+ *
10+ * @author Unknown
11+ *
12+ */
13+ public class StackArray {
14+
15+ /**
16+ * Main method
17+ *
18+ * @param args Command line arguments
19+ */
20+ public static void main (String [] args ) {
21+ StackArray myStackArray = new StackArray (4 ); //Declare a stack of maximum size 4
22+ //Populate the stack
23+ myStackArray .push (5 );
24+ myStackArray .push (8 );
25+ myStackArray .push (2 );
26+ myStackArray .push (9 );
27+
28+ System .out .println ("*********************Stack Array Implementation*********************" );
29+ System .out .println (myStackArray .isEmpty ()); //will print false
30+ System .out .println (myStackArray .isFull ()); //will print true
31+ System .out .println (myStackArray .peek ()); //will print 9
32+ System .out .println (myStackArray .pop ()); //will print 9
33+ System .out .println (myStackArray .peek ()); // will print 2
34+ }
35+
36+ /** The max size of the Stack */
37+ private int maxSize ;
38+
39+ /** The array representation of the Stack */
40+ private int [] stackArray ;
41+
42+ /** The top of the stack */
43+ private int top ;
44+
45+ /**
46+ * Constructor
47+ *
48+ * @param size Size of the Stack
49+ */
50+ public StackArray (int size ){
51+ maxSize = size ;
52+ stackArray = new int [maxSize ];
53+ top = -1 ;
54+ }
55+
56+ /**
57+ * Adds an element to the top of the stack
58+ *
59+ * @param value The element added
60+ */
61+ public void push (int value ){
62+ if (!isFull ()){ //Checks for a full stack
63+ top ++;
64+ stackArray [top ] = value ;
65+ }else {
66+ resize (maxSize *2 );
67+ push (value );// don't forget push after resizing
68+ }
69+ }
70+
71+ /**
72+ * Removes the top element of the stack and returns the value you've removed
73+ *
74+ * @return value popped off the Stack
75+ */
76+ public int pop (){
77+ if (!isEmpty ()){ //Checks for an empty stack
78+ return stackArray [top --];
79+ }
80+
81+ if (top < maxSize /4 ){
82+ resize (maxSize /2 );
83+ return pop ();// don't forget pop after resizing
84+ }
85+ else {
86+ System .out .println ("The stack is already empty" );
87+ return -1 ;
88+ }
89+ }
90+
91+ /**
92+ * Returns the element at the top of the stack
93+ *
94+ * @return element at the top of the stack
95+ */
96+ public int peek (){
97+ if (!isEmpty ()){ //Checks for an empty stack
98+ return stackArray [top ];
99+ }else {
100+ System .out .println ("The stack is empty, cant peek" );
101+ return -1 ;
102+ }
103+ }
104+
105+ private void resize (int newSize ){
106+ //private int[] transferArray = new int[newSize]; we can't put modifires here !
107+ int [] transferArray = new int [newSize ];
108+
109+ //for(int i = 0; i < stackArray.length(); i++){ the length isn't a method .
110+ for (int i = 0 ; i < stackArray .length ; i ++){
111+ transferArray [i ] = stackArray [i ];
112+ stackArray = transferArray ;
113+ }
114+ maxSize = newSize ;
115+ }
116+
117+ /**
118+ * Returns true if the stack is empty
119+ *
120+ * @return true if the stack is empty
121+ */
122+ public boolean isEmpty (){
123+ return (top == -1 );
124+ }
125+
126+ /**
127+ * Returns true if the stack is full
128+ *
129+ * @return true if the stack is full
130+ */
131+ public boolean isFull (){
132+ return (top +1 == maxSize );
133+ }
134+
135+ /**
136+ * Deletes everything in the Stack
137+ *
138+ * Doesn't delete elements in the array
139+ * but if you call push method after calling
140+ * makeEmpty it will overwrite previous
141+ * values
142+ */
143+ public void makeEmpty (){ //Doesn't delete elements in the array but if you call
144+ top = -1 ; //push method after calling makeEmpty it will overwrite previous values
145+ }
146+ }
0 commit comments