-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mack_stack_with_arrayList.java
193 lines (155 loc) · 3.85 KB
/
Mack_stack_with_arrayList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import java.util.ArrayList;
import java.util.Scanner;
/**
* Mack_stack_with_arrayList
*/
public class Mack_stack_with_arrayList {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// System.out.println("Give the capacity of your stack");
Stack st = new Stack();
System.out.println("Enter the function number to perform \n 01\t push \n 02\t pop\n 03\t peep\n 04\t peek\n 05\t update_by_index\n 06\t update_by_data\n 07\t Sizeof\n 08\t isEmpty\n 09\t print\n 00\t terminate");
while(true)
{
int n = sc.nextInt();
if (n==1)
{
st.push(sc.nextInt());
System.out.println("next");
}
else if(n==2)
{
st.pop();
System.out.println("next");
}
else if(n==3)
{
st.peep(sc.nextInt());
System.out.println("next");
}
else if(n==4)
{
st.peek();
System.out.println("next");
}
else if(n==5)
{
st.update_through_index(sc.nextInt(),sc.nextInt());
System.out.println("next");
}
else if(n==6)
{
st.update_through_numbers(sc.nextInt(), sc.nextInt());
System.out.println("next");
}
else if(n==7)
{
System.out.println(st.isEmpty());
System.out.println("next");
}
else if(n==8)
{
System.out.println(st.isEmpty());
System.out.println("next");
}
else if(n==9)
{
st.display();
System.out.println("next");
}
else if(n==0)
{
break;
}
else
{
System.out.println("Give appropriate number");
System.out.println("next or press 0 to terminate");
}
}
}
}
class Stack <T>
{
private ArrayList <T> st;
public Stack()
{
st = new ArrayList<>();
}
public void push(T item)
{
st.add(item);
}
public T pop()
{
if(!st.isEmpty())
{
return st.remove(st.size()-1);
}
else
{
return null;
}
}
public T peek()
{
if(!st.isEmpty())
{
return st.get(st.size()-1);
}
else
{
return null;
}
}
public int size()
{
return st.size();
}
public boolean isEmpty()
{
return st.isEmpty();
}
public T peep(int index)
{
if(index<st.size())
{
return st.get(index);
}
else
{
return null;
}
}
public T update_through_numbers(T oldvalue , T newvalue)
{
if (!st.isEmpty())
{
int i = st.indexOf(oldvalue);
return st.set(i,newvalue);
}
else
{
return null;
}
}
public T update_through_index(int i , T newvalue)
{
if(!st.isEmpty())
{
return st.set(i, newvalue);
}
else
{
return null;
}
}
public void display()
{
for(int i=(st.size()-1);i>=0;i--)
{
System.out.print(st.get(i));
}
System.out.println();
}
}