Results 1 to 5 of 5
  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Question about stack and linkedlist push() and pop()

    I tried to compare linkedlist and stack push() and pop() method and this was the result.

    Stack
    Java Code:
    Stack<String> st = new Stack<>();
    		
    		st.push("a");
    		st.push("b");
    		st.push("c");
    		st.push("d");
    		
    		Iterator<String> iterator = st.iterator();
    		
    		while(iterator.hasNext())
    		{
    			System.out.print(iterator.next() + " ");
    		}
    		
    		System.out.println("popped: " + st.pop());
    result:
    Java Code:
    a b c d popped: d
    LinkedList
    Java Code:
    LinkedList<String> llist = new LinkedList<>();
                    
            llist.push("a");
    		llist.push("b");
    		llist.push("c");
    		llist.push("d");
    		
    		
    		
    		Iterator<String> iterator = llist.iterator();
    		
    		while(iterator.hasNext())
    		{
    			System.out.print(iterator.next() + " ");
    		}
    		
    		System.out.println("popped: " + llist.pop());
    result
    Java Code:
    d c b a popped: d
    So, my question is, which collection does LIFO(last-in-first-out) structure and which collection does FIFO(first-in-first-out). I'm probably sure that stack does LIFO because it's mentioned in java documentation https://docs.oracle.com/javase/8/doc...til/Stack.html and the process of pushing and
    popping is the same to this article https://en.wikipedia.org/wiki/Stack_...act_data_type)

    So, if Stack does LIFO does it mean that linkedlist does FIFO?

    This is my understanding on FIFO: the first element that we push will also be the first to be popped, It's just like a queue in cinema booth ticket. So, in my understanding, element "a" should be popped instead of "d". Correct me if i'm wrong.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,971
    Rep Power
    7

    Default Re: Question about stack and linkedlist push() and pop()

    Even though the method name is the same: push(), the implementation for LinkedList is this:
    Java Code:
        public void push(E e) {
            addFirst(e);
        }
    and the popping code is:
    Java Code:
        public E pop() {
            return removeFirst();
        }
    That confirms that a LinkedList is LIFO. Whatever you pushed last is popped off first.

    Queues on the other hand are usually FIFO.

    There are some interesting reads for you in the sourcecode of Queue, Deque.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Question about stack and linkedlist push() and pop()

    If you want the LinkedList to behave AND appear like the Stack class, you could probably just use add and removeLast methods. I would suspect the removeLast is nothing more than remove(list.size()-1) with appropriate checks.

    I also find these convenience methods a little dangerous since you should use them in pairs. I.e. you can't use add several times and then expect pop to work as you want.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    54
    Rep Power
    0

    Default Re: Question about stack and linkedlist push() and pop()

    So, even the way push() and pop() behave differently in linkedlist it's still consider as LIFO. thanks for clarification, the way that push() and pop() in linkedlist operates in reverse to stack push() and pop() makes me confuse.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,221
    Rep Power
    12

    Default Re: Question about stack and linkedlist push() and pop()

    Push() and pop() in both Stack and LinkedList provide the same service. Their storage order and how the elements are retrieved are what is different. But the implementation details in this and most other classes should be unimportant to the user.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. LinkedList question
    By clerd in forum New To Java
    Replies: 21
    Last Post: 11-19-2013, 03:45 AM
  2. linkedlist question
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-06-2012, 04:37 AM
  3. Stack (push/pop/top)
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-08-2011, 02:40 AM
  4. Making a stack from a LinkedList
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:28 PM
  5. Stack push/pop/peek operations
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •