Java Implementing Singly Linked List

  • Comp Sci
  • Thread starter Robben
  • Start date
  • Tags
    Java List
In summary, the homework equation is not working. The Attempt at a Solution suggests that the problem might be with the linkedlist class definition, so the user posts a different linkedlist implementation.
  • #1
Robben
166
2

Homework Statement



I am implementing a singly linked list but when I am in my main method, to add into my singly linked list, nothing happens.

Homework Equations


[/B]
None

The Attempt at a Solution



I am trying to implement a singly linked list.

My singly linked list, where I implemented a class named linkedlist, that I defined (this implementation of linkedlist is not the java class linkedlist, it is my own version of linkedlist):

Code:
public class SinglyLinkedList<E> implements LinkedList<E> {

    public class Node<E> {

        public Node<E> next;
        public E element;

        public Node(E element) {
  
            this.element = element;
        }

        public Node (E element, Node<E> next) {
       
            this.element = element;
            this.next = next;
        }

        public E getElement() {

            return element;
        }

        public Node<E> getNext() {
    
            return next;
        }

        public void setElement(E element) {
  
            this.element = element;
        }

        public void setNext(Node<E> next) {
  
            this.next = next;
        }
    }

    public Node<E> head;
    public Node<E> tail;
    public int total;

    public SinglyLinkedList() {

        this.head = null;
        this.tail = null;  
        total = 0;
    }

    public void add(E element) {

        Node<E> singlyAdd = new Node<E>(element);

        if (tail == null) {

            head = singlyAdd;
            tail = singlyAdd;
        } else {

            tail.setNext(singlyAdd);
            tail = singlyAdd;
        }
  
        total++;
    }
}

But when I go to my main method to add into my SinglyLinkedList, it doesn't add anything. I am wondering what is wrong with my code?
 
Last edited:
Physics news on Phys.org
  • #2
My Java is a little rusty, but isn't LinkedList a Class, how does one Class implement another?
 
  • #3
gabbagabbahey said:
My Java is a little rusty, but isn't LinkedList a Class, how does one Class implement another?

I redefined the LinkedList class. I can post it if you want but it's just an interface with a few methods like add, addall, remove, get, size, and more.
 
  • #4
In that case, maybe show the class that has your main method
 
  • #5
What does it mean to find two lists, given initial conditions for each and a dual array implementation of linked lists?
 
  • #6
I believe there might be some inconsistency with this line and your constructors:

Code:
Node<E> singlyAdd = new Node<E>(element);

Also, the way your ##\text{push}()## method is written is a bit different than I'd expect (I'm referring to your ##\text{add}()## method). Usually, most singly linked lists push the new node onto the front of the list rather than the rear. Here's some sample code with the constructor call omitted (I leave it to you):

Code:
public void Push(Node<E> headRef, E element) {
     Node<E> newNode = constructor call...;
     newNode.element = element;
     newNode.next = headRef;
     headRef = newNode;
}
 
  • Like
Likes Robben

What is a singly linked list?

A singly linked list is a data structure in which each element, or node, contains a reference to the next node in the list. This creates a linear sequence of nodes that can be easily traversed from the beginning to the end.

Why should I use a singly linked list?

Singly linked lists are useful for storing and manipulating data in a dynamic and efficient manner. They allow for constant time insertion and deletion at the beginning of the list, and can be easily implemented in languages like Java.

How do I implement a singly linked list in Java?

To implement a singly linked list in Java, you will need to create a Node class that contains a data element and a reference to the next node. Then, you will need to create a LinkedList class that contains a reference to the head node and methods for adding, removing, and traversing the list.

What is the time complexity of operations on a singly linked list?

The time complexity of operations on a singly linked list is O(1) for insertion and deletion at the beginning of the list, and O(n) for insertion and deletion at other positions, as well as searching and accessing elements in the list.

What are the advantages of a singly linked list over other data structures?

Singly linked lists have a lower memory footprint compared to other data structures, as they only require enough memory for the data and a reference to the next node. They also allow for easy insertion and deletion at the beginning of the list, and can be used to implement other data structures such as stacks and queues.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
833
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
11K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top