Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Stack List

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Stack List

    public class GenericStackApp
    {
        public static void main(String args[])
        {
            GenericStack<String> q1 = new GenericStack<>();
            q1.push("Apples");
            q1.push("Oranges");
            q1.push("Bananas");
     
            for (int i=0; i < q1.size(); i++)
            {
            System.out.println("Push: " + q1.push());
            }
            System.out.println("The stack contains " + q1.size() + " items");
     
            System.out.println();
     
            System.out.println("Peek: " + q1.peek());
            System.out.println("The stack contains " + q1.size() + " items");
            System.out.println();
     
            while (q1.size() > 0)
            {
            System.out.println("Pop: " + q1.pop());
            }
            System.out.println("The stack contains " + q1.size() + " items");
     
     
        }
    }
    import java.util.*;
    public class GenericStack<E> 
    {
    private LinkedList<E> list = new LinkedList<>();
     
    public void push(E item)
    {
     
        list.addLast(item);
    }
     
    public E push()
    {
        return list.element();
    }
     
    public E peek()
    {
        return list.getLast();
     
    }
     
    public E pop()
    {
        return list.removeLast();
     
     
    }
     
    public int size()
    {
        return list.size();
    }
     
     
    }

    My output was like this:
    Push: Apples
    Push: Apples
    Push: Apples
    The stack contains 3 items

    Peek: Bananas
    The stack contains 3 items

    Pop: Bananas
    Pop: Oranges
    Pop: Apples
    The stack contains 0 items

    I need it to be:
    Push: Apples
    Push: Oranges
    Push: Bananas

    I don't know what to do next.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Stack List

    It's been a while. Have you made any progress?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Stack List

    Why do you have 2 push methods? One actually adds an element to the List the other returns an element. Which are you calling?
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Stack List

    I am kind of confused really. What I'm trying to do is reverse the results of pop meaning my stack should be in order. I haven't made a progress yet.

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. Replies: 3
    Last Post: October 5th, 2013, 09:14 AM
  3. STACK USING LINKED LIST------------HELP !
    By jayeshsolanki93 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 26th, 2012, 10:27 AM
  4. Linked list Schminked list help with Nodes Please
    By Bially in forum Collections and Generics
    Replies: 1
    Last Post: September 29th, 2011, 03:20 PM
  5. Stack
    By AmyH in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2010, 04:04 PM