Skip to content

Instantly share code, notes, and snippets.

@cirussaeb
Last active November 1, 2017 08:12
Show Gist options
  • Save cirussaeb/90e2ab6ce6ce86e8b41f2a13ba772e31 to your computer and use it in GitHub Desktop.
Save cirussaeb/90e2ab6ce6ce86e8b41f2a13ba772e31 to your computer and use it in GitHub Desktop.
Node class and LinkeList class
package ds.SinglyLinkedList;
public class Node {
public int data;
public Node next;
public displayNode() {
System.out.println("{ "+ data + " }");
}
}
//End of Node class
//Start of LinkedList class
package ds.SinglyLinkedList;
public class SinglyLinkedList {
private Node first;
public singlyLinkedList() {
}
public boolean isEmpty() {
return (first == null);
}
//used to insert at the beginning of the list
public void insertFirst(int data) {
Node newNode = new Node();
newNode.data = data;
newNode.next = first;
first = newNode;
}
public Node deleteFirst() {
Node temp = first;
first = first.next;
return temp;
}
public void displayList() {
System.out.println("List (first --> last");
Node current = first;
while(current != null) {
current.displayNode();
current = current.next;
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment