Java linked list implementation

NoobCoderChick

I am learning Linked Lists right now and I'm a little confused. I'm working on an assignment where we're creating our own implementation of some methods to be used with a linked list.

I watched a lot of videos on the concept of Linked List and how Nodes work like getting data and setting the next element but I'm confused on how to make a list to test the methods I'm implementing. I know this may sound silly but I start to get super confused with multiple parameters in methods and return statements get involved and I've never worked with a generic data type before either so those basic ideas might be confusing me here.

I'd love some help with creating a list so I can test the methods as I'm creating them, I'm having difficulty printing out a list. I get that what I'm doing in the main is creating new nodes and adding to some list but I honestly don't know where this supposed LinkedList is or what it's called. Any help is greatly appreciated!! FYI, I'm very new to Java, this is my second class and it's online and not a very well constructed class, please be nice lol

I get the concept of this BUT what is the name of the list (or linked list) these are being added to?

    ListNode node4 = new ListNode("Fourth", null);
    ListNode node3 = new ListNode("Third", node4);
    ListNode node2 = new ListNode("Second", node3);
    ListNode node1 = new ListNode("First", node2);

I DON'T KNOW HOW I CAN PRINT THESE OUT INDIVIDUALLY LIKE I CAN WITH THE ABOVE BECAUSE I CAN USE THE NAMES ABOVE

ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));

Here is my code with the testing class where I have questions as comments which may help you understand what I'm confused about if I'm not making sense above. I left out the ListNode class as it is your basic getNext() setNext() Node class called ListNode with a generic type ....

//Testing program for SinglyLinkedList class
public class LinkedListDriver {

public static void main(String[] args) {
    //List<String> list = new LinkedList<String>();                   //comment out this line to test your code
    SinglyLinkedList<String> list = new SinglyLinkedList<String>();  //remove comment to test your code
    SinglyLinkedList SLL = new SinglyLinkedList();

    ListNode node4 = new ListNode("Fourth", null);
    ListNode node3 = new ListNode("Third", node4);
    ListNode node2 = new ListNode("Second", node3);
    ListNode node1 = new ListNode("First", node2);

//ListNode value = new ListNode("First", new ListNode("Second", new ListNode("Third", null) ));

System.out.println(node2.getData());

//Is this the correct way to add a new node to this method?
    SLL.addLast(new ListNode("Fifth", null));
//I doubt my printList is correct as I do not know what parameter I am       supposed to pass to it.    
    SLL.printList();
}

SinglyLinkedList class

//This class implements a very simple singly-linked list of Objects
public class SinglyLinkedList<E> {
     ListNode<E> first; // first element

public SinglyLinkedList()  {
    first = null;
}

public E getFirst() {
   if (first == null) {
      throw new NoSuchElementException();
   }
   else
      return first.getData();
   }

public void addFirst(E value) {
    first = new ListNode<E>(value, first);
}

// Methods below implemented by you. Note: while writing methods, keep in mind
// that you might be able to call other methods in this class to help you - you
// don't always need to start from scratch(but you'll have to recognize when)
public void addLast(E value) {    
    ListNode<E> temp = first;
    //If list is empty make new node the first node.
    if(temp == null) {
        first = new ListNode <E>(value, null);
        first.setNext(null);
    }
    //Otherwise loop to end of list and add new node.
    else {
        while(temp.getNext() != null) {
            temp = temp.getNext();
        }
    temp.setNext(new ListNode<E>(value, null));
    }
}//end addLast

// throws an exception - you decide when and which one
public E getLast() {
    ListNode<E> temp = first;
    if(temp == null) {
        throw new NullPointerException("There are no elements in this list to get.");
    }
    else {
        while(temp.getNext() != null) {
            temp = temp.getNext();
        }
        return temp.getData();
    }
}

// throws an exception - you decide when and which one
public E removeFirst() {
    if(first == null) {
        throw new NullPointerException("There are no elements in this list to remove.");
    }
    ListNode<E> tempRemove = first;  
    return null;//just so it'll compile
  }

  // throws an exception - you decide when and which one
  public E removeLast() {
     return null; //just so it'll compile
  }

  // return the number of elements in the list
  public int size() {
      return 0;//just so it'll compile
  }

  // return true if o is in this list, otherwise false
  public boolean contains(E obj) {
      return true;//just so it'll compile
  }

 public void printList(java.io.PrintStream out) {
     if(first == null) {
        System.out.println("The list is empty");
     }
     ListNode<E> current = first;
     while(current != null) {
         System.out.println(current.toString());
         current = current.getNext();
     }
  }

  public String toString() {
       String s = "[";
       ListNode<E> current = first;
    //write code to traverse the list, adding each object on its own line
    while(current.getNext() != null) {
        current = current.getNext();
    }

  s += "]";
  return s;
  }

  // OPTIONAL: just for fun...and a challenge
  public void reverse() {
  }
}    
wadda_wadda

Linked lists are basic data structures.

A basic linked list implementation is as follows:

class LinkedListNode {
  int value;
  LinkedListNode next;
}

The basic idea is that you can iterate through the list by checking to see if the list has a next parameter, like so: while (currentNode.next != null) { ... }.

Printing out the values of a linked list takes advantage of the aforementioned while-loop, and you just print the value of the current node.

You can think of a linked list as a line of people who can only see the person ahead of them, and not who is behind them. You know where the person at the very start of the line -- that is, the one with nobody 'behind' him -- is. This person is called the "head" of the list. He knows who is directly in front of him, and each person knows who is directly in front of him. When you reach the point where somebody doesn't have anyone in front of them, you've gone through the whole line.

Here's an example of adding a new element to the end of a list:

LinkedListNode node = myListHead;
while (node.next != null) {
  node = node.next;
}
node.next = new LinkedListNode(...);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linked List Stack Implementation - Java

From Dev

Efficiency of Doubly linked list implementation in java

From Dev

Is there any doubly linked list implementation in Java?

From Dev

Is there an immutable singly linked list implementation in Java?

From Dev

Linked list implementation in C?

From Dev

linked list implementation in c

From Dev

Singly Linked List Implementation

From Dev

Implementation of singly linked list

From Dev

Linked List - Implementation

From Dev

Sort Linked List implementation

From Dev

Singly Linked List Implementation

From Dev

Linked List implementation of a Graph

From Dev

Clarification in deleting a specific link from Doubly Linked List implementation in Java

From Dev

Delete k-th element in a linked list (Java implementation)

From Dev

size not counting properly in linked list implementation of queue in java

From Dev

Double linked list implementation in c

From Dev

The reason for final in the Linked List Implementation

From Dev

Stuck with a nullpointerexception on linked list implementation

From Dev

Single Linked List with Generic Implementation

From Dev

Different implementation of reverse linked list

From Dev

Linked list Implementation in C with structure

From Dev

Implementation of the 'set' method for a linked List?

From Dev

Linked List Implementation Options in C

From Dev

Singly Linked List Python Implementation

From Dev

Linked list implementation segmentation fault

From Dev

Stack corruption with linked list implementation

From Dev

Understanding a Linked List implementation in Python

From Dev

Java Linked List of Linked lists

From Dev

Benefit and disadvantage of linked list or array implementation of list

Related Related

HotTag

Archive