forked from Hithomelabs/Princeton1
Compare commits
No commits in common. "main" and "main" have entirely different histories.
1
.idea/.name
generated
1
.idea/.name
generated
@ -1 +0,0 @@
|
|||||||
Pricenton1
|
|
||||||
9
.idea/Princeton1.iml
generated
9
.idea/Princeton1.iml
generated
@ -1,9 +0,0 @@
|
|||||||
<?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,8 +4,6 @@
|
|||||||
<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>
|
||||||
@ -16,7 +14,6 @@
|
|||||||
<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
Normal file
8
.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?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,65 +2,102 @@ package com.hithomelabs.princeton1.module4;
|
|||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class ArrayQueue<Item> extends Queue<Item> {
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
private int capacity=3;
|
public class ArrayQueue<E> extends Queue<E>{
|
||||||
private Item[] arr = (Item[]) new Object[capacity];
|
|
||||||
private int head=0,tail=0;
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return (tail - head) == 0;
|
return size == 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 Item dequeue() {
|
public E dequeue() {
|
||||||
if(isEmpty())
|
if(isEmpty())
|
||||||
return null;
|
return null;
|
||||||
// System.out.println(head+" "+arr[head]);
|
else{
|
||||||
Item element = arr[head++];
|
E element = arr[head];
|
||||||
if(size() < capacity/4)
|
// Garbage collection
|
||||||
resize(capacity/2);
|
arr[head] = null;
|
||||||
|
head = (head+1)%capacity;
|
||||||
|
size = size - 1;
|
||||||
|
if(capacity >= 40 && size < capacity/4){
|
||||||
|
capacity = capacity/2;
|
||||||
|
resize(capacity, capacity*2);
|
||||||
|
}
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enqueue(Item element) {
|
public void enqueue(E element) {
|
||||||
if(tail == capacity)
|
// We plan capacity expansion if needed
|
||||||
resize(capacity * 2);
|
if (size == capacity){
|
||||||
arr[tail++] = element;
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return (tail - head);
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Item> iterator() {
|
@Nonnull
|
||||||
return new Iterator<Item>() {
|
public Iterator<E> iterator() {
|
||||||
int current = head;
|
return new Iterator<E>() {
|
||||||
|
int counter = size;
|
||||||
|
int pointer = head;
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return current != tail;
|
return counter != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item next() {
|
public E next() {
|
||||||
return arr[current++];
|
E element = arr[pointer];
|
||||||
|
pointer = (pointer + 1)% capacity;
|
||||||
|
--counter;
|
||||||
|
return element;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,109 +1,98 @@
|
|||||||
package com.hithomelabs.princeton1.module4;
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
public class ArrayStack<Item> extends Stack<Item>{
|
// Concrete implementation of stack using arrays
|
||||||
|
// Creating a generic stack of type E
|
||||||
|
public class ArrayStack<E> extends Stack<E> {
|
||||||
|
|
||||||
private int capacity=3;
|
// Capacity and size are two variables, capacity determines total capacity of array, capacity defaults at 10
|
||||||
private int top=0;
|
// every time size == capacity, capacity = 2 * capacity
|
||||||
private Item[] arr = (Item[]) new Object[capacity];
|
private static final int DEFAULT_CAPACITY = 10;
|
||||||
|
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 top == 0;
|
return size == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resize(int newCapacity){
|
private void changeCapacity(int newCapacity){
|
||||||
Item[] temp = arr;
|
E[] resizedArr = (E[]) new Object[newCapacity];
|
||||||
arr = (Item[]) new Object[newCapacity];
|
for (int i = 0; i < size; i++)
|
||||||
|
resizedArr[i] = arr[i];
|
||||||
for(int i=0; i<top; i++){
|
arr = resizedArr;
|
||||||
arr[i]=temp[i];
|
|
||||||
}
|
}
|
||||||
capacity=newCapacity;
|
|
||||||
// System.out.println("New Capacity: "+capacity);
|
private void incrementSize(){
|
||||||
|
if (size == capacity){
|
||||||
|
capacity = 2 * capacity;
|
||||||
|
changeCapacity(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 void push(Item element) {
|
public E pop() {
|
||||||
if(top == capacity)
|
if (isEmpty())
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Item element = arr[--top];
|
private void checkResize() {
|
||||||
arr[top] = null;
|
if (size < capacity / 4 && capacity >= 20){
|
||||||
|
capacity = capacity / 2;
|
||||||
if(top < capacity/4)
|
changeCapacity(capacity);
|
||||||
resize(capacity/2);
|
}
|
||||||
return element;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return top;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Item> iterator() {
|
@Nonnull
|
||||||
return new Iterator<Item>() {
|
public Iterator<E> iterator() {
|
||||||
private int current=0;
|
return new Iterator<E>() {
|
||||||
|
|
||||||
|
int current = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return current<top;
|
return current != size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item next() {
|
public E next() {
|
||||||
return arr[current++];
|
E element = 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");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,136 +0,0 @@
|
|||||||
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,14 +1,21 @@
|
|||||||
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<Item> extends Queue<Item>{
|
public class LinkedQueue<E> extends Queue<E>{
|
||||||
private Node head = null, tail = null;
|
|
||||||
private int size = 0;
|
Node head;
|
||||||
|
Node tail;
|
||||||
|
int size;
|
||||||
|
|
||||||
private class Node{
|
private class Node{
|
||||||
Item value;
|
E value;
|
||||||
Node next;
|
Node next;
|
||||||
|
|
||||||
|
Node(E value){
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -17,34 +24,28 @@ public class LinkedQueue<Item> extends Queue<Item>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item dequeue() {
|
public E dequeue() {
|
||||||
if(size == 0)
|
|
||||||
// throw new java.util.NoSuchElementException();
|
|
||||||
return null;
|
|
||||||
|
|
||||||
Item element = head.value;
|
|
||||||
head = head.next;
|
|
||||||
size--;
|
|
||||||
if(isEmpty())
|
if(isEmpty())
|
||||||
tail=head;
|
return null;
|
||||||
|
E element = head.value;
|
||||||
|
// Only a single element is present
|
||||||
|
if (head == tail){
|
||||||
|
tail = null;
|
||||||
|
}
|
||||||
|
head = head.next;
|
||||||
|
--size;
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void enqueue(Item element) {
|
public void enqueue(E element) {
|
||||||
Node newNode = new Node();
|
Node newNode = new Node(element);
|
||||||
newNode.value = element;
|
if(isEmpty())
|
||||||
newNode.next = null;
|
head = newNode;
|
||||||
if(isEmpty()){
|
else
|
||||||
tail=newNode;
|
|
||||||
head=tail;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
tail.next = newNode;
|
tail.next = newNode;
|
||||||
tail = tail.next;
|
tail = newNode;
|
||||||
}
|
++size;
|
||||||
size++;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -53,8 +54,9 @@ public class LinkedQueue<Item> extends Queue<Item>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Item> iterator() {
|
@Nonnull
|
||||||
return new Iterator<Item>() {
|
public Iterator<E> iterator() {
|
||||||
|
return new Iterator<E>() {
|
||||||
Node current = head;
|
Node current = head;
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
@ -62,42 +64,11 @@ public class LinkedQueue<Item> extends Queue<Item>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item next() {
|
public E next() {
|
||||||
Item element = current.value;
|
E 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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,86 +1,72 @@
|
|||||||
package com.hithomelabs.princeton1.module4;
|
package com.hithomelabs.princeton1.module4;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class LinkedStack<Item> extends Stack<Item> {
|
// Creating a concrete linked Implementation of Stack
|
||||||
|
public class LinkedStack<E> extends Stack<E>{
|
||||||
|
|
||||||
private Node top = null;
|
// No need for an explicit constructor as size will be initialized to 0 and root to null
|
||||||
private int size = 0;
|
private int size;
|
||||||
|
private Node first;
|
||||||
|
|
||||||
|
// By default instance variables are package private
|
||||||
private class Node{
|
private class Node{
|
||||||
Item value;
|
E value;
|
||||||
Node next;
|
Node next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Will return true if size is 0
|
||||||
@Override
|
@Override
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return size == 0 ;
|
return (this.size == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds an element to the start of a linked list
|
||||||
@Override
|
@Override
|
||||||
public void push(Item element) {
|
public void push(E element) {
|
||||||
Node newNode = new Node();
|
Node newNode = new Node();
|
||||||
newNode.value = element;
|
newNode.value = element;
|
||||||
newNode.next = top;
|
newNode.next = first;
|
||||||
top = newNode;
|
first = newNode;
|
||||||
// System.out.println(top.value);
|
this.size = this.size + 1;
|
||||||
size++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Item pop() {
|
public E pop() {
|
||||||
if(size == 0){
|
if (this.isEmpty())
|
||||||
throw new java.util.NoSuchElementException();
|
return null;
|
||||||
|
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 size;
|
return this.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterator<Item> iterator() {
|
public Iterator<E> iterator() {
|
||||||
return new Iterator<Item>() {
|
return new Iterator<E>() {
|
||||||
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 Item next() {
|
public E next() {
|
||||||
Item element = current.value;
|
E 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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user