Java Implementing Singly Linked List

  • Context: Comp Sci 
  • Thread starter Thread starter Robben
  • Start date Start date
  • Tags Tags
    Java List
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a singly linked list in Java, focusing on issues encountered when attempting to add elements to the list in the main method. Participants explore the structure of the linked list, its methods, and potential errors in the implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their implementation of a singly linked list and expresses confusion about why elements are not being added in the main method.
  • Another participant questions the implementation of the LinkedList interface, noting that it is a class and asks how one class can implement another.
  • A third participant mentions having redefined the LinkedList class as an interface with methods like add, addAll, remove, get, and size.
  • There is a request for the participant to show the class containing the main method to better understand the issue.
  • One participant points out a potential inconsistency in the constructor call for creating a new node and suggests that the add method's behavior may differ from typical implementations, which often add new nodes to the front of the list instead of the rear.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the specific issue causing the failure to add elements to the singly linked list. Multiple viewpoints and suggestions are presented, indicating ongoing uncertainty and exploration of the problem.

Contextual Notes

There are unresolved questions regarding the implementation details of the LinkedList interface and the behavior of the add method in relation to typical singly linked list operations.

Robben
Messages
166
Reaction score
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
My Java is a little rusty, but isn't LinkedList a Class, how does one Class implement another?
 
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.
 
In that case, maybe show the class that has your main method
 
What does it mean to find two lists, given initial conditions for each and a dual array implementation of linked lists?
 
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   Reactions: Robben

Similar threads

  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K