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