chore: remove unnecessary files
parent
e9a1aedfcd
commit
e27ca2ba39
|
@ -1,69 +0,0 @@
|
|||
class CircularLinkedList{
|
||||
class Node {
|
||||
int value;
|
||||
Node next;
|
||||
|
||||
public Node(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private Node head = null;
|
||||
private Node tail = null;
|
||||
|
||||
public void addNode(int value) {
|
||||
Node newNode = new Node(value);
|
||||
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
} else {
|
||||
tail.next = newNode;
|
||||
}
|
||||
|
||||
tail = newNode;
|
||||
tail.next = head;
|
||||
}
|
||||
public void printNodes() {
|
||||
Node current = head;
|
||||
if(head == null) {
|
||||
System.out.println("List is empty");
|
||||
return;
|
||||
}
|
||||
System.out.println("The circular linked list is: ");
|
||||
System.out.print("[ ");
|
||||
do{
|
||||
System.out.print(current.value + " ");
|
||||
current = current.next;
|
||||
//go to next until we get back to the head
|
||||
}while(current != head);
|
||||
System.out.println("]");
|
||||
}
|
||||
}
|
||||
|
||||
public class circular {
|
||||
public static void main(String [] args){
|
||||
CircularLinkedList cll = new CircularLinkedList();
|
||||
|
||||
cll.addNode(10);
|
||||
cll.addNode(12);
|
||||
cll.addNode(2);
|
||||
cll.addNode(1);
|
||||
cll.addNode(8);
|
||||
cll.addNode(42);
|
||||
cll.addNode(17);
|
||||
|
||||
cll.printNodes();
|
||||
|
||||
//this will print :
|
||||
/*
|
||||
The circular linked list is:
|
||||
[ 10 12 2 1 8 42 17 ]
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
to run the file:
|
||||
javac circular.java
|
||||
java circular
|
||||
*/
|
|
@ -1,125 +0,0 @@
|
|||
// Java program to clone a linked list with random pointers
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
// Linked List Node class
|
||||
class Node
|
||||
{
|
||||
int data;//Node data
|
||||
Node next, random;//Next and random reference
|
||||
|
||||
//Node constructor
|
||||
public Node(int data)
|
||||
{
|
||||
this.data = data;
|
||||
this.next = this.random = null;
|
||||
}
|
||||
}
|
||||
|
||||
// linked list class
|
||||
class LinkedList
|
||||
{
|
||||
Node head;//Linked list head reference
|
||||
|
||||
// Linked list constructor
|
||||
public LinkedList(Node head)
|
||||
{
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
// push method to put data always at the head
|
||||
// in the linked list.
|
||||
public void push(int data)
|
||||
{
|
||||
Node node = new Node(data);
|
||||
node.next = this.head;
|
||||
this.head = node;
|
||||
}
|
||||
|
||||
// Method to print the list.
|
||||
void print()
|
||||
{
|
||||
Node temp = head;
|
||||
while (temp != null)
|
||||
{
|
||||
Node random = temp.random;
|
||||
int randomData = (random != null)? random.data: -1;
|
||||
System.out.println("Data = " + temp.data +
|
||||
", Random data = "+ randomData);
|
||||
temp = temp.next;
|
||||
}
|
||||
}
|
||||
|
||||
// Actual clone method which returns head
|
||||
// reference of cloned linked list.
|
||||
public LinkedList clone()
|
||||
{
|
||||
// Initialize two references, one with original
|
||||
// list's head.
|
||||
Node origCurr = this.head, cloneCurr = null;
|
||||
|
||||
// Hash map which contains node to node mapping of
|
||||
// original and clone linked list.
|
||||
Map<Node, Node> map = new HashMap<Node, Node>();
|
||||
|
||||
// Traverse the original list and make a copy of that
|
||||
// in the clone linked list.
|
||||
while (origCurr != null)
|
||||
{
|
||||
cloneCurr = new Node(origCurr.data);
|
||||
map.put(origCurr, cloneCurr);
|
||||
origCurr = origCurr.next;
|
||||
}
|
||||
|
||||
// Adjusting the original list reference again.
|
||||
origCurr = this.head;
|
||||
|
||||
// Traversal of original list again to adjust the next
|
||||
// and random references of clone list using hash map.
|
||||
while (origCurr != null)
|
||||
{
|
||||
cloneCurr = map.get(origCurr);
|
||||
cloneCurr.next = map.get(origCurr.next);
|
||||
cloneCurr.random = map.get(origCurr.random);
|
||||
origCurr = origCurr.next;
|
||||
}
|
||||
|
||||
//return the head reference of the clone list.
|
||||
return new LinkedList(map.get(this.head));
|
||||
}
|
||||
}
|
||||
|
||||
// Driver Class
|
||||
class Main
|
||||
{
|
||||
// Main method.
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// Pushing data in the linked list.
|
||||
LinkedList list = new LinkedList(new Node(5));
|
||||
list.push(4);
|
||||
list.push(3);
|
||||
list.push(2);
|
||||
list.push(1);
|
||||
|
||||
// Setting up random references.
|
||||
list.head.random = list.head.next.next;
|
||||
list.head.next.random =
|
||||
list.head.next.next.next;
|
||||
list.head.next.next.random =
|
||||
list.head.next.next.next.next;
|
||||
list.head.next.next.next.random =
|
||||
list.head.next.next.next.next.next;
|
||||
list.head.next.next.next.next.random =
|
||||
list.head.next;
|
||||
|
||||
// Making a clone of the original linked list.
|
||||
LinkedList clone = list.clone();
|
||||
|
||||
// Print the original and cloned linked list.
|
||||
System.out.println("Original linked list");
|
||||
list.print();
|
||||
System.out.println("\nCloned linked list");
|
||||
clone.print();
|
||||
}
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
class DoublyLinkedList {
|
||||
class Node{
|
||||
int item;
|
||||
Node previous;
|
||||
Node next;
|
||||
|
||||
public Node(int item) {
|
||||
this.item = item;
|
||||
}
|
||||
}
|
||||
|
||||
Node head, tail = null;
|
||||
|
||||
public void addNode(int item) {
|
||||
Node newNode = new Node(item);
|
||||
|
||||
//if list is empty, head and tail points to newNode
|
||||
if(head == null) {
|
||||
head = tail = newNode;
|
||||
//head's previous will be null
|
||||
head.previous = null;
|
||||
//tail's next will be null
|
||||
tail.next = null;
|
||||
}
|
||||
else {
|
||||
//add newNode to the end of list. tail->next set to newNode
|
||||
tail.next = newNode;
|
||||
//newNode->previous set to tail
|
||||
newNode.previous = tail;
|
||||
//newNode becomes new tail
|
||||
tail = newNode;
|
||||
//tail's next point to null
|
||||
tail.next = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void printNodes() {
|
||||
Node current = head;
|
||||
if(head == null) {
|
||||
System.out.println("Doubly linked list is empty");
|
||||
return;
|
||||
}
|
||||
System.out.println("The doubly linked list is: ");
|
||||
System.out.print("[ ");
|
||||
while(current != null) {
|
||||
System.out.print(current.item + " ");
|
||||
//go to next
|
||||
current = current.next;
|
||||
}
|
||||
System.out.print("]");
|
||||
}
|
||||
}
|
||||
|
||||
public class doubly {
|
||||
public static void main(String [] args){
|
||||
DoublyLinkedList dl_List = new DoublyLinkedList();
|
||||
|
||||
dl_List.addNode(5);
|
||||
dl_List.addNode(7);
|
||||
dl_List.addNode(1);
|
||||
dl_List.addNode(17);
|
||||
dl_List.addNode(27);
|
||||
|
||||
dl_List.printNodes();
|
||||
//this will print :
|
||||
/*
|
||||
The doubly linked list is:
|
||||
[ 5 7 1 17 27 ]
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
to run the file:
|
||||
javac doubly.java
|
||||
java doubly
|
||||
*/
|
|
@ -1,78 +0,0 @@
|
|||
class LinkedList{
|
||||
class Node{
|
||||
int item;
|
||||
Node next;
|
||||
|
||||
public Node(int item) {
|
||||
this.item = item;
|
||||
}
|
||||
}
|
||||
|
||||
Node head = null;
|
||||
|
||||
public void addNode(int item){
|
||||
Node newElem = new Node(item);
|
||||
|
||||
if(this.head != null){
|
||||
newElem.next = this.head;
|
||||
}
|
||||
|
||||
this.head = newElem;
|
||||
}
|
||||
|
||||
public void reverse(){
|
||||
Node prev = null;
|
||||
Node current = this.head;
|
||||
Node next = null;
|
||||
|
||||
while (current != null) {
|
||||
next = current.next;
|
||||
current.next = prev;
|
||||
prev = current;
|
||||
current = next;
|
||||
}
|
||||
this.head =prev;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
Node current = head;
|
||||
if(head == null) return;
|
||||
System.out.print("[ ");
|
||||
while(current != null) {
|
||||
System.out.print(current.item + " ");
|
||||
current = current.next;
|
||||
}
|
||||
System.out.println("]");
|
||||
}
|
||||
}
|
||||
|
||||
public class reverse{
|
||||
|
||||
public static void main(String [] args){
|
||||
LinkedList list = new LinkedList();
|
||||
|
||||
list.addNode(2);
|
||||
list.addNode(7);
|
||||
list.addNode(1);
|
||||
list.addNode(8);
|
||||
list.addNode(5);
|
||||
|
||||
System.out.print("before : \t");
|
||||
list.print();
|
||||
|
||||
list.reverse();
|
||||
|
||||
System.out.print("after reverse : ");
|
||||
list.print();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
to run the file :
|
||||
javac reverse.java
|
||||
java reverse
|
||||
|
||||
results :
|
||||
before : [ 5 8 1 7 2 ]
|
||||
after reverse : [ 2 7 1 8 5 ]
|
||||
*/
|
|
@ -1,64 +0,0 @@
|
|||
class Node<T> {
|
||||
T data;
|
||||
Node<T> next;
|
||||
|
||||
Node(T data) {
|
||||
this.data = data;
|
||||
this.next = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class SinglyLinkedList<T> {
|
||||
Node<T> head;
|
||||
|
||||
SinglyLinkedList() {
|
||||
this.head = null;
|
||||
}
|
||||
|
||||
void insertAtHead(T data) {
|
||||
Node<T> newNode = new Node<T>(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<T> 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<String> l = new SinglyLinkedList<String>();
|
||||
l.insertAtHead("abc");
|
||||
l.insertAtHead("pqr");
|
||||
l.insertAtHead("xyz");
|
||||
l.removeAtHead();
|
||||
l.printList();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue