Reputation: 21
I know the main idea of pop is to get and delete the top element. But how about if I calculate the sum of the stack by method pop(), and I still need those element in the code later? I know I can use peek() to get the top element and I don't know how to get other element.
Here is the sum
method I want to adapt:
public int sum(){
int sum = 0;
int n = super.size();
int i;
for ( i=0 ; i<n ; i++){
int s = (Integer)super.pop();
sum = sum + s;
}
return sum;
}
The class type extends another type named LinkedStack
:
import java.util.*;
public class LinkedStack {
protected ChainNode top;
protected int size;
public boolean empty() {
return size == 0;
}
public Object peek() {
if (empty())
throw new EmptyStackException();
return top.element;
}
public void push(Object theElement) {
// put theElement at the top of the stack
top = new ChainNode(theElement, top);
size++;
}
public Object pop() {
if (empty())
throw new EmptyStackException();
Object topElement = top.element;
top = top.next;
size--;
return topElement;
}
}
Upvotes: 0
Views: 948
Reputation: 18939
You don't need to pop elements from stack to find sum. You only need to iterate in order to calculate that sum. Stream Api is very useful here also
Check the following example
Stack<Integer> stack = new Stack<>();
stack.push(2);
stack.push(3);
stack.push(4);
Integer sum = stack.stream().mapToInt(Integer::valueOf).sum();
System.out.println(sum);
Edit
After more explanation from comments here is how a method which would take a stack as parameter would return the sum.
public static Integer calculateSum(Stack<Integer> stack){
int sum = 0;
for(Integer element: stack){
sum += element;
}
return sum;
}
Upvotes: 2
Reputation: 1786
In order to use pop()
as it is you could invoke it over a clone
version of your data structure. On original DS
you'll have always unmodified data and with clone-DS
you could invoke what ever methods wanted, which would modify(or not) data. Simple iteration over initial DS is better over cloning for this case, but for different scenarios maybe clone will fit better.
Upvotes: 0
Reputation: 16354
If you are extending the java.util.Stack
collection type, you can use the default Iterator
or the iterate over the Stack
directly using a for each
syntax as it extends the Iterable
super-interface:
public class CustomStack<T> extends Stack<T> {
public void printElements() {
for (T element : this) {
System.out.println(element);
}
}
}
Using this Stack
extension:
CustomStack<Integer> stack = new CustomStack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.printElements(); // prints: 1, 2 and 3
System.out.println(stack.size()); // prints 3 as the Stack elements are un-changed
Here down the updated type CustomStack
to show a #sum
method:
public class CustomStack<T extends Integer> extends Stack<T> {
public int sum() {
int sum = 0;
for (T element : this) {
sum = sum + (int) element;
}
return sum;
}
}
CustomStack<Integer> stack = new CustomStack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.sum()); // will print 6
System.out.println(stack.size()); // prints 3 as the elements are still un-changed
Upvotes: 1