0

I'm learning to implement a Stack and I'm struggling to understand what .next REALLY means. I've seen it in many data structures and it's obvious what its purpose is until I actually ask myself if it is a keyword or how it functions. It looks like it is an object but it seems to be acting like a pointer (like in a LinkedList) to each of the item objects that are created.

Please help in any way you can to shed some light on my confusion!

class Item {
    intdata;
    Item next;

    public Item(int data) {
        this.data = data;
    }
}

public class Stack {

   private Item top;

   public void push(int data) {
       if (null == top) {
           top = new Item(data);
       } else {
           Item item = new Item(data);
           item.next = top;
           top = item;
       }
   }
}
springathing
  • 425
  • 3
  • 8
  • 28
  • *It looks like it is an object but it seems to be acting like a pointer (like in a LinkedList) to each of the item objects that are created.* You are not confused. That is 100% correct (well, it **is** an object). – Elliott Frisch Oct 26 '17 at 01:47
  • It's not clear what you're confused about, please explain it better. Each `Item` has a field `next` which is of type `Item` and is a reference to another `Item`. What's not clear? – Oleg Oct 26 '17 at 02:00
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Usagi Miyamoto Oct 26 '17 at 02:01
  • If you understand how a linked list works, a stack shouldn't be too mysterious: it's just a linked list that you only access from one end. – Kevin Anderson Oct 26 '17 at 02:02
  • 1
    And, no `next` isn't a keyword: its the name of a member field of your `Item` class. You could have called it anything you wanted: `link`, `previous`, `boogabooga`, whatever, the point being it's a programmer-chosen name, not a language-imposed reserved word or keyword. – Kevin Anderson Oct 26 '17 at 02:04
  • I thought that "next" was some kind of method or keyword from a library that meant "look at next address" but then I saw the instantiation of "Item next" and didn't understand that it is an object that somehow assumes a functional ability of a pointer. (I hope I worded what I"m trying to say properly) – springathing Oct 26 '17 at 02:05
  • 1
    Technically, `Item next;` in the `Item` class is declaring a member field `next` which is of type "reference to `Item`". If you're familiar with C++ references (e.g., `Item&`), it's the same general idea. – Kevin Anderson Oct 26 '17 at 02:23
  • I'll have to read up more of C++ references, I seem to be lacking some understanding in object orientated programming. I just looked it up and there is apparently a difference between a reference and a pointer so thank you for the new information to learn – springathing Oct 26 '17 at 02:29
  • 1
    Please don't, unless you want to be even more confused. @KevinAnderson was imprecise. References in Java are like c++ pointers only the pointer is hidden. Java doesn't have anything equivalent to c++ reference. – Oleg Oct 26 '17 at 03:14

1 Answers1

1

Just think about this, if you have two initial raw data 1 and 2. And, on the other hand, I tell you to map all the natural numbers as Items and then put them into a stack.

Considering, you might just come up with let us say, two Item objects currently unrelated as -

Item item1 = new Item(1);

and

Item item2 = new Item(2);

and then as you put them into the Stack, you push item1 and you would eventually push item2. There could similarly be N number of data that you might have pushed.

And now to make you a little smarter, I would ask you to remember while you pop an element from that Stack as to what could be the next element possible, just to see if we might even need to pop more or not from that stack. [Let's say need be to display only elements greater than 5]

That is where the relation between the two items would become useful. You make one item point to the next while pushing them into the stack to keep a reference of the same. And so you do:-

item2.setNext(item1); // using setter for 'next'

Which eventually means while accessing item2 you can actually be aware of the next Item it points to i.e. item1.

PS: The same implementation as item2.setNext(item1) is generalized in your code's push method which accepts a data and creates a new instance of Item every time its called.

Naman
  • 27,789
  • 26
  • 218
  • 353