0

I don't know if this is a silly question or maybe it's comenscence but my question is I'm creating a Linked List class which contains an add Method

public void addFirst(int data){
        node node = new node(data);
        if (head == null) {
            head = node;
            tail = node;
            currentSize++;
        }
        else
        node.next = head;
        head = node;
        currentSize++;
    }  

} so when i use it like this:

public static void main(String argas[]){
Linkedlist list = new Linkedlist();
  
  list.addFirst(5)
  list.addFirst(10)
  list.addFirst(15)
  list.addFirst(20)

the node that contains 5 has the same name as the node contains 10 and the rest of nodes, how does it works?

The complete code

public class LinkedList {
    class node {
        int data;
        node next;

        public node(int data) {
            this.data = data;
            next = null;
        }
        public node(){
        }

    }
    private node head;
    private node tail;
    private int currentSize;
        public LinkedList (){
        head = null;
        currentSize = 0;
        }

        public void addFirst(int data){
            node node = new node(data);
            if (head == null) {
                head = node;
                tail = node;
                currentSize++;
            }
            else
            node.next = head;
            head = node;
            currentSize++;
        }
        public void addLast(int data){
            node node = new node(data);
            node tmp = new node();
            if (head == null) {
                head = node;
                tail = node;
                currentSize++;
                return;
            }
            tail.next = node;
            tail = node;
            currentSize++;
            return;
        }

        public void removeFirst(){
            if (head == null){
                return;
            }
            if (head == tail){
                head = tail = null;
                currentSize--;
            }
            else
            head = head.next;
            currentSize--;
        }

        public void removeLast(){
            if (head == null){
                return;
            }
            if (head == tail){
                head = tail = null;
                return;
            }
            else {
                node tmp = new node();
                tmp = head;
                while (tmp.next != tail){
                    tmp = tmp.next;
                }
                tmp.next = null;
                tail = tmp;
                currentSize--;
            }
        }


        public void printList(){
            node tmp = new node();
            tmp = head;

            while (tmp != null){
                System.out.println(tmp.data);
                tmp = tmp.next;
            }
        }

        public void size(){
            System.out.println((currentSize));
        }

}
  • 1
    What do you mean by _name_ here? Btw, you should work on your class naming: `node node` should be `Node node`, i.e. the class name should start with a capital letter to avoid confusion with variable names. – Thomas Dec 15 '21 at 07:13
  • Another side note: you should also work on code formatting and ideally use curly braces even for one-statement blocks. Right now that else block of yours can make it hard to spot errors or the intent, i.e. would `head = node;` be intended to be inside the else block or not? (right now it isn't) – Thomas Dec 15 '21 at 07:17
  • @Thomas I mean : when we create a new node this's what happening Node node = new Node(data); everytime we create a new node we create it with the name "node" how can this be possible – Saleh Salem Dec 15 '21 at 07:20
  • You must learn about `variable scopes` and `references` in Java. That will give you a lot of clarity – Kris Dec 15 '21 at 07:30
  • Honestly, the real answer here is: pick a good book on Java, and start reading slowly and carefully. This community is for specific questions on programming, it is not meant as replacement for you doing that *learning* part. Your request basically boils down to "please explain to me some fundamental concepts of programming languages". – GhostCat Dec 15 '21 at 07:35
  • For starters, you could read this question and the answers to it. https://stackoverflow.com/questions/7019754/what-does-the-new-keyword-actually-do-in-java-and-should-i-avoid-creating-new – GhostCat Dec 15 '21 at 07:35
  • 1
    @GhostCat Thanks – Saleh Salem Dec 15 '21 at 07:40
  • And yes, you definitely want to learn about java naming practices. Class names go UpperCase, always. And you try very hard to not "shadow" names, like calling a class `node` , and then a field / variable `node` too. – GhostCat Dec 15 '21 at 07:47

1 Answers1

0

Follow the good suggestions in the above comments. However, for any other new learner coming here and looking for any answer, I am giving below the correction to be done in addFirst() method, you need to change other functions if you have not used blocks properly. Try to rename your class to Node, and so your source file to Node.java

public void addFirst(int data)
{
     node node = new node(data);
     if (head == null) 
     {
            //this is the first ever node, set the head & tail
            head = node;
            tail = node;
     }
     else
     {   
           //place the below lines of code together in a block
           node.next = head;
           head = node;
      }
     currentSize++;
}
Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22