Skip to content

Commit 5ee700a

Browse files
committed
Updates
Updates
1 parent 6cd4358 commit 5ee700a

File tree

7 files changed

+221
-7
lines changed

7 files changed

+221
-7
lines changed
11 Bytes
Binary file not shown.
2.44 KB
Binary file not shown.
1.89 KB
Binary file not shown.

src/chapter4stacks/DynamicArrayStack.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public int size() {
4646
return (top + 1);
4747
}
4848

49-
// Testes whether the stack is empty. This method runs in O(1) time.
49+
// Checks whether the stack is empty. This method runs in O(1) time.
5050
public boolean isEmpty() {
5151
return (top < 0);
5252
}
@@ -57,10 +57,11 @@ public void push(int data) throws Exception {
5757
expand();
5858
stackRep[++top] = data;
5959
}
60-
60+
61+
// Increases the stack size by double
6162
private void expand() {
6263
int length = size();
63-
int[] newstack=new int[length<<1];
64+
int[] newstack=new int[length<<1]; // or 2* length
6465
System.arraycopy(stackRep,0,newstack,0,length);
6566
stackRep=newstack;
6667
}
@@ -73,9 +74,9 @@ private void shrink() {
7374
return;
7475
length=length + (top<<1); // still means shrink to at 1/2 or less of the heap
7576
if(top<MINCAPACITY) length = MINCAPACITY;
76-
int[] newstack=new int[length];
77-
System.arraycopy(stackRep,0,newstack,0,top+1);
78-
stackRep=newstack;
77+
int[] newStack=new int[length];
78+
System.arraycopy(stackRep,0,newStack,0,top+1);
79+
stackRep=newStack;
7980
}
8081

8182
// Inspects the element at the top of the stack. This method runs in O(1) time.

src/chapter4stacks/FixedSizeArrayStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public int size() {
4343
return (top + 1);
4444
}
4545

46-
// Testes whether the stack is empty. This method runs in O(1) time.
46+
// Checks whether the stack is empty. This method runs in O(1) time.
4747
public boolean isEmpty() {
4848
return (top < 0);
4949
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : DynamicArrayQueue.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter5queues;
16+
17+
public class DynamicArrayQueue{
18+
// Array used to implement the queue.
19+
private int[] queueRep;
20+
private int size, front, rear;
21+
22+
// Length of the array used to implement the queue.
23+
private static int CAPACITY = 16; //Default Queue size
24+
25+
public static int MINCAPACITY=1<<15; // power of 2
26+
27+
// Initializes the queue to use an array of default length.
28+
public DynamicArrayQueue (){
29+
queueRep = new int [CAPACITY];
30+
size = 0; front = 0; rear = 0;
31+
}
32+
33+
// Initializes the queue to use an array of given length.
34+
public DynamicArrayQueue (int cap){
35+
queueRep = new int [ cap];
36+
size = 0; front = 0; rear = 0;
37+
}
38+
39+
// Inserts an element at the rear of the queue. This method runs in O(1) time.
40+
public void enQueue (int data)throws NullPointerException, IllegalStateException{
41+
if (size == CAPACITY)
42+
expand();
43+
size++;
44+
queueRep[rear] = data;
45+
rear = (rear+1) % CAPACITY;
46+
}
47+
48+
// Removes the front element from the queue. This method runs in O(1) time.
49+
public int deQueue () throws IllegalStateException{
50+
// Effects: If queue is empty, throw IllegalStateException,
51+
// else remove and return oldest element of this
52+
if (size == 0)
53+
throw new IllegalStateException ("Queue is empty: Underflow");
54+
else {
55+
size--;
56+
int data = queueRep [ (front % CAPACITY) ];
57+
queueRep [front] = Integer.MIN_VALUE;
58+
front = (front+1) % CAPACITY;
59+
return data;
60+
}
61+
}
62+
63+
// Checks whether the queue is empty. This method runs in O(1) time.
64+
public boolean isEmpty(){
65+
return (size == 0);
66+
}
67+
68+
// Checks whether the queue is full. This method runs in O(1) time.
69+
public boolean isFull(){
70+
return (size == CAPACITY);
71+
}
72+
73+
// Returns the number of elements in the queue. This method runs in O(1) time.
74+
public int size() {
75+
return size;
76+
}
77+
78+
// Increases the queue size by double
79+
private void expand(){
80+
int length = size();
81+
int[] newQueue = new int[length<<1]; // or 2* length
82+
83+
//copy items
84+
for(int i = front; i <= rear; i ++)
85+
newQueue[i-front] = queueRep[i%CAPACITY];
86+
87+
queueRep = newQueue;
88+
front = 0;
89+
rear = size-1;
90+
CAPACITY *= 2;
91+
}
92+
93+
// dynamic array operation: shrinks to 1/2 if more than than 3/4 empty
94+
@SuppressWarnings("unused")
95+
private void shrink() {
96+
int length = size;
97+
if(length <= MINCAPACITY || length <<2 >= length)
98+
return;
99+
100+
if(length<MINCAPACITY) length = MINCAPACITY;
101+
int[] newQueue=new int[length];
102+
System.arraycopy(queueRep,0,newQueue,0,length+1);
103+
queueRep=newQueue;
104+
}
105+
106+
// Returns a string representation of the queue as a list of elements, with
107+
// the front element at the end: [ ... , prev, rear ]. This method runs in O(n)
108+
// time, where n is the size of the queue.
109+
public String toString(){
110+
String result = "[";
111+
for (int i = 0; i < size; i++){
112+
result += Integer.toString(queueRep[ (front + i) % CAPACITY ]);
113+
if (i < size -1) {
114+
result += ", ";
115+
}
116+
}
117+
result += "]";
118+
return result;
119+
}
120+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*Copyright (c) Dec 21, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : FixedSizeArrayQueue.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter5queues;
16+
17+
public class FixedSizeArrayQueue{
18+
// Array used to implement the queue.
19+
private int[] queueRep;
20+
private int size, front, rear;
21+
22+
// Length of the array used to implement the queue.
23+
private static final int CAPACITY = 16; //Default Queue size
24+
25+
// Initializes the queue to use an array of default length.
26+
public FixedSizeArrayQueue (){
27+
queueRep = new int [CAPACITY];
28+
size = 0; front = 0; rear = 0;
29+
}
30+
31+
// Initializes the queue to use an array of given length.
32+
public FixedSizeArrayQueue (int cap){
33+
queueRep = new int [ cap];
34+
size = 0; front = 0; rear = 0;
35+
}
36+
37+
// Inserts an element at the rear of the queue. This method runs in O(1) time.
38+
public void enQueue (int data)throws NullPointerException, IllegalStateException{
39+
if (size == CAPACITY)
40+
throw new IllegalStateException ("Queue is full: Overflow");
41+
else {
42+
size++;
43+
queueRep[rear] = data;
44+
rear = (rear+1) % CAPACITY;
45+
}
46+
}
47+
48+
// Removes the front element from the queue. This method runs in O(1) time.
49+
public int deQueue () throws IllegalStateException{
50+
// Effects: If queue is empty, throw IllegalStateException,
51+
// else remove and return oldest element of this
52+
if (size == 0)
53+
throw new IllegalStateException ("Queue is empty: Underflow");
54+
else {
55+
size--;
56+
int data = queueRep [ (front % CAPACITY) ];
57+
queueRep [front] = Integer.MIN_VALUE;
58+
front = (front+1) % CAPACITY;
59+
return data;
60+
}
61+
}
62+
63+
// Checks whether the queue is empty. This method runs in O(1) time.
64+
public boolean isEmpty(){
65+
return (size == 0);
66+
}
67+
68+
// Checks whether the queue is full. This method runs in O(1) time.
69+
public boolean isFull(){
70+
return (size == CAPACITY);
71+
}
72+
73+
// Returns the number of elements in the queue. This method runs in O(1) time.
74+
public int size() {
75+
return size;
76+
}
77+
78+
// Returns a string representation of the queue as a list of elements, with
79+
// the front element at the end: [ ... , prev, rear ]. This method runs in O(n)
80+
// time, where n is the size of the queue.
81+
public String toString(){
82+
String result = "[";
83+
for (int i = 0; i < size; i++){
84+
result += Integer.toString(queueRep[ (front + i) % CAPACITY ]);
85+
if (i < size -1) {
86+
result += ", ";
87+
}
88+
}
89+
result += "]";
90+
return result;
91+
}
92+
93+
}

0 commit comments

Comments
 (0)