1
+ import java .util .*;
2
+ abstract class DynamicArrayStack
3
+ {
4
+ private static class DArrayStack
5
+ {
6
+ private int [] stack ; //array for stack
7
+ private int top ; //top for stack
8
+ private int s ; //s for calculating the size of the stack
9
+
10
+ /**
11
+ * DArrayStack()
12
+ * this is default constructor
13
+ **/
14
+ public DArrayStack ()
15
+ {
16
+ this (1 ); //call parameterised contructor as size 1
17
+ }
18
+
19
+ /**
20
+ * DArrayStack()
21
+ * this is parametrized constructor
22
+ *
23
+ * @param num for size of the input
24
+ **/
25
+ public DArrayStack (int num )
26
+ {
27
+ this .s = num ;
28
+ top =-1 ;
29
+ this .stack = new int [num ];
30
+ }
31
+
32
+ /**
33
+ * push()
34
+ * this function will push an element to the stack
35
+ *
36
+ * @param num for element to push
37
+ *
38
+ * @return void
39
+ **/
40
+ public void push (int num )
41
+ {
42
+ if (top ==s -1 )
43
+ {
44
+ expand (); //expand array when stack gets full
45
+ }
46
+
47
+ top ++;
48
+ stack [top ] = num ; //push to stack
49
+ }
50
+
51
+ /**
52
+ * expand()
53
+ * this function will expand the array to its double
54
+ *
55
+ * @param void
56
+ *
57
+ * @return void
58
+ **/
59
+ public void expand ()
60
+ {
61
+ int oldsize = s ;
62
+ s = s *2 ; //increase the array size to double
63
+ int [] arr = new int [s ];
64
+ for (int i =0 ;i <oldsize ;i ++) //copying current stack to arr
65
+ {
66
+ arr [i ] = stack [i ];
67
+ }
68
+ stack = arr ;
69
+ }
70
+
71
+ /**
72
+ * pop()
73
+ * this function will pop an element from the stack
74
+ *
75
+ * @param none
76
+ *
77
+ * @return int for element to be popped
78
+ **/
79
+ public int pop () throws EmptyStackException
80
+ {
81
+ if (top ==-1 ) //if stack is empty
82
+ {
83
+ throw new EmptyStackException ();
84
+ }
85
+ int temp = stack [top ];
86
+ stack [top ]=0 ;
87
+ top --;
88
+ if (top +1 ==s /2 )
89
+ {
90
+ shrink (); //shrink the array size
91
+ }
92
+ return temp ;
93
+ }
94
+
95
+ /**
96
+ * shrink()
97
+ * it will shrink the stack array size to its half
98
+ *
99
+ * @param void
100
+ *
101
+ * @return void
102
+ **/
103
+ public void shrink ()
104
+ {
105
+ s =s /2 ;
106
+ int [] arr = new int [s ];
107
+ for (int i =0 ;i <s ;i ++)
108
+ {
109
+ arr [i ] = stack [i ];
110
+ }
111
+ stack = arr ;
112
+ }
113
+
114
+ /**
115
+ * peek()
116
+ * it will return the top element of the stack
117
+ *
118
+ * @param void
119
+ *
120
+ * @return int for top element of the stack
121
+ **/
122
+ public int peek ()
123
+ {
124
+ return stack [top ];
125
+ }
126
+
127
+ /**
128
+ * print()
129
+ * it will print the whole array for checking only never do that
130
+ *
131
+ * @param void
132
+ *
133
+ * @return void
134
+ **/
135
+ public void print ()
136
+ {
137
+ for (int i =0 ;i <s ;i ++)
138
+ {
139
+ System .out .print (stack [i ]+" " );
140
+ }
141
+ System .out .println ();
142
+ }
143
+
144
+ /**
145
+ * size()
146
+ * it will return the size of the stack
147
+ *
148
+ * @param void
149
+ *
150
+ * @return int for size of the stack
151
+ **/
152
+ public int size ()
153
+ {
154
+ return top +1 ;
155
+ }
156
+ }
157
+
158
+ public static void main (String [] args )
159
+ {
160
+ try
161
+ {
162
+ DArrayStack S = new DArrayStack ();
163
+ S .push (1 );
164
+ S .push (2 );
165
+ S .push (3 );
166
+ S .push (4 );
167
+ S .push (5 );
168
+ S .push (6 );
169
+ S .push (10 );
170
+ S .pop ();
171
+ S .push (11 );
172
+ S .push (12 );
173
+ S .print ();
174
+ System .out .println (S .peek ());
175
+ System .out .println (S .size ());
176
+ }
177
+ catch (Exception e )
178
+ {
179
+ System .out .println (e );
180
+ }
181
+ }
182
+ }
0 commit comments