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

LinkBack Thread Tools Display Modes
October 2nd, 2013, 09: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++)
return revStack;
For example

LinkedStack<String> newLS = new LinkedStack<String>();
and newLS.rev() returns Pain instead of a reversed stack


Here's my pop method

public T pop()
T element=peek();;
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++){
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!


  My Computer Forum > Computer Science Forum > Programming

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 01:24 AM
Best book to learn java chetanbhasin Programming 7 February 13th, 2012 08:46 AM
Java Basics asbo Programming 0 April 30th, 2010 05:52 PM

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