forked from Hithomelabs/Princeton1
Mod 4 all exercises
This commit is contained in:
parent
38de5fe0a5
commit
60e8425dd9
1
.idea/.name
generated
Normal file
1
.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
Pricenton1
|
9
.idea/Princeton1.iml
generated
Normal file
9
.idea/Princeton1.iml
generated
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
3
.idea/gradle.xml
generated
3
.idea/gradle.xml
generated
@ -4,6 +4,8 @@
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
<option name="delegatedBuild" value="false" />
|
||||
<option name="testRunner" value="PLATFORM" />
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
@ -14,6 +16,7 @@
|
||||
<option value="$PROJECT_DIR$/module5" />
|
||||
<option value="$PROJECT_DIR$/module6" />
|
||||
<option value="$PROJECT_DIR$/module7" />
|
||||
<option value="$PROJECT_DIR$/module8" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
|
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" filepath="$PROJECT_DIR$/.idea/modules/module4/Pricenton1.module4.test.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -2,102 +2,65 @@ package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
public class ArrayQueue<Item> extends Queue<Item> {
|
||||
|
||||
public class ArrayQueue<E> extends Queue<E>{
|
||||
|
||||
// insertion from tail, removal from head
|
||||
public static final int DEFAULT_CAPACITY = 10;
|
||||
private int capacity;
|
||||
private int tail;
|
||||
private int head;
|
||||
private int size;
|
||||
private E[] arr;
|
||||
|
||||
public ArrayQueue(){
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
public ArrayQueue(int capacity){
|
||||
this.capacity = capacity;
|
||||
arr = (E[]) new Object[this.capacity];
|
||||
size = 0;
|
||||
}
|
||||
private int capacity=3;
|
||||
private Item[] arr = (Item[]) new Object[capacity];
|
||||
private int head=0,tail=0;
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
return (tail - head) == 0;
|
||||
}
|
||||
|
||||
private void resize(int newCapacity){
|
||||
Item[] temp = arr;
|
||||
arr = (Item[]) new Object[newCapacity];
|
||||
int iNew=0,iOld=head;
|
||||
while(iOld<tail){
|
||||
arr[iNew]=temp[iOld];
|
||||
iNew++;
|
||||
iOld++;
|
||||
}
|
||||
|
||||
capacity=newCapacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E dequeue() {
|
||||
public Item dequeue() {
|
||||
if(isEmpty())
|
||||
return null;
|
||||
else{
|
||||
E element = arr[head];
|
||||
// Garbage collection
|
||||
arr[head] = null;
|
||||
head = (head+1)%capacity;
|
||||
size = size - 1;
|
||||
if(capacity >= 40 && size < capacity/4){
|
||||
capacity = capacity/2;
|
||||
resize(capacity, capacity*2);
|
||||
}
|
||||
// System.out.println(head+" "+arr[head]);
|
||||
Item element = arr[head++];
|
||||
if(size() < capacity/4)
|
||||
resize(capacity/2);
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enqueue(E element) {
|
||||
// We plan capacity expansion if needed
|
||||
if (size == capacity){
|
||||
capacity = capacity * 2;
|
||||
resize(capacity, capacity/2);
|
||||
}
|
||||
arr[tail] = element;
|
||||
tail = (tail + 1) % capacity;
|
||||
++size;
|
||||
}
|
||||
|
||||
// When resize takes place always the original array is full, so copy the complete array as is
|
||||
private void resize(int capacity, int oldCapacity) {
|
||||
E[] resizedArr = (E[]) new Object[capacity];
|
||||
for(int i = 0; i < size; i++) {
|
||||
resizedArr[i] = arr[head];
|
||||
// halving because capacity has now doubled
|
||||
arr[head] = null;
|
||||
head = (head + 1) % oldCapacity;
|
||||
}
|
||||
arr = resizedArr;
|
||||
// When resizing takes place, we bring the head to 0 and the tail to size
|
||||
// tail is where new inserts will be made and head will be where elements will be removed
|
||||
tail = size;
|
||||
head = 0;
|
||||
|
||||
public void enqueue(Item element) {
|
||||
if(tail == capacity)
|
||||
resize(capacity * 2);
|
||||
arr[tail++] = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
return (tail - head);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
int counter = size;
|
||||
int pointer = head;
|
||||
public Iterator<Item> iterator() {
|
||||
return new Iterator<Item>() {
|
||||
int current = head;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return counter != 0;
|
||||
return current != tail;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
E element = arr[pointer];
|
||||
pointer = (pointer + 1)% capacity;
|
||||
--counter;
|
||||
return element;
|
||||
public Item next() {
|
||||
return arr[current++];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,98 +1,109 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import java.util.Iterator;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
// Concrete implementation of stack using arrays
|
||||
// Creating a generic stack of type E
|
||||
public class ArrayStack<E> extends Stack<E> {
|
||||
public class ArrayStack<Item> extends Stack<Item>{
|
||||
|
||||
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10
|
||||
// every time size == capacity, capacity = 2 * capacity
|
||||
private static final int DEFAULT_CAPACITY = 10;
|
||||
private int capacity;
|
||||
private int size;
|
||||
private E[] arr;
|
||||
private int capacity=3;
|
||||
private int top=0;
|
||||
private Item[] arr = (Item[]) new Object[capacity];
|
||||
|
||||
public ArrayStack(int capacity){
|
||||
this.capacity = capacity;
|
||||
arr = (E[]) new Object[this.capacity];
|
||||
}
|
||||
|
||||
// Constructor chaining, default constructor will call parametrized constructor with default initial capacity 10
|
||||
public ArrayStack(){
|
||||
this(DEFAULT_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
return top == 0;
|
||||
}
|
||||
|
||||
private void changeCapacity(int newCapacity){
|
||||
E[] resizedArr = (E[]) new Object[newCapacity];
|
||||
for (int i = 0; i < size; i++)
|
||||
resizedArr[i] = arr[i];
|
||||
arr = resizedArr;
|
||||
}
|
||||
private void resize(int newCapacity){
|
||||
Item[] temp = arr;
|
||||
arr = (Item[]) new Object[newCapacity];
|
||||
|
||||
private void incrementSize(){
|
||||
if (size == capacity){
|
||||
capacity = 2 * capacity;
|
||||
changeCapacity(capacity);
|
||||
for(int i=0; i<top; i++){
|
||||
arr[i]=temp[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Push always happens at the end of the stack
|
||||
// Say the size of the stack is 1, new element gets inserted at 1
|
||||
@Override
|
||||
public void push(E element) {
|
||||
// Lazy approach, we assume size to always be lesser than capacity
|
||||
incrementSize();
|
||||
arr[size++] = element;
|
||||
capacity=newCapacity;
|
||||
// System.out.println("New Capacity: "+capacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() {
|
||||
if (isEmpty())
|
||||
public void push(Item element) {
|
||||
if(top == capacity)
|
||||
resize(capacity * 2);
|
||||
arr[top++] = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item pop() {
|
||||
|
||||
//System.out.println(top);
|
||||
|
||||
if(top == 0)
|
||||
//throw new java.util.NoSuchElementException();
|
||||
return null;
|
||||
else{
|
||||
E e = arr[--size];
|
||||
arr[size] = null;
|
||||
checkResize();
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
private void checkResize() {
|
||||
if (size < capacity / 4 && capacity >= 20){
|
||||
capacity = capacity / 2;
|
||||
changeCapacity(capacity);
|
||||
}
|
||||
Item element = arr[--top];
|
||||
arr[top] = null;
|
||||
|
||||
if(top < capacity/4)
|
||||
resize(capacity/2);
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
return top;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
|
||||
int current = 0;
|
||||
|
||||
public Iterator<Item> iterator() {
|
||||
return new Iterator<Item>() {
|
||||
private int current=0;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return current != size;
|
||||
return current<top;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
E element = arr[current];
|
||||
current = current + 1;
|
||||
return element;
|
||||
public Item next() {
|
||||
return arr[current++];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
Stack<Integer> s = new ArrayStack<>();
|
||||
|
||||
try{
|
||||
s.push(0);
|
||||
s.push(1);
|
||||
s.push(2);
|
||||
s.push(3);
|
||||
s.push(4);
|
||||
s.push(5);
|
||||
s.push(6);
|
||||
s.push(7);
|
||||
|
||||
for(Integer i: s)
|
||||
System.out.println(i);
|
||||
|
||||
s.pop();
|
||||
s.pop();
|
||||
s.pop();
|
||||
s.pop();
|
||||
s.pop();
|
||||
s.pop();
|
||||
s.pop();
|
||||
s.pop();
|
||||
// s.pop();
|
||||
|
||||
for(Integer i: s)
|
||||
System.out.println(i);
|
||||
|
||||
} catch (java.util.NoSuchElementException e) {
|
||||
System.out.println("Cannot pop an empty stack");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,136 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Deque<Item> implements Iterable<Item> {
|
||||
|
||||
private Node head;
|
||||
private Node tail;
|
||||
private int capacity;
|
||||
|
||||
private class Node{
|
||||
Item value;
|
||||
Node next;
|
||||
}
|
||||
|
||||
public Deque(){
|
||||
this.head = null;
|
||||
this.tail = null;
|
||||
this.capacity = 0;
|
||||
}
|
||||
|
||||
public boolean isEmpty(){
|
||||
return this.capacity == 0;
|
||||
}
|
||||
|
||||
public int size(){
|
||||
return this.capacity;
|
||||
}
|
||||
|
||||
|
||||
public void addFirst(Item item){
|
||||
Node temp=this.head;
|
||||
this.head=new Node();
|
||||
this.head.value = item;
|
||||
this.head.next = temp;
|
||||
// System.out.println(this.isEmpty());
|
||||
if(this.isEmpty())
|
||||
this.tail=this.head;
|
||||
this.capacity++;
|
||||
}
|
||||
|
||||
public void addLast(Item item){
|
||||
Node temp = new Node();
|
||||
temp.value = item;
|
||||
System.out.println(this.isEmpty());
|
||||
if(!this.isEmpty()) {
|
||||
this.tail.next = temp;
|
||||
this.tail = this.tail.next;
|
||||
}
|
||||
else{
|
||||
this.head = temp;
|
||||
this.tail = temp;
|
||||
}
|
||||
this.capacity++;
|
||||
}
|
||||
|
||||
public Item removeFirst(){
|
||||
Item item = this.head.value;
|
||||
this.head=this.head.next;
|
||||
this.capacity--;
|
||||
return item;
|
||||
}
|
||||
|
||||
public Item removeLast(){
|
||||
Item item = this.tail.value;
|
||||
Node temp = this.head;
|
||||
|
||||
if(capacity <= 1){
|
||||
this.tail=null;
|
||||
this.head=null;
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
while(temp.next.next != null){
|
||||
temp=temp.next;
|
||||
}
|
||||
|
||||
this.tail=temp;
|
||||
this.tail.next=null;
|
||||
this.capacity--;
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Item> iterator() {
|
||||
|
||||
return new Iterator<Item>() {
|
||||
private Node current=head;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return this.current != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item next() {
|
||||
Item item = this.current.value;
|
||||
this.current=this.current.next;
|
||||
return item;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
Deque<String> deque = new Deque<String>();
|
||||
|
||||
deque.addLast("qwe");
|
||||
deque.addFirst("asd");
|
||||
deque.addLast("zxc");
|
||||
|
||||
for(String s: deque){
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
deque.addLast("qwess");
|
||||
|
||||
System.out.println();
|
||||
|
||||
for(String s: deque){
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
deque.removeFirst();
|
||||
deque.removeLast();
|
||||
deque.removeFirst();
|
||||
deque.removeLast();
|
||||
|
||||
System.out.println();
|
||||
|
||||
for(String s: deque){
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,51 +1,50 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class LinkedQueue<E> extends Queue<E>{
|
||||
|
||||
Node head;
|
||||
Node tail;
|
||||
int size;
|
||||
public class LinkedQueue<Item> extends Queue<Item>{
|
||||
private Node head = null, tail = null;
|
||||
private int size = 0;
|
||||
|
||||
private class Node{
|
||||
E value;
|
||||
Item value;
|
||||
Node next;
|
||||
|
||||
Node(E value){
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return size==0;
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E dequeue() {
|
||||
if(isEmpty())
|
||||
public Item dequeue() {
|
||||
if(size == 0)
|
||||
// throw new java.util.NoSuchElementException();
|
||||
return null;
|
||||
E element = head.value;
|
||||
// Only a single element is present
|
||||
if (head == tail){
|
||||
tail = null;
|
||||
}
|
||||
|
||||
Item element = head.value;
|
||||
head = head.next;
|
||||
--size;
|
||||
size--;
|
||||
if(isEmpty())
|
||||
tail=head;
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enqueue(E element) {
|
||||
Node newNode = new Node(element);
|
||||
if(isEmpty())
|
||||
head = newNode;
|
||||
else
|
||||
public void enqueue(Item element) {
|
||||
Node newNode = new Node();
|
||||
newNode.value = element;
|
||||
newNode.next = null;
|
||||
if(isEmpty()){
|
||||
tail=newNode;
|
||||
head=tail;
|
||||
}
|
||||
else{
|
||||
tail.next = newNode;
|
||||
tail = newNode;
|
||||
++size;
|
||||
tail = tail.next;
|
||||
}
|
||||
size++;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -54,9 +53,8 @@ public class LinkedQueue<E> extends Queue<E>{
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
public Iterator<Item> iterator() {
|
||||
return new Iterator<Item>() {
|
||||
Node current = head;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
@ -64,11 +62,42 @@ public class LinkedQueue<E> extends Queue<E>{
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
E element = current.value;
|
||||
public Item next() {
|
||||
Item element = current.value;
|
||||
current = current.next;
|
||||
return element;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
Queue<Integer> q = new LinkedQueue<>();
|
||||
|
||||
try{
|
||||
System.out.println("Queue empty?: " + q.isEmpty());
|
||||
q.enqueue(1);
|
||||
q.enqueue(2);
|
||||
q.enqueue(3);
|
||||
q.enqueue(4);
|
||||
|
||||
for(int value: q)
|
||||
System.out.println(value);
|
||||
|
||||
System.out.println("Queue empty?: " + q.isEmpty());
|
||||
System.out.println("Queue Size?: " + q.size());
|
||||
|
||||
q.dequeue();
|
||||
q.dequeue();
|
||||
// q.dequeue();
|
||||
// q.dequeue();
|
||||
// q.dequeue();
|
||||
|
||||
for(int value: q)
|
||||
System.out.println(value);
|
||||
|
||||
} catch (java.util.NoSuchElementException e) {
|
||||
System.out.println("Cannot dequeue empty queue");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,72 +1,86 @@
|
||||
package com.hithomelabs.princeton1.module4;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
// Creating a concrete linked Implementation of Stack
|
||||
public class LinkedStack<E> extends Stack<E>{
|
||||
public class LinkedStack<Item> extends Stack<Item> {
|
||||
|
||||
// No need for an explicit constructor as size will be initialized to 0 and root to null
|
||||
private int size;
|
||||
private Node first;
|
||||
private Node top = null;
|
||||
private int size = 0;
|
||||
|
||||
// By default instance variables are package private
|
||||
private class Node{
|
||||
E value;
|
||||
Item value;
|
||||
Node next;
|
||||
}
|
||||
|
||||
// Will return true if size is 0
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return (this.size == 0);
|
||||
return size == 0 ;
|
||||
}
|
||||
|
||||
// Adds an element to the start of a linked list
|
||||
@Override
|
||||
public void push(E element) {
|
||||
public void push(Item element) {
|
||||
Node newNode = new Node();
|
||||
newNode.value = element;
|
||||
newNode.next = first;
|
||||
first = newNode;
|
||||
this.size = this.size + 1;
|
||||
newNode.next = top;
|
||||
top = newNode;
|
||||
// System.out.println(top.value);
|
||||
size++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() {
|
||||
if (this.isEmpty())
|
||||
return null;
|
||||
else{
|
||||
Node toBePopped = first;
|
||||
first = first.next;
|
||||
this.size = this.size - 1;
|
||||
return toBePopped.value;
|
||||
public Item pop() {
|
||||
if(size == 0){
|
||||
throw new java.util.NoSuchElementException();
|
||||
}
|
||||
Item element = top.value;
|
||||
top=top.next;
|
||||
size--;
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.size;
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new Iterator<E>() {
|
||||
|
||||
// Internal classes can access outer objects
|
||||
Node current = first;
|
||||
public Iterator<Item> iterator() {
|
||||
return new Iterator<Item>() {
|
||||
private Node current = top;
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return current != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() {
|
||||
E element = current.value;
|
||||
current = current.next;
|
||||
public Item next() {
|
||||
Item element = current.value;
|
||||
current=current.next;
|
||||
return element;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
Stack <String> s = new LinkedStack<>();
|
||||
|
||||
try {
|
||||
System.out.println("String empty?: " + s.isEmpty());
|
||||
// s.pop();
|
||||
s.push("12");
|
||||
s.push("13");
|
||||
s.pop();
|
||||
s.push("14");
|
||||
System.out.println("String empty?: " + s.isEmpty());
|
||||
System.out.println("String size?: " + s.size());
|
||||
|
||||
for(String value: s){
|
||||
System.out.print(value+" ");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Cannot pop an empty stack");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user