-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a3d379
commit ac674a2
Showing
9 changed files
with
404 additions
and
11 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section3/GeneralizedQueueArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.ali.algojava.chapter1.section3; | ||
|
||
// 1.3.38 | ||
public class GeneralizedQueueArray<Item> { | ||
|
||
private Item[] items; | ||
private int first = 0; | ||
private int last = 0; | ||
private int size = 0; | ||
|
||
public GeneralizedQueueArray() { | ||
items = (Item[]) new Object[2]; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void insert(Item x) { | ||
if (last == size) { | ||
resize(items.length * 2); | ||
} | ||
|
||
items[last] = x; | ||
last++; | ||
size++; | ||
} | ||
|
||
public Item delete(int k) { | ||
|
||
int index = (first + k) % items.length; | ||
Item result = items[index]; | ||
items[index] = null; | ||
|
||
size--; | ||
|
||
if (index != last) { | ||
for (int i = index; i < last; i++) { | ||
items[i] = items[i + 1]; | ||
} | ||
} | ||
|
||
last--; | ||
if (size > 0 && size < items.length / 4) { | ||
resize(items.length / 2); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private void resize(int newSize) { | ||
Item[] copy = (Item[]) new Object[newSize]; | ||
for (int i = 0; i < size; i++) { | ||
Item item = items[(first + i) % items.length]; | ||
if (item == null) { | ||
continue; | ||
} | ||
copy[i] = item; | ||
} | ||
items = copy; | ||
first = 0; | ||
last = size; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...AlgoJava/src/main/java/com/ali/algojava/chapter1/section3/GeneralizedQueueLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.ali.algojava.chapter1.section3; | ||
|
||
// 1.3.38 | ||
public class GeneralizedQueueLinkedList<Item> { | ||
|
||
private Node first; | ||
private int size; | ||
|
||
public class Node { | ||
public Item item; | ||
public Node next; | ||
|
||
public Node(Item item) { | ||
this.item = item; | ||
} | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void insert(Item x) { | ||
Node newNode = new Node(x); | ||
if (isEmpty()) { | ||
first = newNode; | ||
} else { | ||
newNode.next = first; | ||
first = newNode; | ||
} | ||
size++; | ||
} | ||
|
||
public Item delete(int k) { | ||
if (isEmpty()) { | ||
throw new RuntimeException("queue is empty"); | ||
} | ||
|
||
if (k >= size) { | ||
throw new RuntimeException("not enough items"); | ||
} | ||
|
||
Item result = null; | ||
int target = size - k - 1; | ||
if (size == 1 || target == 0) { | ||
result = first.item; | ||
first = first.next; | ||
} else { | ||
int index = 0; | ||
Node current = first; | ||
|
||
while (index < target - 1) { | ||
current = current.next; | ||
index++; | ||
} | ||
|
||
result = current.next.item; | ||
current.next = current.next.next; | ||
} | ||
|
||
size--; | ||
return result; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section3/Josephus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.ali.algojava.chapter1.section3; | ||
|
||
import edu.princeton.cs.algs4.StdOut; | ||
import java.util.Arrays; | ||
|
||
// 1.3.37 | ||
// eliminate every Mth person from a circle of N persons | ||
public class Josephus { | ||
public static void main(String[] args) { | ||
int M = 2; | ||
int N = 7; | ||
if (args.length == 2) { | ||
M = Integer.parseInt(args[0]); | ||
N = Integer.parseInt(args[1]); | ||
} | ||
int[] output = josephus(M, N); | ||
StdOut.println(Arrays.toString(output)); | ||
} | ||
|
||
private static int[] josephus(int M, int N) { | ||
int[] result = new int[N]; | ||
Queue<Integer> q = new Queue<>(); | ||
for (int i = 1; i <= N; i++) { | ||
q.enqueue(i); | ||
} | ||
|
||
for (int i = 0; i < N; i++) { | ||
|
||
for (int j = 1; j < M; j++) { | ||
q.enqueue(q.dequeue()); | ||
} | ||
|
||
result[i] = q.dequeue(); | ||
} | ||
|
||
return result; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
Java/AlgoJava/src/main/java/com/ali/algojava/chapter1/section3/MoveToFront.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.ali.algojava.chapter1.section3; | ||
|
||
// 1.3.40 | ||
public class MoveToFront<Item> { | ||
|
||
private Node first; | ||
private int size; | ||
|
||
public class Node { | ||
public Item item; | ||
public Node next; | ||
public Node prev; | ||
|
||
public Node(Item item) { | ||
this.item = item; | ||
} | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
public void insert(Item item) { | ||
Node newNode = new Node(item); | ||
if (!isEmpty()) { | ||
newNode.next = first; | ||
first.prev = newNode; | ||
} | ||
first = newNode; | ||
size++; | ||
if (size > 1) { | ||
removeOtherOccurences(item); | ||
} | ||
} | ||
|
||
public Item peek() { | ||
return first == null ? null : first.item; | ||
} | ||
|
||
private void removeOtherOccurences(Item item) { | ||
Node current = first.next; | ||
while (current != null) { | ||
if (current.item == item) { | ||
current.prev.next = current.next; | ||
if (current.next != null) { | ||
current.next.prev = current.prev; | ||
} | ||
size--; | ||
} | ||
current = current.next; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.