Mod 4 all exercises

This commit is contained in:
jugal 2025-03-04 16:00:23 +05:30
parent 38de5fe0a5
commit 60e8425dd9
27 changed files with 366 additions and 208 deletions

1
.idea/.name generated Normal file
View File

@ -0,0 +1 @@
Pricenton1

9
.idea/Princeton1.iml generated Normal file
View 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
View File

@ -4,6 +4,8 @@
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="delegatedBuild" value="false" />
<option name="testRunner" value="PLATFORM" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules"> <option name="modules">
<set> <set>
@ -14,6 +16,7 @@
<option value="$PROJECT_DIR$/module5" /> <option value="$PROJECT_DIR$/module5" />
<option value="$PROJECT_DIR$/module6" /> <option value="$PROJECT_DIR$/module6" />
<option value="$PROJECT_DIR$/module7" /> <option value="$PROJECT_DIR$/module7" />
<option value="$PROJECT_DIR$/module8" />
</set> </set>
</option> </option>
</GradleProjectSettings> </GradleProjectSettings>

8
.idea/modules.xml generated
View File

@ -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>

View File

@ -2,102 +2,65 @@ package com.hithomelabs.princeton1.module4;
import java.util.Iterator; import java.util.Iterator;
import javax.annotation.Nonnull; public class ArrayQueue<Item> extends Queue<Item> {
public class ArrayQueue<E> extends Queue<E>{ private int capacity=3;
private Item[] arr = (Item[]) new Object[capacity];
// insertion from tail, removal from head private int head=0,tail=0;
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;
}
@Override @Override
public boolean isEmpty() { 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 @Override
public E dequeue() { public Item dequeue() {
if(isEmpty()) if(isEmpty())
return null; return null;
else{ // System.out.println(head+" "+arr[head]);
E element = arr[head]; Item element = arr[head++];
// Garbage collection if(size() < capacity/4)
arr[head] = null; resize(capacity/2);
head = (head+1)%capacity; return element;
size = size - 1;
if(capacity >= 40 && size < capacity/4){
capacity = capacity/2;
resize(capacity, capacity*2);
}
return element;
}
} }
@Override @Override
public void enqueue(E element) { public void enqueue(Item element) {
// We plan capacity expansion if needed if(tail == capacity)
if (size == capacity){ resize(capacity * 2);
capacity = capacity * 2; arr[tail++] = element;
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;
} }
@Override @Override
public int size() { public int size() {
return size; return (tail - head);
} }
@Override @Override
@Nonnull public Iterator<Item> iterator() {
public Iterator<E> iterator() { return new Iterator<Item>() {
return new Iterator<E>() { int current = head;
int counter = size;
int pointer = head;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return counter != 0; return current != tail;
} }
@Override @Override
public E next() { public Item next() {
E element = arr[pointer]; return arr[current++];
pointer = (pointer + 1)% capacity;
--counter;
return element;
} }
}; };
} }

View File

@ -1,98 +1,109 @@
package com.hithomelabs.princeton1.module4; package com.hithomelabs.princeton1.module4;
import java.util.Iterator; import java.util.Iterator;
import javax.annotation.Nonnull;
// Concrete implementation of stack using arrays public class ArrayStack<Item> extends Stack<Item>{
// Creating a generic stack of type E
public class ArrayStack<E> extends Stack<E> {
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10 private int capacity=3;
// every time size == capacity, capacity = 2 * capacity private int top=0;
private static final int DEFAULT_CAPACITY = 10; private Item[] arr = (Item[]) new Object[capacity];
private int capacity;
private int size;
private E[] arr;
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 @Override
public boolean isEmpty() { public boolean isEmpty() {
return size == 0; return top == 0;
} }
private void changeCapacity(int newCapacity){ private void resize(int newCapacity){
E[] resizedArr = (E[]) new Object[newCapacity]; Item[] temp = arr;
for (int i = 0; i < size; i++) arr = (Item[]) new Object[newCapacity];
resizedArr[i] = arr[i];
arr = resizedArr;
}
private void incrementSize(){ for(int i=0; i<top; i++){
if (size == capacity){ arr[i]=temp[i];
capacity = 2 * capacity;
changeCapacity(capacity);
} }
} capacity=newCapacity;
// System.out.println("New Capacity: "+capacity);
// 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;
} }
@Override @Override
public E pop() { public void push(Item element) {
if (isEmpty()) 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; return null;
else{
E e = arr[--size];
arr[size] = null;
checkResize();
return e;
}
}
private void checkResize() { Item element = arr[--top];
if (size < capacity / 4 && capacity >= 20){ arr[top] = null;
capacity = capacity / 2;
changeCapacity(capacity); if(top < capacity/4)
} resize(capacity/2);
return element;
} }
@Override @Override
public int size() { public int size() {
return size; return top;
} }
@Override @Override
@Nonnull public Iterator<Item> iterator() {
public Iterator<E> iterator() { return new Iterator<Item>() {
return new Iterator<E>() { private int current=0;
int current = 0;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return current != size; return current<top;
} }
@Override @Override
public E next() { public Item next() {
E element = arr[current]; return arr[current++];
current = current + 1;
return element;
} }
}; };
} }
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");
}
}
} }

View File

@ -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);
}
}
}

View File

@ -1,51 +1,50 @@
package com.hithomelabs.princeton1.module4; package com.hithomelabs.princeton1.module4;
import javax.annotation.Nonnull;
import java.util.Iterator; import java.util.Iterator;
public class LinkedQueue<E> extends Queue<E>{ public class LinkedQueue<Item> extends Queue<Item>{
private Node head = null, tail = null;
Node head; private int size = 0;
Node tail;
int size;
private class Node{ private class Node{
E value; Item value;
Node next; Node next;
Node(E value){
this.value = value;
}
} }
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return size==0; return size == 0;
} }
@Override @Override
public E dequeue() { public Item dequeue() {
if(isEmpty()) if(size == 0)
// throw new java.util.NoSuchElementException();
return null; return null;
E element = head.value;
// Only a single element is present Item element = head.value;
if (head == tail){
tail = null;
}
head = head.next; head = head.next;
--size; size--;
if(isEmpty())
tail=head;
return element; return element;
} }
@Override @Override
public void enqueue(E element) { public void enqueue(Item element) {
Node newNode = new Node(element); Node newNode = new Node();
if(isEmpty()) newNode.value = element;
head = newNode; newNode.next = null;
else if(isEmpty()){
tail=newNode;
head=tail;
}
else{
tail.next = newNode; tail.next = newNode;
tail = newNode; tail = tail.next;
++size; }
size++;
} }
@Override @Override
@ -54,9 +53,8 @@ public class LinkedQueue<E> extends Queue<E>{
} }
@Override @Override
@Nonnull public Iterator<Item> iterator() {
public Iterator<E> iterator() { return new Iterator<Item>() {
return new Iterator<E>() {
Node current = head; Node current = head;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
@ -64,11 +62,42 @@ public class LinkedQueue<E> extends Queue<E>{
} }
@Override @Override
public E next() { public Item next() {
E element = current.value; Item element = current.value;
current = current.next; current = current.next;
return element; 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");
}
} }
} }

View File

@ -1,72 +1,86 @@
package com.hithomelabs.princeton1.module4; package com.hithomelabs.princeton1.module4;
import java.util.Iterator; import java.util.Iterator;
// Creating a concrete linked Implementation of Stack public class LinkedStack<Item> extends Stack<Item> {
public class LinkedStack<E> extends Stack<E>{
// No need for an explicit constructor as size will be initialized to 0 and root to null private Node top = null;
private int size; private int size = 0;
private Node first;
// By default instance variables are package private
private class Node{ private class Node{
E value; Item value;
Node next; Node next;
} }
// Will return true if size is 0
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return (this.size == 0); return size == 0 ;
} }
// Adds an element to the start of a linked list
@Override @Override
public void push(E element) { public void push(Item element) {
Node newNode = new Node(); Node newNode = new Node();
newNode.value = element; newNode.value = element;
newNode.next = first; newNode.next = top;
first = newNode; top = newNode;
this.size = this.size + 1; // System.out.println(top.value);
size++;
} }
@Override @Override
public E pop() { public Item pop() {
if (this.isEmpty()) if(size == 0){
return null; throw new java.util.NoSuchElementException();
else{
Node toBePopped = first;
first = first.next;
this.size = this.size - 1;
return toBePopped.value;
} }
Item element = top.value;
top=top.next;
size--;
return element;
} }
@Override @Override
public int size() { public int size() {
return this.size; return size;
} }
@Override @Override
public Iterator<E> iterator() { public Iterator<Item> iterator() {
return new Iterator<E>() { return new Iterator<Item>() {
private Node current = top;
// Internal classes can access outer objects
Node current = first;
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return current != null; return current != null;
} }
@Override @Override
public E next() { public Item next() {
E element = current.value; Item element = current.value;
current = current.next; current=current.next;
return element; 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");
}
}
} }