From 9983e266ba3beb85c7d7aae5eed231ad913aad7d Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:08:53 +0800 Subject: [PATCH 01/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Chapter8/Try_This_8_1.java diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java new file mode 100644 index 0000000..a331471 --- /dev/null +++ b/Chapter8/Try_This_8_1.java @@ -0,0 +1,19 @@ +// A character queue interface +interface ICharQ{ + + // Put a character into the queue. + void put(char ch); + + // Get a character from the queue. + char get(); +} + + +public class Try_This_8_1 { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + } + +} From d972d3051068a94e30da086eb97b48d97920bed2 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:17:09 +0800 Subject: [PATCH 02/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index a331471..ed4e8dd 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -9,6 +9,27 @@ interface ICharQ{ } +// A fixed-size queue class for characters. +class FixedQueue implements ICharQ{ + + private char q[]; // This array holds the queue. + private int putloc, getloc; // The put and get indices. + + @Override + public void put(char ch) { + // TODO Auto-generated method stub + + } + + @Override + public char get() { + // TODO Auto-generated method stub + return 0; + } + +} + + public class Try_This_8_1 { public static void main(String[] args) { From 3944846104635066af3e6702f2ed8f34527cef16 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:19:44 +0800 Subject: [PATCH 03/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index ed4e8dd..e227098 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -14,14 +14,16 @@ class FixedQueue implements ICharQ{ private char q[]; // This array holds the queue. private int putloc, getloc; // The put and get indices. + + public FixedQueue(int size) { + q = new char[size]; // allocate memory for queue. + putloc = getloc = 0; + } - @Override public void put(char ch) { - // TODO Auto-generated method stub } - @Override public char get() { // TODO Auto-generated method stub return 0; From 77dd325e75ad5f8184623abdc9ffc6f42822484f Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:24:25 +0800 Subject: [PATCH 04/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index e227098..24d0937 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -21,7 +21,12 @@ public FixedQueue(int size) { } public void put(char ch) { + if(putloc == q.length) { + System.out.println(" - Queue is full."); + return; + } + q[putloc++] = ch; } public char get() { From 62ee7e47c5c803109379fe192c12048ec1cf9cda Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:26:25 +0800 Subject: [PATCH 05/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 24d0937..d35cea1 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -30,8 +30,13 @@ public void put(char ch) { } public char get() { - // TODO Auto-generated method stub - return 0; + + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + + return (char) 0; + } + return q[getloc++]; } } From fdb7b1ee837ea9e329862309e5575ffbcf624d27 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:30:12 +0800 Subject: [PATCH 06/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index d35cea1..09c1c42 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -41,6 +41,21 @@ public char get() { } +class CircularQueue implements ICharQ{ + private char q[]; // this array holds the queue. + private int putloc, getloc; // the put and get indices. + + public void put(char ch) { + // TODO Auto-generated method stub + + } + + public char get() { + // TODO Auto-generated method stub + return 0; + } +} + public class Try_This_8_1 { From c6118f9e52c7b5064a0077be61cd36ba0d7644a5 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:46:52 +0800 Subject: [PATCH 07/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 41 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 09c1c42..4f11bee 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -44,19 +44,56 @@ public char get() { class CircularQueue implements ICharQ{ private char q[]; // this array holds the queue. private int putloc, getloc; // the put and get indices. + + // Construct an empty queue given its size. + public CircularQueue(int size) { + q = new char[size + 1]; // allocate memory for queue + putloc = getloc = 0; + } public void put(char ch) { - // TODO Auto-generated method stub + /* + * Queue is full if either putloc is one less than getloc, + * or if putloc is at the end of the array and getloc is at + * the beginning. + * */ + + if (putloc + 1 == getloc | ((putloc == q.length - 1) & (getloc == 0))) { + System.out.println(" - Queue is full. "); + return; + } + + q[putloc++] = ch; + + if (putloc == q.length) { + putloc = 0; + } } public char get() { // TODO Auto-generated method stub - return 0; + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + return (char) 0; + } + + char ch = q[getloc++]; + + if (getloc == q.length) { + getloc = 0; + } + + return ch; } } +class DynQueue implements ICharQ{ + +} + + public class Try_This_8_1 { public static void main(String[] args) { From 4b36e2baae369bc31a3930fc5e8f53c592d14ba7 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:47:06 +0800 Subject: [PATCH 08/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 4f11bee..45afa8b 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -90,6 +90,18 @@ public char get() { class DynQueue implements ICharQ{ + + @Override + public void put(char ch) { + // TODO Auto-generated method stub + + } + + @Override + public char get() { + // TODO Auto-generated method stub + return 0; + } } From b1f34621768464fed08fd6fc929ee306d6ecdb51 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:53:53 +0800 Subject: [PATCH 09/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 39 ++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 45afa8b..892c1de 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -1,4 +1,7 @@ // A character queue interface + +import javax.print.attribute.Size2DSyntax; + interface ICharQ{ // Put a character into the queue. @@ -90,17 +93,45 @@ public char get() { class DynQueue implements ICharQ{ + + private char q[]; + private int putloc, getloc; + + public DynQueue(int size) { + q = new char[size]; + putloc = getloc = 0; + } + - @Override public void put(char ch) { // TODO Auto-generated method stub + if (putloc == q.length) { + // increase queue size + char t[] = new char[q.length * 2]; + + // copy elements into new queue + for (int i = 0; i < q.length; i++) { + + t[i] = q[i]; + + q = t; + } + q[putloc++] = ch; + } + } - @Override + public char get() { - // TODO Auto-generated method stub - return 0; + + if (getloc == putloc) { + System.out.println(" - Queue is empty."); + return (char) 0; + } + + return q[getloc++]; + } } From f1396e2068ed929249321fdd1a013e5c204de83b Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:56:52 +0800 Subject: [PATCH 10/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 892c1de..0678a7f 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -1,7 +1,5 @@ // A character queue interface -import javax.print.attribute.Size2DSyntax; - interface ICharQ{ // Put a character into the queue. @@ -141,6 +139,12 @@ public class Try_This_8_1 { public static void main(String[] args) { // TODO Auto-generated method stub + + FixedQueue q1 = new FixedQueue(10); + DynQueue q2 = new DynQueue(5); + CircularQueue q3 = new CircularQueue(10); + + ICharQ iQ; } From 5210b57c104bc2af28009e9f1303211c41bd0683 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:57:24 +0800 Subject: [PATCH 11/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 0678a7f..25c054c 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -145,6 +145,9 @@ public static void main(String[] args) { CircularQueue q3 = new CircularQueue(10); ICharQ iQ; + + char ch; + int i; } From 85615cdb43221ed9b56553873ef8bec415b44fa5 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 21:22:06 +0800 Subject: [PATCH 12/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 25c054c..de4b1dc 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -148,6 +148,15 @@ public static void main(String[] args) { char ch; int i; + + iQ = q1; + + // Put some characters into fixed queue. + for (i = 0; i < 10; i++) { + iQ.put((char) ('A' + i)); + } + + // Show the queue. } From 935042f1502e4cdd298995956f2c8af015bcd59a Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:58:01 +0800 Subject: [PATCH 13/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index de4b1dc..07350db 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -157,6 +157,7 @@ public static void main(String[] args) { } // Show the queue. + System.out.print("Contents of fixed queue: "); } From 421badb760c9bdd0d8b62429277d7835283b1d6e Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:00:02 +0800 Subject: [PATCH 14/17] Committed on or around 2023/10/03 --- Chapter8/Try_This_8_1.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 07350db..7fc33f2 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -158,6 +158,11 @@ public static void main(String[] args) { // Show the queue. System.out.print("Contents of fixed queue: "); + + for (i = 0; i < 10; i++) { + ch = iQ.get(); + System.out.print(ch); + } } From d4331dc3f18d9bcc1c4197c8d5ffc71c0dadf175 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:02:31 +0800 Subject: [PATCH 15/17] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index 7fc33f2..f5af1c9 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -163,6 +163,13 @@ public static void main(String[] args) { ch = iQ.get(); System.out.print(ch); } + + System.out.println(); + + iQ = q3; + + // Put some characters into circular queue. + } From 547a382f210161b79c4bea716e77916f5b697ed2 Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:10:56 +0800 Subject: [PATCH 16/17] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index f5af1c9..b5864d5 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -164,12 +164,33 @@ public static void main(String[] args) { System.out.print(ch); } + System.out.println(); + + iQ = q2; + + // Put some characters into dynamic queue. + for (i = 0; i < 10; i++) { + iQ.put((char) ('Z' - i)); + } + + // Show the queue. + System.out.print("Contents of dynamic queue: "); + + System.out.println(); iQ = q3; // Put some characters into circular queue. + for (i = 0; i < 20; i++) { + iQ.put((char) ('A' + i)); + } + // Show the queue. + System.out.print("Contents of circular queue: "); + for (i = 0; i < 10; i++) { + + } } From a80ed15e9e362320b3d506c7e40960a43441aa7e Mon Sep 17 00:00:00 2001 From: Ceiling_roof <48134466+ResilientSpring@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:13:44 +0800 Subject: [PATCH 17/17] Committed on or around 2023/10/04 --- Chapter8/Try_This_8_1.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Chapter8/Try_This_8_1.java b/Chapter8/Try_This_8_1.java index b5864d5..3528fe2 100644 --- a/Chapter8/Try_This_8_1.java +++ b/Chapter8/Try_This_8_1.java @@ -175,6 +175,10 @@ public static void main(String[] args) { // Show the queue. System.out.print("Contents of dynamic queue: "); + for (i = 0; i < 10; i++) { + ch = iQ.get(); + System.out.print(ch); + } System.out.println();