My Computer Forum Computer Science Forum

Go Back   My Computer Forum > Computer Science Forum > Programming

Programming Programming Languages, Interpreters, and Compilers - Assembly, Declarative, Machine, Object Oriented, Iterative


Reply
 
LinkBack Thread Tools Display Modes
October 2nd, 2013, 10:55 PM   #1
 
Joined: Oct 2013
Posts: 1
Java. Reversed stack returns one item

I have a generic LinkedStack class with a LinkedStack reversed method It creates a new LinkedStack say newLinkedstack and pushes items from the current stack by popping them. So at the end of the operation the current stack is left empty and newLinkedStack contains the same items/elements that were in "this" stack only in reverse order.

My code compiles but when I create and fill up a stack and then print out the contents, I only get one result.

public LinkedStack<T> rev()
{
LinkedStack<T> revStack=new LinkedStack<T>();
final int SIZE=this.size();

for(int i=0;i<SIZE;i++)
{
revStack.push(this.pop());
}
return revStack;
}
For example

LinkedStack<String> newLS = new LinkedStack<String>();
newLS.push("noid");
newLS.push("enilec");
newLS.push("River");
newLS.push("Healing");
newLS.push("Pain");
and newLS.rev() returns Pain instead of a reversed stack

UPDATE

Here's my pop method

public T pop()
{
T element=peek();
top=top.next;
return element;
}
I have two push methods this one moves every element from input stack to current stack leaving input stack from argument empty

public void push(LinkedStack<T> stack){
final int SZE = s.size();
for(int i=0;i<SZE;i++){
this.push(stack.pop());}}
Here's my other push method

public void push(T element)
{
top=new Node<T>(item,top);
}
pyler is offline  
 

My Computer Forum is free to register and we welcome everyone!

Reply

  My Computer Forum > Computer Science Forum > Programming

Tags
item, java, returns, reversed, stack



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning Java and C++ chetanbhasin Programming 6 February 22nd, 2013 02:24 AM
Best book to learn java chetanbhasin Programming 7 February 13th, 2012 09:46 AM
Java Basics asbo Programming 0 April 30th, 2010 06:52 PM





Copyright © 2017 My Computer Forum Forum. All rights reserved.