1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Implementing Singly Linked List

  1. Apr 17, 2015 #1
    1. The problem statement, all variables and given/known data

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


    2. Relevant equations

    None

    3. 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 (Java):

    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: Apr 17, 2015
  2. jcsd
  3. Apr 17, 2015 #2

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    My Java is a little rusty, but isn't LinkedList a Class, how does one Class implement another?
     
  4. Apr 17, 2015 #3
    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.
     
  5. Apr 17, 2015 #4

    gabbagabbahey

    User Avatar
    Homework Helper
    Gold Member

    In that case, maybe show the class that has your main method
     
  6. May 3, 2015 #5
    What does it mean to find two lists, given initial conditions for each and a dual array implementation of linked lists?
     
  7. May 3, 2015 #6

    Zondrina

    User Avatar
    Homework Helper

    I believe there might be some inconsistency with this line and your constructors:

    Code (Text):
    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 (Text):

    public void Push(Node<E> headRef, E element) {
         Node<E> newNode = constructor call...;
         newNode.element = element;
         newNode.next = headRef;
         headRef = newNode;
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Java Implementing Singly Linked List
Loading...