Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions JavaLearnings/src/LinkedLists/LinkedList.java
Original file line number Diff line number Diff line change
@@ -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<index;i++)
{
temp=temp.next;
}
return temp;
}

public boolean set(int index , int value)
{
Node temp = get(index);

if(temp!=null)
{
temp.value=value;
return true;
}
return false;
}

public boolean insert(int index , int value)
{
if(index<0 || index > 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;
}
}



32 changes: 32 additions & 0 deletions JavaLearnings/src/LinkedLists/Main.java
Original file line number Diff line number Diff line change
@@ -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);





}
}
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# JavaLearnings
# 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
Binary file modified out/production/JavaLearnings/OOPS/Student1.class
Binary file not shown.
Binary file modified out/production/JavaLearnings/OOPS/polyMorphism.class
Binary file not shown.