Last active
November 1, 2017 08:12
-
-
Save cirussaeb/90e2ab6ce6ce86e8b41f2a13ba772e31 to your computer and use it in GitHub Desktop.
Node class and LinkeList class
This file contains 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
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