Skip to content

Added Data Structures implementation in java #694

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

Open
wants to merge 1 commit into
base: main
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
91 changes: 91 additions & 0 deletions JAVA/collection/AbstractCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;

/**
*
* @author Zohaib Hassan Soomro
*/
public abstract class AbstractCollection implements Collection {

protected AbstractCollection() {
}

@Override
public abstract boolean add(Object object);

@Override
public boolean isEmpty() {
return (size() == 0);
}

@Override
public abstract Iterator iterator();

public boolean remove(Object object) {
Iterator it = iterator();
if (object == null) {
while (it.hasNext()) {
if (it.next() == null) {
it.remove();
return true;
}
}
} else {
while (it.hasNext()) {
if (object.equals(it.next())) {
it.remove();
return true;
}
}
}
return false;
}

@Override
public abstract int size();

@Override
public boolean contains(Object object) {
Iterator it = iterator();
if (object == null) {
while (it.hasNext()) {
if (it.next() == null) {
return true;
}
}
} else {
while (it.hasNext()) {
if (object.equals(it.next())) {
return true;
}
}
}
return false;
}

@Override
public String toString() {
if (isEmpty()) {
return "[ ]";
}
Iterator it = iterator();
StringBuffer buffer = new StringBuffer("[" + it.next());
while (it.hasNext()) {
buffer.append("," + it.next());
}
return (buffer + "]");
}

@Override
public void clear() {
for (Iterator it = iterator(); it.hasNext();) {
it.next();
it.remove();
}
}

}
69 changes: 69 additions & 0 deletions JAVA/collection/ArrayCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;

/**
*
* @author Zohaib Hassan Soomro
*/
public class ArrayCollection extends AbstractCollection {

private final int INITIAL_LENGTH = 15;
private int size;
private Object[] a = new Object[INITIAL_LENGTH];

@Override
public boolean add(Object object) {
if (size == a.length) {
resizeArray();
}
a[size++] = object;
return true;
}

@Override
public int size() {
return size;
}

@Override
public Iterator iterator() {
return new Iterator() {
private int i = 0; //index of current element
private boolean okToRemove = false;

@Override
public boolean hasNext() {
return (i < size);
}

@Override
public Object next() {
if (i == size) {
throw new RuntimeException();
}
okToRemove = true;
return a[i++];
}

@Override
public void remove() {
if (!okToRemove) {
throw new IllegalStateException();
}
a[--i] = a[--size];
okToRemove = false;
}
};
}

public void resizeArray() {
Object[] aa = a;
a = new Object[2 * aa.length];
System.arraycopy(aa, 0, a, 0, size);
}

}
20 changes: 20 additions & 0 deletions JAVA/collection/Collection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;

/**
*
* @author Zohaib Hassan Soomro
*/
public interface Collection {
public boolean add(Object object);
public void clear();
public boolean contains(Object object);
public boolean isEmpty();
public Iterator iterator();
public boolean remove(Object object);
public int size();
}
44 changes: 44 additions & 0 deletions JAVA/collection/Country.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;

/**
*
* @author Zohaib Hassan Soomro
*/
public class Country {

private String name, language, iso;
private int area, population;

public Country(String name, String iso, String language, int area, int population) {
this.name = name;
this.language = language;
this.iso = iso;
this.area = area;
this.population = population;
}

@Override
public String toString() {
return ("[" + name + "," + iso + "," + language + "," + area + "," + population + "]");
}

public String getValue(String key) {
//mechanism to find the country with this key
return ("[" + name + "," + iso + "," + language + "," + area + "," + population + "]");
}

public static void main(String[] args) {
Country pak = new Country("Pakistan", "PK", "Urdu", 333333, 3333333);
Country bang = new Country("Bangladesh", "BN", "Bengali", 4444, 44444);
Country ind = new Country("India", "IN", "Hindi", 555555, 555555);

System.out.println("Pakistan: " + pak);
System.out.println("Bangladesh: " + bang);
System.out.println("India: " + ind);
}
}
16 changes: 16 additions & 0 deletions JAVA/collection/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;

/**
*
* @author Zohaib Hassan Soomro
*/
public interface Iterator {
public boolean hasNext();
public Object next();
public void remove();
}
83 changes: 83 additions & 0 deletions JAVA/collection/LinkedCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;

/**
*
* @author Zohaib Hassan Soomro
*/
public class LinkedCollection extends AbstractCollection {

private int size;
private Node head = new Node();

@Override
public boolean add(Object object) {
head.previous = head.previous.next = new Node(object, head.previous, head);
size++;
return true;
}

class Node {

private Object object;
private Node previous, next;

public Node() {
previous = this;
next = this;
}

public Node(Object object, Node previous, Node next) {
this.object = object;
this.previous = previous;
this.next = next;
}
}

@Override

public Iterator iterator() {

return new Iterator() {
private boolean okToRemove = false;
Node cursor = head.next;

@Override
public boolean hasNext() {
return (cursor != head);
}

@Override
public Object next() {
if (cursor == head) {
throw new RuntimeException();
}
okToRemove = true;
Object obj = cursor.object;
cursor = cursor.next;
return obj;
}

@Override
public void remove() {
if (!okToRemove) {
throw new IllegalStateException();
}
cursor.previous = cursor.previous.previous;
cursor.previous.next = cursor;
size--;
okToRemove = false;
}
};
}

@Override
public int size() {
return size;
}

}
41 changes: 41 additions & 0 deletions JAVA/collection/TestMyCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collection;

/**
*
* @author Zohaib Hassan Soomro
*/
public class TestMyCollection {

public static void main(String[] args) {
ArrayCollection collection = new ArrayCollection();
System.out.println("ArrayCollection:");

collection.add("Hello!");
collection.add("boyz");
collection.add("and");
collection.add("girlz");
System.out.println(collection);
collection.remove("and");
System.out.println(collection);

System.out.println("\n\nLinkedCollection:");
LinkedCollection collection2 = new LinkedCollection();
collection2.add("Hello!");
collection2.add("boyz");
collection2.add("and");
collection2.add("girlz");
System.out.println(collection2);
collection2.remove("and");
System.out.println(collection2);

System.out.println("Contains: "+collection.contains("boyz"));
System.out.println("Contains: "+collection2.contains("and"));

}

}
Loading