Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linked list #278

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions JAVA/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry excluding="merge_sort/src/|quick_sort/src/" kind="src" path=""/>
<classpathentry kind="src" path="merge_sort/src"/>
<classpathentry kind="src" path="quick_sort/src"/>
<classpathentry kind="output" path="merge_sort/out/production/merge_sort"/>
</classpath>
17 changes: 17 additions & 0 deletions JAVA/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JAVA</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
114 changes: 114 additions & 0 deletions JAVA/Linked_List/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package Linked_List;

public class LinkedList {
//intitializing head to null
Node head = null;
//declaring a runner pointer variable
Node runner;
//declaring size variable
int size = 0;

public void add(int data) {
Node n = new Node();
n.data = data;
if(head == null) {
head = n;
}
else {
runner = head;
//traverse to end
while(runner.next != null) {
runner = runner.next;
}
runner.next = n;
}
size++;
}
public void insertAtBegin(int data) {
Node n = new Node();
n.data = data;
n.next = head;
head = n;
size++;
}
public void inserAt(int data, int pos) {
Node n = new Node();
n.data = data;
if(pos < 0 || pos > size) {
throw new IllegalArgumentException();
}
else {
runner = head;
//traverse at 1 before pos
for(int i=0; i<pos-1; i++) {
runner = runner.next;
}
n.next = runner.next;
runner.next = n;
}
size++;
}

public void deleteAt(int pos) {
if(pos == 0) {
head = head.next;
}
else {
runner = head;
//traverse to 1 before pos
for(int i=0; i<pos-1; i++) {
runner = runner.next;
}
runner.next = runner.next.next;
}
size--;
}

public int get(int pos) {
runner = head;
for(int i=0; i<=pos; i++) {
if(i == pos) {
return runner.data;
}
runner = runner.next;
}
return -1;
}

public void print() {
runner = head;
while(runner != null) {
System.out.println(runner.data);
runner = runner.next;
}
}
public boolean contains(int data) {
if(head == null) {
return false;
}
runner = head;
while(runner != null) {
if(runner.data == data) {
return true;
}
runner = runner.next;
}
return false;
}
public void clear() {
head = null;
size = 0;
}
public int size() {
return size;
}
public boolean isEmpty() {
if(head == null) {
return true;
}
else {
return false;
}
}

}
6 changes: 6 additions & 0 deletions JAVA/Linked_List/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package Linked_List;

public class Node {
int data;
Node next;
}
37 changes: 37 additions & 0 deletions JAVA/Linked_List/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Linked_List;


public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList s1 = new LinkedList();

System.out.println(s1.isEmpty());
s1.add(10);
System.out.println();
s1.print();
s1.add(20);
s1.add(30);
s1.print();
System.out.println();
System.out.println(s1.isEmpty());
s1.insertAtBegin(100);
System.out.println();
s1.print();
s1.inserAt(200, 2);
System.out.println();
s1.print();
s1.deleteAt(3);
System.out.println();
s1.print();
System.out.println();
System.out.println(s1.get(2));
System.out.println();
System.out.println(s1.contains(2000));



}

}
24 changes: 24 additions & 0 deletions JAVA/merge_sort/out/production/merge_sort/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/.classpath
/.project
/AreaOfPi.class
/DiameterOfBinaryTree.class
/DiamondPattern.class
/DoubleLLImplementation$Node.class
/DoubleLLImplementation.class
/FibonacciSeries.class
/FindPalindrome.class
/LifeGame.class
/LinearSearch.class
/Paln.class
/Program to calculate power of a number using for loop
/ReverseInteger.class
/ReverseString.class
/SwapNumbers.class
/TowerOfHanoi.class
/TreeNode.class
/VowelConsonant.class
/com/
/floyd.class
/merge_sort/
/quick_sort/
/Linked_List/
Binary file not shown.