-2

I have some problems in understanding the implementation of a Stack class in "Cracking the coding" interview (for those who have the book it is the first page of chapter 3 "Stack and Queues").

The code looks like this:

public class MyStack<T>{
  private static class StackNode<T>{
    private T data;
    private StackNode<T> next;
    public StackNode(T data){
      thid.data = data;
    }
  }
  private StackNode<T> top;
// and so on
}

The problem is that I really do not understand why I should use a generic type T for MyStack and the same for Stacknode. A stack is a "queue" of nodes, which are made of values. When I first tried to to this myself, I did something that looked more like:

class Stack{
  private static class Node{
    private Object data;
    private Node next;

    public Node(Object data){
      this.data = data;
    }
  }
  private Node top;

  public Stack(){
    top = null;
  }
  public Stack(Node top){
    this.top = top;
    top.next = null;
  }
//and so on
}

To me, MyStack<T> means I am creating an Object MyStack made of an other Object T, which does not make any sense. If a stack is made of Nodes, then my type T should be the type StackNode (???). But then, when I define Stacknode<T> it would be like saying Stacknode<Stacknode>. I think I making to much confusion with generic types, but the point is I do not understand why it is not sufficient doing like I did in my code: whatever sort of stack it is, it is made of nodes, which can then of course have a generic data, so I just said that the data of my Node is of type Object, can someone explain the main difference, or at least why in "Cracking the coding interview" it is done like this? What are the advantages? What am I understanding wrong? Thank you in advance!!

  • 2
    If you use Object instead of generics then you don't have type safety because you always have to cast the objects that you take out. – takendarkk Jun 29 '18 at 20:35
  • In the generic example, `T` is the type of `data`, not the node itself. – Radiodef Jun 29 '18 at 20:41
  • Counter-question: when would you use a generic parameter then? As @csmckelvey pointed out: generic parameters give you (some kind of) type safety at compile time. The [erased nature of generics in Java](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html) make it impossible to guarantee runtime type checks. – Turing85 Jun 29 '18 at 20:53

1 Answers1

0

The purpose of the <T> here is to specify a type for the elements to be stored in the stack. Here T is kind of a template, which can be replaced by any type while being declared.

For example, if you wish to create a stack of strings, you can declare it as

Stack<String> myStack;

This would ensure that the internal class Node would also use String as the data type of data. You'll just have to specify the type during the declaration of the stack and it should be good to go. This way you wouldn't have to write the Stack class again when you would need a stack of Integers.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
  • so this means that the use of does not imply that my stack is made of T's (of Objects T)? The thing is that when we initialize vectors for example, we use Vector vector, where String is the type contained in the vector, so my vector will be made of strings. In this case MyStack is made of Nodes, but nodes contain T's. So when do we think one way, when the other? – Gruppo Asek Jun 29 '18 at 20:58
  • @GruppoAsek When you take a look at [the `LinkedList` implementation of OpenJDK](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/LinkedList.java), you will see they use the same concept. For an `ArrayList`, you already have the underlying datastructure in form of an array. For a `LinkedList`, however, you still need the data structure, i.e. the linked nodes. This is why you use the inner class `Node`, that wraps each value. – Turing85 Jun 29 '18 at 21:04
  • When you declare `Stack myStack`, it will automatically ensure that the `Node`s will contain `String`s. To get a hold of it, replace _all_ the `T`s inside the class `Stack` and remove the ``s and see how it goes. – Sufian Latif Jun 29 '18 at 21:05
  • A `Vector` is not "made of Strings". It is a structure containing strings. And a `Stack` would be a stack containing strings. – khelwood Jun 29 '18 at 21:05
  • @khelwood, that's exactly what I don't understand. When you say Stack is a stack containing strings, doesn't a stack contain Nodes? (Of course those nodes then contain strings) but shouldn't it be Stack ? – Gruppo Asek Jun 29 '18 at 21:11
  • @0605002 I understand what you mean, so here is can be better seen as a parameter that is used later on within the class? – Gruppo Asek Jun 29 '18 at 21:12
  • 1
    @GruppoAsek In this implementation, the `Stack` is a linked list of nodes. Each node contain an element of type `T`. And yeah, you can think of it that way: it's like specifying the data types of the elements as a parameter so that we can use the same code for creating stacks of any type of elements. – Sufian Latif Jun 29 '18 at 21:15