From 445a57d57b1ddfc2a7516e47d1d8c74afd0ee2a2 Mon Sep 17 00:00:00 2001 From: Visrut Date: Tue, 2 Feb 2021 21:32:08 +0530 Subject: [PATCH] added singly linked list in java and javascript (#53) * added singly linked list in java * added commnets * addded singly linked list in javascript * Rename SinglyLinkedLIst.java to singly-linked-list.java --- linked-lists/java/singly-linked-list.java | 64 +++++++++++++++++++++++ linked-lists/js/singly-linked-list.js | 55 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 linked-lists/java/singly-linked-list.java create mode 100644 linked-lists/js/singly-linked-list.js diff --git a/linked-lists/java/singly-linked-list.java b/linked-lists/java/singly-linked-list.java new file mode 100644 index 00000000..779e46fc --- /dev/null +++ b/linked-lists/java/singly-linked-list.java @@ -0,0 +1,64 @@ +class Node { + T data; + Node next; + + Node(T data) { + this.data = data; + this.next = null; + } +} + +public class SinglyLinkedList { + Node head; + + SinglyLinkedList() { + this.head = null; + } + + void insertAtHead(T data) { + Node newNode = new Node(data); + if(this.head == null) { + this.head = newNode; + } else { + newNode.next = this.head; + this.head = newNode; + } + } + + T removeAtHead() { + // check for underflow + if(this.head == null) { + System.out.println("Underflow"); + System.exit(1); + } + // store data for return + T data = this.head.data; + this.head = this.head.next; + return data; + } + + void printList() { + // if head is null then list is empty + if(this.head == null) { + System.out.println("List is Empty"); + } else { + // take itr same as head reference and loop through it until it became null + Node itr = this.head; + while(itr != null) { + System.out.print(itr.data + " "); + itr = itr.next; + } + System.out.println(""); + } + } + + // Test Code + public static void main(String... args) { + SinglyLinkedList l = new SinglyLinkedList(); + l.insertAtHead("abc"); + l.insertAtHead("pqr"); + l.insertAtHead("xyz"); + l.removeAtHead(); + l.printList(); + } +} diff --git a/linked-lists/js/singly-linked-list.js b/linked-lists/js/singly-linked-list.js new file mode 100644 index 00000000..05825af0 --- /dev/null +++ b/linked-lists/js/singly-linked-list.js @@ -0,0 +1,55 @@ +class Node { + constructor(data) { + this.data = data; + this.next = null; + } +} + +class SinglyLinkedList { + constructor() { + this.head = null; + } + + insertAtHead(data) { + let newNode = new Node(data); + if(this.head == null) { + this.head = newNode; + } else { + newNode.next = this.head; + this.head = newNode; + } + } + + removeAtHead() { + // check for underflow + if(this.head == null) { + console.log("Underflow"); + return; + } + // store head's reference data value in local data variable + let data = this.head.data; + this.head = this.head.next; + return data; + } + + printList() { + // if head is null then list is empty + if(this.head == null) { + console.log("List is Empty"); + } else { + // iterate through list until itr is not null + let itr = this.head; + while(itr != null) { + console.log(itr.data); + itr = itr.next; + } + } + } +} + +let l = new SinglyLinkedList(); +l.insertAtHead(1); +l.insertAtHead('xyz'); +l.insertAtHead(1.1); +l.removeAtHead(); +l.printList()