How to add objects to a linked list?

I am working on a project where I have to implement a java class that implements the use of doubly linked lists. I have a LinkedList class with all my methods. I just don't know how to actually add node objects to the list. Here is my code so far with the test below. Any help would be greatly appreciated. Thanks

public class LinkedList { private Node first; private Node current; private Node last; private int currentIndex; private int numElements; public LinkedList() { this.first = null; this.last = null; this.numElements = 0; this.current = null; this.currentIndex = -1; } private class Node { Node next; Node previous; Object data; } public boolean hasNext() { return (current != null && current.next != null); } public Object next() { if (!this.hasNext()) { throw new IllegalStateException("No next"); } current = current.next; return current.data; } public boolean hasPrevious() { return (current != null && current.previous != null); } public Object previous() { if (!this.hasPrevious()) { throw new IllegalStateException("No previous"); } current = current.previous; return current.data; } int nextIndex() { int index = numElements; if (hasNext()) { index = this.currentIndex + 1; } System.out.println(index + "The current index is " + current); return index; } int previousIndex() { int index = -1; if (hasPrevious()) { index = this.currentIndex - 1; } System.out.println(index + "The current index is " + current); return index; } public void set(Object o) { if (this.current == null) { throw new IllegalStateException("No node found, cannot set."); } current.data = o; } public int size() { return numElements; } public void add(Object o) { Node newNode = new Node(); newNode.data = o; if (first == null) { first = newNode; last = newNode; newNode.next = null; } else if (first != null) { if (current == null) { newNode.previous = null; newNode.next = first; first.previous = newNode; first = newNode; } else if (current == last) { newNode.previous = current; newNode.next = null; current.next = newNode; last = newNode; } else { newNode.previous = current; newNode.next = current.next; current.next.previous = newNode; current.next = newNode; } } current = newNode; numElements++; currentIndex++; } public void remove() { if (current != null) { if (current == first && current == last) { first = null; last = null; } else if (current == last) { current.previous = null; last = current.previous; } else if (current == last) { current.previous.next = null; last = current.previous; } else { current.previous.next = current.next; current.next.previous = current.previous; } current = current.next; numElements--; } } } import java.util.Scanner; public class LinkedListTest { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String name; int index; LinkedList<Object> listOne = new LinkedList<Object>(); listOne.add(object o); } } 
+4
source share
7 answers

The published LinkedList class looks functional for me.

Make sure your test code does not confuse this class with the java.util.LinkedList that Java provides to you (this is part of the existing collection structure).

For clarity, I would recommend renaming your class to something like MyLinkedList

The following code works, and the output is "0", "2":

 public class MyLinkedListTest { public static final void main(String[] args) { MyLinkedList list = new MyLinkedList(); System.out.println("Number of items in the list: " + list.size()); String item1 = "foo"; String item2 = "bar"; list.add(item1); list.add(item2); System.out.println("Number of items in the list: " + list.size()); // and so on... } } 
+4
source

I would be surprised if your code is compiled since your class is not generic. Just initialize it as LinkedList listOne = new LinkedList(); (without angle brackets).

Regarding the actual addition of elements, you just need an instance of some Object to add; will do something (assuming that your internal code is working correctly). Try this below:

 Object objectToAdd = "Strings are Objects"; listOne.add(objectToAdd); objectToAdd = new File("C:\\foo.bar"); // Or use any other Objects! listOne.add(objectToAdd); 
+2
source

Think of a numbered list and look at the relationships between the elements.

Let's say I have a list:

  • A
  • IN
  • WITH

What I need to do so that the relationship gets a list:

  • A
  • IN
  • Newnode
  • WITH

The new next node from B is NewNode. The new previous node C is NewNode. So the insert function would like to know the immediate previous node or the nearest next node, and then adjust the relationship.

+1
source

Your LinkedList does not have common types, so you cannot declare it as

 LinkedList<Object> listOne = new LinkedList<Object>(); 

but rather how

 LinkedList listOne = new LinkedList(); 

And now to add items just use your add method

 listOne.add("something"); listOne.add(1);//int will be autoboxed to Integer objects 

Also, if you want to add data from the keyboard, you can use something like

 String line=""; do{ System.out.println("type what you want to add to list:"); line = keyboard.nextLine(); listOne.add(line); }while(!line.equals("exit")); 
+1
source

Line

LinkedList<Object> listOne = new LinkedList<Object>();

will not compile unless you change the class declaration to

class LinkedList<T>

or alternately you can just write

LinkedList listOne = new LinkedLis();

After that, you can add objects to your list. However, you need to create an object to add to it, listOne.add(object o); won't do - at least you have to write listOne.add(new Object()) . (Your code does not create an instance of the object, there is no object that you have already named o , and, in addition, object o does not mean anything in Java and does not compile.

0
source

As already mentioned, your list is not general. However, since they advise you to get rid of the parameter, you can also simply add <Object> or <E> to your linked list implementation and leave your list initialization as is.

So, in your linked list class, you should do something like:

 public class LinkedList<E> 

This will make sure that when using LinkedList<Object> listOne = new LinkedList<Object>(); , E will be translated to Object

0
source

Let me improve your test a bit so that it becomes obvious where your problems are (if any). I commented on the call to the current () method since you did not enable it. (I would leave this alone, as this may confuse you.) The general idea would be to add items to the linked list and go back and forth, checking the items with each step.

 public class LinkedListTest { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String name; int index; LinkedList listOne = new LinkedList(); //Initially we should be empty so we are positioned // at both the beginning and end of the list assert listOne.size() == 0 :"List should be empty"; assert listOne.hasPrevious()==false: "Should be at the beginning of the list"; assert listOne.hasNext()==false : "Should be at the end of the list"; Object firstNode = "I am the first node"; listOne.add(firstNode); //we've added something //I left this commented out since you don't have a current() method. // assert firstNode == listOne.current() : "Our current item should be what we just added"; assert listOne.hasPrevious()==false : "Should not have moved forward in our list yet"; assert listOne.hasNext()==true : "should have an item after our current"; assert listOne.size() == 1 : "Should only have one item in the list"; Object secondNode = "I am the second node"; listOne.add(secondNode); assert listOne.size() == 2 : "Should only have two items in the list"; assert firstNode == listOne.next() : "1st call to next should return the 1st node"; assert listOne.hasPrevious()==true : "We should be positioned after the 1st node"; assert listOne.hasNext()==true : "We should be positioned before the 2nd node"; } } 
0
source

All Articles