diff --git a/JavaLearnings/src/LinkedLists/LinkedList.java b/JavaLearnings/src/LinkedLists/LinkedList.java new file mode 100644 index 0000000..d071a57 --- /dev/null +++ b/JavaLearnings/src/LinkedLists/LinkedList.java @@ -0,0 +1,173 @@ +package LinkedLists; + + +import com.sun.jdi.connect.spi.TransportService; +import org.w3c.dom.Node; + +import javax.swing.*; +import java.security.PublicKey; + +public class LinkedList { + + private Node head; + private Node tail; + private int length; + + class Node{ // Nested class + + int value ; + Node next; + + Node(int value) { + this.value = value; + } + } + public LinkedList(int value) + { + Node newNode = new Node(value); + head = newNode; + tail = newNode; + length =1; + + } + + public void printList() + { + Node temp = head; + + while(temp!= null) + { + System.out.println(temp.value); + temp=temp.next; + + } + } + public void getHead() + { + System.out.println("The Head value is " + head.value); + } + public void getTail() + { + System.out.println("The Tail value is "+ tail.value); + } + + public void Append(int value) + { + Node newNode = new Node(value); + if(length==0) + { + head = newNode; + tail = newNode; + } + else { + tail.next=newNode; + tail=newNode; + } + length++; + } + + + public Node removeLast() + { + if(length==0) return null; + + Node temp = head; + Node Pre = head; + + while(temp.next!=null) + { + Pre=temp; + temp =temp.next; + + } + tail= Pre; + tail.next=null; + length--; + if(length==0) head=null; tail=null; + + return temp; + } + public void prePend(int value) + { + Node newNode = new Node(value); + + if(length==0) + {head=newNode; tail = newNode;} + else{ + newNode.next= head; + head= newNode; + } + length++; + } + + public Node removeFirst() + { + if(length==0) return null; + + Node temp = head; + head=head.next; + temp.next=null; + length--; + + if(length==0) tail=null; + + return temp; + + } + + public Node get(int index) + { + if(index<0 || index>=length) return null; + + Node temp = head; + for(int i =0;i length) return false; + + if(index==0){ prePend(value); return true;} + if(index==length) {Append(value); return true;} + + Node newNode = new Node(value); + Node temp =get(index-1); + newNode.next=temp.next; + temp.next=newNode; + length++; + return true; + + } + public Node remove(int index) + { + if(index <0 || index>length) return null; + if(index==0) return removeFirst(); + if(index==length) return removeLast(); + + Node prev = get(index-1); + Node temp= prev.next; + prev.next=temp.next; + temp.next=null; + length--; + return temp; + } + } + + + diff --git a/JavaLearnings/src/LinkedLists/Main.java b/JavaLearnings/src/LinkedLists/Main.java new file mode 100644 index 0000000..12422c9 --- /dev/null +++ b/JavaLearnings/src/LinkedLists/Main.java @@ -0,0 +1,32 @@ +package LinkedLists; + +public class Main { + public static void main(String[] args) { + LinkedList myLinkedList = new LinkedList(4); +myLinkedList.Append(5); +myLinkedList.Append(9); +myLinkedList.Append(8); +//myLinkedList.getHead(); +// myLinkedList.getTail(); + + + myLinkedList.printList(); + +// System.out.println("The removed value is "+ myLinkedList.removeLast().value); +// System.out.println("The removed value is "+ myLinkedList.removeLast().value); +// System.out.println("The removed value is "+ myLinkedList.removeLast().value); +// System.out.println("The removed value is "+ myLinkedList.removeLast().value); +// System.out.println(myLinkedList.removeLast()); + + System.out.println("The removed first value is "+ myLinkedList.removeFirst().value); + System.out.println(myLinkedList.get(0).value); + System.out.println(myLinkedList.set(0,5)); + System.out.println(myLinkedList.insert(2,9)); + System.out.println(myLinkedList.remove(2).value); + + + + + + } +} diff --git a/README.md b/README.md index e401f57..5016286 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ -# JavaLearnings \ No newline at end of file +# JavaLearnings + +This is All the learnings i have done in Java Starting from Sep 9 2024 +Hoping to keep this repo alive and make contributions till end of the year +And learn a few Data Structures + +Pull diff --git a/out/production/JavaLearnings/OOPS/Student1.class b/out/production/JavaLearnings/OOPS/Student1.class index e516adb..2faecc1 100644 Binary files a/out/production/JavaLearnings/OOPS/Student1.class and b/out/production/JavaLearnings/OOPS/Student1.class differ diff --git a/out/production/JavaLearnings/OOPS/polyMorphism.class b/out/production/JavaLearnings/OOPS/polyMorphism.class index 0c806a0..9a4ecae 100644 Binary files a/out/production/JavaLearnings/OOPS/polyMorphism.class and b/out/production/JavaLearnings/OOPS/polyMorphism.class differ