Skip to content

Add [Queue,Stack,List][Java] #1

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: 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
6 changes: 6 additions & 0 deletions OrderedList/ArrayListImplementation/BoundsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BoundsException extends Exception {
void Error() {
printStackTrace();
System.out.println("Out of bounds exception occured!");
}
}
49 changes: 49 additions & 0 deletions OrderedList/ArrayListImplementation/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.Scanner;

class Driver {
public static void main(String arg[]) {
Scanner in = new Scanner(System.in);
OrderedList dataStructure = new OrderedList();
int choice, userInput;
Boolean Continue = true;
System.out.println("MENU DRIVEN ORDEREDLIST");
while (Continue) {
System.out.print(
"\n1) Enter new Data Element\n2) Search for an Element\n3) Traverse the whole array\n4) Find element by position\n5) Check if array is empty\n6) Exit\n\n Your CHOICE: ");
choice = in.nextInt();
switch (choice) {
case 1:
System.out.print("Enter element to add: ");
userInput = in.nextInt();
dataStructure.add(userInput);
break;
case 2:
System.out.print("Enter element to Search: ");
userInput = in.nextInt();
if (!dataStructure.search(userInput))
System.out.println("Element " + userInput + " not found!");
break;
case 3:
dataStructure.traverse();
break;
case 4:
System.out.print("Enter index to get Element: ");
userInput = in.nextInt();
System.out.println("Element at index " + userInput + " is " + dataStructure.getElementAt(userInput));
break;
case 5:
if (dataStructure.isEmpty())
System.out.println("DataStructure is empty!");
else
System.out.println("DataStructure is not empty!");
break;
case 6:
Continue = false;
break;
default:
System.out.println("Invalid Value! Enter 6 to exit!!");
}
}
in.close();
}
}
8 changes: 8 additions & 0 deletions OrderedList/ArrayListImplementation/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface List{
Integer getElementAt(Integer position);
void add(Integer element);
void traverse();
Boolean isEmpty();
Boolean search(Integer element);
Integer getSize();
}
82 changes: 82 additions & 0 deletions OrderedList/ArrayListImplementation/OrderedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
public class OrderedList implements List {
Integer arr[], size, initialSize;

OrderedList() {
initialSize = 5;
size = 0;
arr = new Integer[initialSize];
}

public void add(Integer element) {
if (size == initialSize) {
Integer tempArr[] = new Integer[initialSize *= 2];
for (int i = 0; i < size; i++) {
tempArr[i] = arr[i];
}
arr = tempArr;
}
if (size == 0) {
arr[size] = element;
} else {
for (int i = size; i >= 0; i--) {
if (arr[i - 1] > element) {
arr[i] = arr[i - 1];
if (i - 2 < 0) {
arr[i - 1] = element;
break;
}
} else {
arr[i] = element;
break;
}
}
}
size++;
traverse();
}

public Boolean isEmpty() {
return size == 0 ? true : false;
}

public void traverse() {
System.out.print("Datastructure contents: ");
for (Integer i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}

public Boolean search(Integer element) {
Integer low = 0, high = size - 1, mid;
Boolean elementFound = false;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == element) {
System.out.println("Found " + element + " at index " + mid);
elementFound = true;
break;
} else if (arr[mid] > element) {
high = mid-1;
} else {
low = mid+1;
}
}
return elementFound;
}

public Integer getElementAt(Integer position) {
try {
if (position >= size || position < 0)
throw new BoundsException();
} catch (BoundsException exception) {
exception.Error();
}
return arr[position];
}

public Integer getSize() {
return size;
}

}
6 changes: 6 additions & 0 deletions OrderedList/LinearListImplementation/BoundsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class BoundsException extends Exception {
void Error() {
printStackTrace();
System.out.println("Out of bounds exception occured!");
}
}
48 changes: 48 additions & 0 deletions OrderedList/LinearListImplementation/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.Scanner;

class Driver {
public static void main(String arg[]) {
Scanner in = new Scanner(System.in);
OrderedLinkedList dataStructure = new OrderedLinkedList();
int choice, userInput;
Boolean Continue = true;
System.out.println("MENU DRIVEN ORDEREDLIST");
while (Continue) {
System.out.print(
"\n1) Enter new Data Element\n2) Search for an Element\n3) Traverse the whole array\n4) Find element by position\n5) Check if array is empty\n6) Exit\n\n Your CHOICE: ");
choice = in.nextInt();
switch (choice) {
case 1:
System.out.print("Enter element to add: ");
userInput = in.nextInt();
dataStructure.add(userInput);
break;
case 2:
System.out.print("Enter element to Search: ");
userInput = in.nextInt();
dataStructure.search(userInput);
break;
case 3:
dataStructure.traverse();
break;
case 4:
System.out.print("Enter index to get Element: ");
userInput = in.nextInt();
System.out.println("Element at index " + userInput + " is " + dataStructure.getElementAt(userInput));
break;
case 5:
if (dataStructure.isEmpty())
System.out.println("DataStructure is empty!");
else
System.out.println("DataStructure is not empty!");
break;
case 6:
Continue = false;
break;
default:
System.out.println("Invalid Value! Enter 6 to exit!!");
}
}
in.close();
}
}
8 changes: 8 additions & 0 deletions OrderedList/LinearListImplementation/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface List{
Integer getElementAt(Integer position);
void add(Integer element);
void traverse();
Boolean isEmpty();
void search(Integer element);
Integer getSize();
}
14 changes: 14 additions & 0 deletions OrderedList/LinearListImplementation/ListObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ListObject {
Integer data;
ListObject next;

ListObject() {
data = null;
next = null;
}

ListObject(Integer element) {
data = element;
next = null;
}
}
75 changes: 75 additions & 0 deletions OrderedList/LinearListImplementation/OrderedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class OrderedLinkedList implements List {
ListObject Head, iterator;

OrderedLinkedList() {
Head = null;
}

public Integer getElementAt(Integer position) {
return null;
}

public void add(Integer element) {
ListObject obj = new ListObject(element);
if (Head == null) {
Head = obj;
} else {
iterator = Head;
ListObject previous = new ListObject();
previous.next = Head;
while (iterator != null) {
if (iterator.data > element) {
previous.next = obj;
obj.next = iterator;
return;
}
iterator = iterator.next;
previous = previous.next;
}
previous.next = obj;
}
traverse();
}

public void traverse() {
iterator = Head;
System.out.print("DataStructure cotains: ");
while (iterator != null) {
System.out.print(iterator.data + " ");
iterator = iterator.next;
}
System.out.println();
}

public Boolean isEmpty() {
return getSize() == 0;
}

public void search(Integer element) {
iterator = Head;
boolean found = false;
Integer index = 0;
while (iterator != null) {
if (iterator.data == element) {
System.out.println("Element " + element + " found at position " + index);
found = true;
break;
}
index++;
iterator = iterator.next;
}
if (found == false)
System.out.println("Element " + element + " not found!");
}

public Integer getSize() {
Integer size = 0;
iterator = Head;
while (iterator != null) {
size++;
iterator = iterator.next;
}
return size;
}

}
8 changes: 8 additions & 0 deletions Queue/ArrayList Implementation/BoundsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class BoundsException extends Exception {
private static final long serialVersionUID = 1L;

void Error() {
printStackTrace();
System.out.println("Out of bounds Exception occured!");
}
}
45 changes: 45 additions & 0 deletions Queue/ArrayList Implementation/Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Scanner;

class Driver {
public static void main(String arg[]) {
Scanner in = new Scanner(System.in);
QueueAL dataStructure = new QueueAL();
int choice, userInput;
Boolean Continue = true;
System.out.println("MENU DRIVEN QUEUE");
while (Continue) {
System.out.print(
"\n1) Enqueue\n2) Dequeue\n3) Traverse\n4) Get Size\n5) Check if empty\n6) Exit\n\n Your CHOICE: ");
choice = in.nextInt();
switch (choice) {
case 1:
System.out.print("Enter element to add: ");
userInput = in.nextInt();
dataStructure.enqueue(userInput);
break;
case 2:
System.out.println("Removed element: " + dataStructure.dequeue());
dataStructure.traverse();
break;
case 3:
dataStructure.traverse();
break;
case 4:
System.out.println("Size of data structure: " + dataStructure.getSize());
break;
case 5:
if (dataStructure.isEmpty())
System.out.println("DataStructure is empty!");
else
System.out.println("DataStructure is not empty!");
break;
case 6:
Continue = false;
break;
default:
System.out.println("Invalid Value! Enter 6 to exit!!");
}
}
in.close();
}
}
7 changes: 7 additions & 0 deletions Queue/ArrayList Implementation/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface List{
void enqueue(Integer element);
Integer dequeue();
void traverse();
Integer getSize();
boolean isEmpty();
}
Loading