From 3f686f645073e00c767f68792aa940d384eb99f8 Mon Sep 17 00:00:00 2001 From: Suryansh Sinha Date: Sun, 2 Oct 2022 15:29:02 +0530 Subject: [PATCH 1/2] Added Singly Linked List implementation in Java --- .DS_Store | Bin 0 -> 8196 bytes .../LinkedList.java | 140 ++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 .DS_Store create mode 100644 Data Structure Implementations/LinkedList.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..567a80b844b544a0f18f2a7c26d85f3ef642d1a8 GIT binary patch literal 8196 zcmeHM%We}f6uoW(Nfbc~DpEI~k=V8+O%YPXCe1@Ah-atJ1)z|0hIZ&=93_)RD5{h- zEZOr5Z216o?D+)#p({Aoc2Jr}H>nT^yRy$6+vm>s)YsEHB_dJicv9-D1DtO8a6tAJI&Dqt1(8x+7hn@eiSdtZ;*)+%5X z_%9XU^T9!7%p2@$R7VFIl>z`0=vDg+)nsxT>r&~%JDJRO)f z*w?7(Bs86bxw9}Cicoh4&r#D!S+=sD`fuHQd()wy2}N8gyX! z=&Qg-lS0@H)JV;YDZuxIh(?}FT!-&ZLr*r7?|Qf0G^_qnZ-zlT&St-hx%tF}i^*g< znMuBK4`kPkYjLMtsI^}3)sB>5ePGw>&jY_(U0f>4D6R#O-%^CY^D*V+ZV-90TacZ| zYb&m+2h!*elbSDcM(_ISnVZRTL#-pm~zr&HH%-nzS8Jq#O>e9!XZh!Yy= zlEDFdfH9K>;r_b2CmYRy!&&_`WT|8{P5n3tKYok9@@|^x85`0&cc0CJ^Ii9m+loRN z?PIO{yIP|A*zZTEZS3_Hc5@T^n&VDlr@x~&=n3&NX@3t#-c@>lnDD_Zg6Y9>=qcjI zhu>*2!h2%aM-o0N;Fl4xE|pJ5t2h~%okon8aRQX+A#g%`&+!_E{a?mu5MY%%SS`0w zJ!OFCb*OmyZt7$ij^8V^O9zT~NUOkAoB^zm4{IOSI1xrX@aZz_!M^_?h%HuuGZmQE zqsyHCm#2UKKhuXDvgJ9S8pY chaqeSQ<;-D*w=_26#F4S$zU6+z#moM7Z92-*8l(j literal 0 HcmV?d00001 diff --git a/Data Structure Implementations/LinkedList.java b/Data Structure Implementations/LinkedList.java new file mode 100644 index 00000000..4db9c8dd --- /dev/null +++ b/Data Structure Implementations/LinkedList.java @@ -0,0 +1,140 @@ +package com.Slash; // This is basically the folder our code belongs to. + +public class LinkedList { + + private Node head; // Reference variable pointing to nodes. + private Node tail; // Reference variable pointing to nodes. + private int size; // Number of elements in the linked list. + public LinkedList(){ + this.size=0; + } + + private class Node { + + private int value; + private Node next; + + public Node(int value) { + this.value = value; + } + + public Node(int value, Node next){ + this.value = value; + this.next = next; + } + } + + // Inserting a new node at the starting of the list. + public void insertFirst(int val){ + Node node = new Node(val); + node.next = head; // Assigning the newly created node to point to the previous head. + head = node; // Changing the head to the current node. + + if (tail == null) + tail = head; // If this is the only node in the list, then tail also points to this address. + + size += 1; // Increasing the size of the list coz new element added. + } + + // Inserting a new node at the end of the list. + public void insertLast(int val){ + if (tail == null) + insertFirst(val); + else { + Node node = new Node(val); + tail.next = node; + tail = node; + size += 1; + } + } + + // Inserting a new node at any given list + public void insert(int val, int index){ + if (index == 0) { + insertFirst(val); + return; + } + + if(index == size){ + insertLast(val); + return; + } + + Node temp = head; + for (int i = 1; i < index; i++) { + temp = temp.next; // Reaching before the node where we want to insert our value. + } + Node node = new Node(val, temp.next); // Pointing the new node to the next value of the temporary node. + temp.next = node; + size++; + } + + // Delete node at the first index. + public int deleteFirst(){ + int val = head.value; + head = head.next; + if(head==null){ + tail = null; + } + size -= 1; + return val; + } + + // Delete node at the last index. + public int deleteLast(){ + if(size <= 1){ + return deleteFirst(); + } + + Node secondLast = get(size - 2); + int value = tail.value; + tail = secondLast; + tail.next = null; + size -= 1; + return value; + } + + // To delete a node at given index. + public int delete(int index){ + if (index == 0) + return deleteFirst(); + if (index == size-1) + return deleteLast(); + Node prev = get(index - 1); + Node deleted = prev.next; + int val = deleted.value; + prev.next = deleted.next; + size -= 1; + return val; + } + + // To return a node that has a particular value. + public Node find(int value){ + Node node = head; + while(node != null){ + if(node.value == value) + return node; + node = node.next; + } + return null; + } + + // To return a node at a particular index. + public Node get(int index){ + Node node = head; + for (int i = 0; i < index; i++) { + node = node.next; + } + return node; + } + + // Displaying the entire linked list. + public void displayList(){ + Node temp = head; + while(temp != null) { + System.out.print(temp.value + "-->"); + temp = temp.next; + } + System.out.println("END"); + } +} From d9ba2f88418eefe36eb2fb5f57029970b76aed05 Mon Sep 17 00:00:00 2001 From: Suryansh Sinha Date: Sun, 2 Oct 2022 15:30:31 +0530 Subject: [PATCH 2/2] Added Singly Linked List implementation in Java --- Data Structure Implementations/LinkedList.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Data Structure Implementations/LinkedList.java b/Data Structure Implementations/LinkedList.java index 4db9c8dd..cda1c0e1 100644 --- a/Data Structure Implementations/LinkedList.java +++ b/Data Structure Implementations/LinkedList.java @@ -1,5 +1,3 @@ -package com.Slash; // This is basically the folder our code belongs to. - public class LinkedList { private Node head; // Reference variable pointing to nodes.