I have a homework about making a Double stack, consisting of redStack and blueStack, and have their own operations (e.g redPush, bluePush). But for the pop, it knows the last added element in both stacks, and remove it. My question is how could I make it know the last added elements?
Here's my code:
public T pop() {
if (redCounter > blueCounter) {
redCounter--;
return redStack.pop();
}
if (blueCounter > redCounter) {
blueCounter--;
return blueStack.pop();
}
}
My code here shows removing the last element depending on the counter for each stack. But how can I know the last added one if they are equal to each other? Thanks.
If you must use two stacks for this then I would maintain a third stack lastAdded that pushes a label for the last pushed element. If the last pushed element was red, then push 0, otherwise 1.
Then in pop you check lastAdded for last type of element pushed, and pop from the corresponding stack.
public T pop() {
if (lastAdded.empty()) {
throw Exception();
}
int lastColor = lastAdded.pop();
if (lastColor == 0) {
return redStack.pop();
}
return blueStack.pop();
}
Besides this I would simply use a single stack for all operations, since per your description of the problem the operations you want seem no different then that of a single stack.
Update: With exactly two stacks you would have to make some modifications to the stack itself. Instead of pushing the value itself, push the value + a counter. When popping, pop the element off the stack that has a greater counter value:
class DoubleStack {
// Java 7 has its own pair class
private class Pair<T, U> {
private T first;
private U second;
public Pair(T x, U y) { first = x; second = y; }
public T getKey() { return first; }
public U getValue() { return second; }
}
private Stack<Pair<Integer, Integer>> redStack = new Stack<>(), blueStack = new Stack<>();
private int c = 0;
public boolean empty() {
return redStack.empty() && blueStack.empty();
}
public void pushRed(int x) {
redStack.push(new Pair<>(x, c++));
}
public void pushBlue(int x) {
blueStack.push(new Pair<>(x, c++));
}
public int pop() {
if (empty()) {
return Integer.MAX_VALUE; // throw an exception
}
if (redStack.empty()) {
return popBlue();
}
if (blueStack.empty()) {
return popRed();
}
if (redStack.peek().getValue() > blueStack.peek().getValue()) {
return popRed();
}
return popBlue();
}
private int popRed() {
return redStack.pop().getKey();
}
private int popBlue() {
return blueStack.pop().getKey();
}
};
Related
I'm trying to create a dynamic array based stack and I am getting an index out of bound error when I try to push elements elements onto a full array. I also made the array generic to accommodate all types of stacks.
import java.util.Arrays;
import java.util.Iterator;
public class Stack<T> {
private int topStack;
private int stackSize;
private T[] stack;
// Constructor
public Stack(T[] arr) {
stack = arr;
stackSize = arr.length;
topStack = -1;
}
// isEmpty
public boolean isEmpty() {
if (topStack == -1)
return true;
return false;
}
// isFull method
public boolean isFull() {
if ((topStack + 1) == stackSize)
return true;
return false;
}
// increment array by 10 spaces <-----------------------------------
public void incrementArray() {
T[] temp = (T[]) new Object[stackSize*2];
System.arraycopy(stack, 0, temp, 0, stack.length);
stack=temp;
stackSize=stackSize*2;
}
// decrement array
public void decrementArray() {
stackSize=stackSize/2;
T[] temp = (T[]) new Object[stackSize];
System.arraycopy(stack, 0, temp, 0, stackSize);
stack=temp;
}
// push method which adds element to top of stack
public void push(T element) {
if (isFull())
incrementArray();
topStack=topStack+1;
stack[topStack] = element;
}
// peek method which shows top of stack without popping it
public T peek() {
return stack[topStack];
}
// pop which copies top of stack, deletes top and returns copy
public T pop() throws EmptyStackException {
if (isEmpty()) {
throw new EmptyStackException();
}
int temp = topStack+1;
if(temp<stackSize/2)
decrementArray();
return stack[topStack--];
}
public static void main(String[] args) {
Stack operands = new Stack<>(new Integer[0]);
operands.push(2);
operands.push(1);
}
}
I'm trying to increase the stack size instead of having it overflow out of bounds.
You should give your stack an initial size > 0. As it stands, your initial stackSize is 0. And guess what stackSize*2 is equal to? And another observation is that you created a generic stack but did not specify a type when creating it in main
Also, note that you can change
public boolean isEmpty() {
if (topStack == -1)
return true;
return false;
}
to
public boolean isEmpty() {
return topStack == -1;
}
You can make similar changes in other methods that return a boolean.
When your code does not behave the way it should, think about what is going on and why that might happen. Placing ubiquitous print statements thru out your code is a good first step to check on key values to see if they are what you expect them to be. A more sophisticated method is to use a debugging tool to step thru your program as it executes to look at the values of various fields.
Create a class called inverse_Stack where our 'stack' is organized in such a way where the first/"bottom" element is located at index (-1). Each element pushed is placed into the array slot before the [current] top-most element. if(size=0)store at index: (length -1); if(size=1), store at index:(length -2);if(size=2), store at index: (length-3);
This is all i have so far. Lost with the push pop peek methods for an inverse stack. I know how to make them work for a regular stack
public class Inverse_Stack<T> implements StackADT<T>{
private T[] stack;
private int top;
//private int bot;
public Inverse_Stack(){
this(100);
}
public Inverse_Stack(int capacity){
top = 0;
stack = (T[] new Object[capacity];
}
public int size(){
//returns size of array
return stack.length;
}
public void push(T element){
//fill in code
}
private void expandCapacity(){
T[] newStack = (T[] new Object[stack.length*2];
for(int i = 0; i < stack.length;i++)
newStack[i] = stack[i];
stack = newStack;
}
public T pop(){
if(isEmpty())
throw new RuntimeException("Empty Stack");
//fill in code
}
public T peek(){
if(isEmpty())
throw new RuntimeException("Empty Stack");
//fill in code
}
length tells you the capacity: the number of items the stack can hold. You also need to keep a count variable so you know how many items are currently in the stack.
I won't write the Java code, but I can give you the general idea:
In your constructor, set count to 0.
isEmpty returns true if count is greater than 0.
push
if the stack is full, expand the capacity
add element at stack[count]
increment count
pop
if the stack is empty, throw empty stack exception
decrement count
return the value at stack[count]
peek is like pop, but you don't actually decrement count.
Is not ArrayDeque doing exactly what you want?
public class InverseStack<T> extends ArrayDeque<T> {
…
}
and toArray( T[] a ) would get Your inverse array
this is my answer. I think this is more help you.
class StackX {
private int maxSize; //size of stack array
private char[] stackData;
private int top; //top of stack
//-------------------------------------------------------------------------
public StackX(int s) {
this.stackData = new char[s];
this.maxSize = s;
this.top = -1;
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public void push(char item) {
if (isFull()) {
System.out.println("Stack is full");
return;
}
stackData[++top] = item;
}
public char pop() throws Exception {
if (isEmpty()) {
throw new Exception("Stack is empty");
}
return stackData[top--];
}
public char peek() throws Exception {
if (isEmpty()) {
throw new Exception("Stack is empty");
}
char peekValue = this.pop();
this.push(peekValue);
return peekValue;
}
public void display() {
if (isEmpty()) {
System.out.println("Stack is empry");
}
System.out.println("Start printing stack data");
for (int i = top; i >= 0; i--) {
System.out.println(stackData[i]);
}
System.out.println("End printing stack data");
}
}
Write a method to find the position of a given element in a stack counting from the top of the stack. More precisely,
the method should return 0 if the element occurs on the top, 1 if there is another element on top of it, and so on. If
the element occurs several times, the topmost position should be returned. If the element doesn’t occur at all, -1
must be returned.
You are asked to write this method in two different ways; one way is to implement it internally inside the
ArrayStack class and the other way is to implement it externally in a separate class. Important: At the end
the stack should be returned to the original state (i.e. no elements should be removed and the order of the elements
should not change).
This is the externall class
public class Stack{
public static int searchstack(ArrayStack z, int n) {
ArrayStack temp = new ArrayStack(z.size());
int c = 0;
boolean flag = false;
while (!z.isEmpty()) {
if (z.top() == n) {
flag = true;
return c;
}
if (z.top() != n) {
temp.push(z.pop());
c++;
flag = false;
}
}
if (flag == false) {
c = -1;
}
while (!temp.isEmpty() && !z.isFull()) {
z.push(temp.pop());
}
return c;
}
public static void main(String[] args) {
ArrayStack z = new ArrayStack(4);
z.push(3); // first element
z.push(7);// 2nd
z.push(8);// 3rd
z.push(1);// 4th
z.printStack();
int n = 3;
System.out.println("Searching externally for" + " " + n + " " + searchstack(z, n));
System.out.println("Searching internally for" +" "+n+" "+ z.searchfor(n)+" "); //THE ERROR IS HERE
}
}
And this is the ArrayClass
public class ArrayStack {
private int[] theStack;
private int maxSize;
private int top;
public ArrayStack(int s) {
maxSize = s;
theStack = new int[maxSize];
top = -1;
}
public void push(int elem) {
top++;
theStack[top] = elem;
}
public int pop() {
int result = theStack[top];
top--;
return result;
}
public int top() {
return theStack[top];
}
public boolean isFull() {
return (top == (maxSize - 1));
}
public boolean isEmpty() {
return (top == -1);
}
public int size() {
return (top + 1);
}
//HERE IS THE METHOD I IMPLEMENTED INTERNALLY AND CALL IT AT THE STACK CLASS
public int searchfor(int n) {
for (int i = top; i >= 0; i--) {
if (theStack[top] == n) {
return i;
}
}
return -1;
}
public void printStack() {
if (top == -1)
System.out.println("Stack is empty!!\n");
else {
System.out.println(theStack[top] + " <- top");
for (int i = top - 1; i >= 0; i--)
System.out.println(theStack[i]);
System.out.println();
}
}
}
The error appearing at the Stack class is at the last line of calling the searchfor method implemented in the Arraystack class , error says that there is no method implemented in Arraystack with the name searchfor (); thiugh I did implement it .whatseems to be the problem ?
You have a bug in your searchStack() implementation. You are losing elements if you find the one you are looking for and it isn't the topmost one.
How to fix your searchStack() method:
keep popping z until you have an empty ArrayStack. While doing so, add the value to a queue.
create valIndex and assign it -1.
Then go through the queue and remove the items from it and adding them to z. While doing so, check for the last occurence of the desired value and save it in valIndex.
if valIndex equals -1 return it. Else, use following equation to convert it to correct index and return:
valIndex = (z.size - 1) - valIndex
I'm having trouble with programming a function in Java.
First i have implemented a Stack through a Single Linked List, like this:
public class ListStack<E> implements Stack<E> {
private static class Node<T> {
private T item;
private Node<T> next;
private Node(T item, Node<T> next) {
this.item = item;
this.next = next;
}
}
private Node<E> first;
private int size;
public ListStack() {
this.size = 0;
this.first = null;
}
#Override
public E peek() {
return first.item;
}
#Override
public void pop() {
first = first.next;
size--;
}
#Override
public void push(E e) {
Node<E> node = new Node<E>(e, first);
first = node;
size++;
}
#Override
public boolean isEmpty() {
return (first == null);
}
#Override
public int size() {
return size;
}
#Override
public Stack<E> reverse(){
ListStack<E> reversed = new ListStack<E>();
Node<E> node = first;
while(node != null){
reversed.push(node.item);
node = node.next;
}
return reversed;
}
}
Then i have created a stackof a type X. Here's that type's definition and constructor:
private String first, second;
private ListStack<String> text;
public X(String first, String second){
this.first = first;
this.second = second;
this.text = new ListStack<String>();
}
There are getters for both the strings firstand second, getFirst(), and getSecond(), respectively.
Then i want to write a function that basically, for each X of the stack, checks if the String second is equal to the String txt, passed as the function's argument. If it is, it returns X and deletes the Node from the stack, otherwise just returns null.
Here's my implementation of the method:
First, as a private attribute of the class:
private Stack<X> text; //for simplicity, let's assume the stack already contains values of type `X`.
Then:
private X getX(String txt) {
Stack<X> stack = text.reverse();
Stack<X> stack_final = new ListStack<X>();
X c;
String txt2;
boolean found = false;
for (int i = 0; i < stack.size() && !found; i++) {
c = stack.peek(); //extracts the element
txt2 = c.getSecond(); //gets the name
if (txt2.equals(txt)) {
found = true;
stack.pop();
} else
stack_final.push(c);
stack.pop();
}
if (found) {
text = stack_final;
return c;
}
else
return null;
}
What am i doing wrong ?
My guess is that i'm not updating the final stack correctly, with only the values that don't check, but i'm not sure it is that...
The main problem is the for-loop. Just step through it in your head:
i=0 stack=[1,2,3,4,5,6] stack_size=6
i=1 stack=[2,3,4,5,6] stack_size=5
i=2 stack=[3,4,5,6] stack_size=4
i=3 stack=[4,5,6] stack_size=3
the code actually breaks off after reading only half of the stack. You should rather use isEmpty() than a counter.
private X getX(String txt){
Stack<X> stack = text.reverse();
Stack<X> stack_final = new ListStack<X>();
X c = null;
while(!stack.isEmpty()){//transfer all items from stack to stack_final
//retrive and remove the first item from stack
X x = stack.peek();
stack.pop();
if(x.getSecond().equals(txt))//save x as searched item, if it matches
c = x;
//add the item to stack_final
stack_final.push(x);
}
//save stack_final as text (stack_final is a copy of text)
text = stack_final;
//c is either the searched item, or null, if no item was found
return c;
}
And btw., it's common that pop() returns the removed element.
public X pop(){
X res = first.item;
first = first.next;
return res;
}
You will get a NullPointerException if you try to peek() or pop() an empty stack!
public E peek() {
return first.item; // first is null if stack is empty!
}
public void pop() {
first = first.next; // first is null if stack is empty!
size--;
}
The Java Stack class throws an EmptyStackException in those cases.
Then I want to write a function that basically, for each X of the stack, checks if the String second is equal to the String txt, passed as the function's argument. If it is, it returns X and deletes the Node from the stack, otherwise just returns null.
Now, to achieve what you asked, you can simply do:
Stack<X> stack = text.reversed();
X elementFound = null;
while (!stack.isEmpty()) {
if (txt.equals(stack.peek().getSecond()) {
elementFound = stack.peek();
} else {
stackCopy.push(stack.peek());
}
stack.pop();
}
// now stack is empty and stackCopy contains stack reversed and without
// elementFound, if elementFound is not null (meaning it was found)
text = stackCopy; // stack was text reversed
return elementFound;
I'm having difficulty with a linked lists program. I want to write a method that destructively replaces the value in each node n by the sum of the values in the tail of the list. So if the list is 2,3,5,7; I want to change it to 17,15,12,7. I am given a program where I have to add a method that does this. I can change the first number, but I can not change the other three, and I am stuck. If anyone could help me, that would be great.
Original Program
public class IntList {
private int value;
private IntList next;
public IntList(int v, IntList n) { // Constructor
value = v;
next = n;
}
public int getValue() { return value; } // Getters
public IntList getNext() { return next; }
public void setValue(int v) { value = v; } // Setters
public void setNext(IntList n) { next = n; }
// Find the last node of a linked list.
public IntList findLast() {
if (getNext() == null) return this;
else return getNext().findLast();
}
// Add a new node with value v at the end of l;
public void addEnd(int v) {
findLast().setNext(new IntList(v,null));
}
// Add up the values in a list, recurring down the owner
public int sumList() {
if (getNext() == null) return getValue();
else return getValue() + getNext().sumList();
}
// Convert list of int to string
// Recursive method for constructing the end of the string, after the
// initial open bracket.
public String toString1() {
if (getNext() == null)
return getValue() + "]";
else return getValue() + ", " + getNext().toString1();
}
// Top level rountine that starts with the "[" and then calls toString1 to
// do the rest.
public String toString() {
return "[" + toString1();
}
// Recursive method for finding the sum of a list, using recursion down
// an argument. Note that this is a static method, associated with the class
// not with an object owner.
static public int sumListArg(IntList l) {
if (l==null) return 0;
else return l.getValue() + sumListArg(l.getNext());
}
static public void main(String[] args) {
IntList l = new IntList(2,null);
l.addEnd(3);
l.addEnd(5);
l.addEnd(7);
System.out.println("h");
System.out.println(l.toString());
System.out.println("Sum = " + l.sumList());
} // end main
} // end RecursiveIntList
Here is what I have so far for my method (I think it's logically ok, but it's incorrect):
public static void runningSum(IntList l)
{
l.setValue(l.sumList());
while(l.getNext() != null)
{
l.setNext(l.getNext()); //Set Next to be the next reference
l.getValue(); //Get the Next's value
l.setValue(l.sumList()); //Add the rest of the numbers together
}
if(l.getNext() == null)
{
l.setValue(l.getValue());
}
System.out.println(l.toString());
}
There is a nice and elegant solution:
public int sumList() {
if (getNext() == null) {
return getValue();
}
value = getValue() + getNext().sumList();
return value;
}
In this method you recursively iterate through the List, and summarize all the elements that are behind the current element, and set the value at the same time.
This is the code:
public static void runningSum(IntList l)
{
IntList head = l;
int rSum = l.sumList();
while(l != null)
{
int curRS = rSum;
curRS -= l.getValue();
l.setValue(rSum);
rSum = curRS;
l = l.getNext();
}
System.out.println(head.toString());
}
I'll break it in several parts to explain what's going on. We want to code a procedure that takes the head of a list and alters the list in the way you describe; basically the first element must become the sum of all the original elements; the second element must be the sum of all elements except the first, and so on. The last element, the tail, must remain unchanged.
public static void runningSum(IntList l)
{
The function we need to remember the head that was passed to the function; we save l in a variable called head.
IntList head = l;
The running sum for the first element is the sum of all elements; so we call sumList and store the result in a variable called rSum.
int rSum = l.sumList();
This is a very typical idiom in data structures programming; while the element is not null, you loop.
while(l != null)
{
The running sum of the next element is rSum minus the value of the current element.
int nextRS = rSum - l.getValue();
Now we can set running sum of the current element to rSum.
l.setValue(rSum);
For the next iteration, the current running sum is nextRS. Finally we update l to point to the next element.
rSum = nextRS;
l = l.getNext();
}
If we didn't keep track of head, now we wouldn't know what to print.
System.out.println(head.toString());
}
Linked lists lend themselves to recursion. A recursive solution might look like this:
public static void runningSum(IntList l) {
if (l.getNext() != null) {
runningSum(l.getNext());
l.setValue(l.getValue() + l.getNext().getValue());
}
}