-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_PA.java
More file actions
225 lines (203 loc) · 8.02 KB
/
Stack_PA.java
File metadata and controls
225 lines (203 loc) · 8.02 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import java.util.Stack;
import java.util.*;
public class Stack_PA
{
/************************ to be solved ***********************/
/* @param stack - is a stack of Integer values
*
* precondition - stack contains at least two elements
*
* postcondition - returns the sum of the all the integers within the stack
* without changing the original stack
*/
public static int sumStack(Stack<Integer> stack)
{
Stack<Integer> temp = (Stack<Integer>)(stack.clone());
int count = 0;
int size = temp.size();
for(int i = 0; i < size; i++){
count += temp.pop();
}
return count;
}
/* @param stack - is a stack of Integer values
* @param odd - is an empty stack
* @param even - is an empty stack
*
* precondition - no preconditions
*
* postcondition - Removes each element from stack and places it in either
* the odd stack or the even stack based on the element
* stack will be empty at the end of the call
*/
public static void splitStack(Stack<Integer> stack, Stack<Integer> odd, Stack<Integer> even)
{
Stack<Integer> temp = (Stack<Integer>)(stack.clone());
int size = temp.size();
for(int i = 0; i < size; i++){
int val = temp.pop();
if(val % 2 == 0)
even.push(val);
else
odd.push(val);
}
}
/* @param stack - is a stack of Integer values
*
* precondition - no preconditions
*
* postcondition - Removes all duplicates in a stack and leaves the
* stack in the same relative order
* Example:
* original stack ==> [4, 6, 7, 4, 2, 7]
* final stack ==> [4, 6, 7, 2]
*/
public static int removeDuplicates(Stack<Integer> stack)
{
Stack<Integer> temp = new Stack<Integer>();
ArrayList<Integer> tempInt = new ArrayList<Integer>();
int count = 0;
while(!stack.isEmpty())
tempInt.add(stack.pop());
for(int i = 0; i < tempInt.size(); i++){
for(int j = i + 1; j < tempInt.size();j++){
if(tempInt.get(i).equals(tempInt.get(j))){
tempInt.remove(j);
j--;
count++;
}
}
}
System.out.print(tempInt);
for(int k = tempInt.size() - 1; k > 0; k--){
stack.push(tempInt.get(k));
}
return count;
}
/* @param stack - is a stack of Integer values
* @param element - value of the element to be removed
*
* precondition - no precodition
*
* postcondition - Removes the first occurrence of a specific element from a stack, leaving
* the other elements in the stack in the same relative order
*/
public static void removeFirstOccurrence(Stack<Integer> stack, int element)
{
ArrayList<Integer> tempInt = new ArrayList<Integer>();
while(!stack.isEmpty())
tempInt.add(stack.pop());
if(tempInt.lastIndexOf(element) != -1)
tempInt.remove(tempInt.lastIndexOf(element));
for(int i = tempInt.size() - 1; i >= 0; i--)
stack.push(tempInt.get(i));
}
/* @param stack - is a stack of String values
* @param element - value of the element to be removed
*
* precondition - no precodition
*
* postcondition - Removes all occurrances of a specific element from a stack, leaving
* the other elements in the stack in the same relative order
*/
public static void removeAllOccurrences(Stack<String> stack, String element)
{
ArrayList<String> tempInt = new ArrayList<String>();
while(!stack.isEmpty())
tempInt.add(stack.pop());
if(tempInt.indexOf(element) != -1)
while(tempInt.indexOf(element) != -1)
tempInt.remove(tempInt.indexOf(element));
for(int i = tempInt.size() - 1; i >= 0; i--)
stack.push(tempInt.get(i));
}
/****************** Testing **********************************/
public static void main(String[] args)
{
System.out.println("********** Sum Stack Output *********");
// Creating a Stack
Stack<Integer> numbers = new Stack<Integer>();
// Pushing new items to the Stack
numbers.push(15);
numbers.push(3);
numbers.push(9);
numbers.push(-2);
numbers.push(10);
//display stack
System.out.println("Stack => " + numbers);
System.out.println();
//sum up stack values and display
int sum = sumStack(numbers);
System.out.println("Sum of the numbers in the Stack => " + sum);
System.out.println("Stack => " + numbers);
System.out.println();
System.out.println("********** Even and Odd Stack Output *********");
// Creating a Stack
Stack<Integer> list = new Stack<Integer>();
Stack<Integer> odd = new Stack<Integer>();
Stack<Integer> even = new Stack<Integer>();
// Pushing random elements on the Stack
for(int i = 0; i < 100; i ++)
list.push((int)(Math.random() * 100) + 1);
//display stack
System.out.println("Stack => " + list);
System.out.println();
//splitStack into odd andd even
splitStack(list, odd, even);
System.out.println("Stack => " + list);
System.out.println("Odd => " + odd);
System.out.println("Even => " + even);
System.out.println();
System.out.println("********** Remove Duplicate Stack Output *********");
//removes duplicates from odd and even
System.out.println("Number duplicates in Odd => " + removeDuplicates(odd));
System.out.println("Odd => " + odd);
System.out.println("Number duplicates in even => " + removeDuplicates(even));
System.out.println("Even => " + even);
System.out.println();
System.out.println("********** Remove First Occurrence Stack Output *********");
// Creating a Stack
list = new Stack<Integer>();
// Pushing new items to the Stack
list.push(5);
list.push(3);
list.push(9);
list.push(2);
list.push(3);
list.push(8);
list.push(4);
//display stack
System.out.println("Stack => " + list);
System.out.println();
//remove specific elements from stack and display
removeFirstOccurrence(list, 9);
System.out.println("Element 9 removed - Stack => " + list);
removeFirstOccurrence(list, 1);
System.out.println("Element 1 removed - Stack => " + list);
removeFirstOccurrence(list, 3);
System.out.println("Element 3 removed - Stack => " + list);
System.out.println();
System.out.println("********** Remove All Occurrence Stack Output *********");
// Creating a Stack
Stack<String> list2 = new Stack<String>();
// Pushing new items to the Stack
list2.push("C");
list2.push("F");
list2.push("Q");
list2.push("C");
list2.push("B");
list2.push("A");
list2.push("C");
//display stack
System.out.println("Stack => " + list2);
System.out.println();
//remove specific elements from stack and display
removeAllOccurrences(list2, "A");
System.out.println("Element \"A\" removed - Stack => " + list2);
removeAllOccurrences(list2, "Q");
System.out.println("Element \"Q\" removed - Stack => " + list2);
removeAllOccurrences(list2, "C");
System.out.println("Element \"C\" removed - Stack => " + list2);
System.out.println();
}
}