Last active
October 12, 2021 22:24
-
-
Save lobster1234/5037064 to your computer and use it in GitHub Desktop.
A generic, singly LinkedList Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LinkedList<T> { | |
private LinkedListNode<T> first = null; | |
/** | |
* Insert at the front of the list | |
* @param node | |
*/ | |
public void insert(LinkedListNode<T> node) { | |
node.setNext(first); | |
first = node; | |
} | |
/** | |
* Remove from the front of the list | |
* @param node | |
*/ | |
public void remove(){ | |
if(first.getNext()!=null) | |
first = first.getNext(); | |
else first = null; | |
} | |
/** | |
* Recursively traverse this list and print the node value | |
* @param node | |
*/ | |
private void printList(LinkedListNode<T> node) { | |
System.out.println("Node is " + node.getValue()); | |
if(node.getNext()!=null) printList(node.getNext()); | |
} | |
public void print(){ | |
printList(first); | |
} | |
public static void main(String[] args) { | |
LinkedList<String> list = new LinkedList<String>(); | |
list.insert(new LinkedListNode<String>("Manish")); | |
list.insert(new LinkedListNode<String>("Pandit")); | |
list.insert(new LinkedListNode<String>("Tanvi")); | |
list.insert(new LinkedListNode<String>("Monika")); | |
list.print(); | |
list.remove(); | |
System.out.println("After removing the head.."); | |
list.print(); | |
} | |
} | |
class LinkedListNode<T> { | |
private T value; | |
private LinkedListNode<T> next; | |
public LinkedListNode(T value) { | |
this.value = value; | |
} | |
public void setNext(LinkedListNode<T> next) { | |
this.next = next; | |
} | |
public LinkedListNode<T> getNext() { | |
return next; | |
} | |
public T getValue() { | |
return value; | |
} | |
} |
Line 16: There is no param there?
Weee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Is it possible to Sort this Linked List?