@@ -38,20 +38,81 @@ public void makeEmpty(){ //Doesn't delete elements in the array but if you call
38
38
top = -1 ; //push method after calling makeEmpty it will overwrite previous values
39
39
}
40
40
}
41
+
42
+
43
+ /* This is ArrayList Implementation of stack , Where size is not
44
+ a problem we can extend the stack as much as we want*/
45
+ class Stack2 {
46
+
47
+
48
+ ArrayList <Integer > stackList ;
49
+
50
+ Stack2 (){ //constructor
51
+ stackList =new ArrayList <>();
52
+ }
53
+
54
+
55
+ void push (int value ){ //adds value to the end of list which is the top for stack
56
+ stackList .add (value );
57
+ }
58
+
59
+ int pop (){ //pops last element of list which is indeed the top for Stack
60
+
61
+ if (!isEmpty ()){ // checks for an empty Stack
62
+
63
+ int popValue =stackList .get (stackList .size ()-1 );
64
+ stackList .remove (stackList .size ()-1 ); //removes the poped element from the list
65
+ return popValue ;
66
+ }
67
+ else {
68
+ System .out .print ("The stack is already empty " );
69
+ return -1 ;
70
+ }
71
+
72
+ }
73
+
74
+ boolean isEmpty (){ //checks for empty Stack
75
+ if (stackList .isEmpty ())
76
+ return true ;
77
+
78
+ else return false ;
79
+
80
+ }
81
+
82
+ int peek (){ //top element of stack
83
+ return stackList .get (stackList .size ()-1 );
84
+ }
85
+ }
86
+
41
87
//Example
42
88
public class Stacks {
43
89
public static void main (String args []){
44
- Stack myStack = new Stack (4 ); //Declare a stack of maximum size 4
45
- //Populate the stack
46
- myStack .push (5 );
47
- myStack .push (8 );
48
- myStack .push (2 );
49
- myStack .push (9 );
50
-
51
- System .out .println (myStack .isEmpty ()); //will print false
52
- System .out .println (myStack .isFull ()); //will print true
53
- System .out .println (myStack .peek ()); //will print 9
54
- System .out .println (myStack .pop ()); //will print 9
55
- System .out .println (myStack .peek ()); // will print 2
56
- }
57
- }
90
+ Stack myStack = new Stack (4 ); //Declare a stack of maximum size 4
91
+ //Populate the stack
92
+ myStack .push (5 );
93
+ myStack .push (8 );
94
+ myStack .push (2 );
95
+ myStack .push (9 );
96
+
97
+ System .out .println ("*********************Stack Array Implementation*********************" );
98
+ System .out .println (myStack .isEmpty ()); //will print false
99
+ System .out .println (myStack .isFull ()); //will print true
100
+ System .out .println (myStack .peek ()); //will print 9
101
+ System .out .println (myStack .pop ()); //will print 9
102
+ System .out .println (myStack .peek ()); // will print 2
103
+
104
+ Stack2 myStack2 = new Stack2 (); //Declare a stack of maximum size 4
105
+ //Populate the stack
106
+ myStack2 .push (5 );
107
+ myStack2 .push (8 );
108
+ myStack2 .push (2 );
109
+ myStack2 .push (9 );
110
+
111
+ System .out .println ("*********************Stack List Implementation*********************" );
112
+ System .out .println (myStack2 .isEmpty ()); //will print false
113
+ System .out .println (myStack2 .peek ()); //will print 9
114
+ System .out .println (myStack2 .pop ()); //will print 9
115
+ System .out .println (myStack2 .peek ()); // will print 2
116
+ System .out .println (myStack2 .pop ()); //will print 2
117
+ }
118
+ }
0 commit comments