Skip to content

Commit e5756f8

Browse files
committed
Updates
Updates
1 parent ccf8fb6 commit e5756f8

File tree

8 files changed

+177
-3
lines changed

8 files changed

+177
-3
lines changed
0 Bytes
Binary file not shown.
1.76 KB
Binary file not shown.

bin/chapter4stacks/LinkedStack.class

0 Bytes
Binary file not shown.

bin/chapter5queues/LinkedQueue.class

1.71 KB
Binary file not shown.

src/chapter3linkedlists/ListNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void setNext (ListNode node){
3939
}
4040

4141
// Returns the data stored in this node.
42-
public int getdata(){
42+
public int getData(){
4343
return data;
4444
}
4545

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 : FixedSizeArrayStack.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 chapter4stacks;
16+
public class FixedSizeArrayStack{
17+
// Length of the array used to implement the stack.
18+
protected int capacity;
19+
20+
// Default array capacity.
21+
public static final int CAPACITY = 10;
22+
23+
// Array used to implement the stack.
24+
protected int[] stackRep;
25+
26+
// Index of the top element of the stack in the array.
27+
protected int top = -1;
28+
29+
// Initializes the stack to use an array of default length.
30+
public FixedSizeArrayStack() {
31+
this(CAPACITY); // default capacity
32+
}
33+
34+
// Initializes the stack to use an array of given length.
35+
public FixedSizeArrayStack(int cap) {
36+
capacity = cap;
37+
stackRep = new int[capacity]; // compiler may give warning, but this
38+
// is ok
39+
}
40+
41+
// Returns the number of elements in the stack. This method runs in O(1) time.
42+
public int size() {
43+
return (top + 1);
44+
}
45+
46+
// Testes whether the stack is empty. This method runs in O(1) time.
47+
public boolean isEmpty() {
48+
return (top < 0);
49+
}
50+
51+
// Inserts an element at the top of the stack. This method runs in O(1) time.
52+
public void push(int data) throws Exception {
53+
if (size() == capacity)
54+
throw new Exception("Stack is full.");
55+
stackRep[++top] = data;
56+
}
57+
58+
// Inspects the element at the top of the stack. This method runs in O(1) time.
59+
public int top() throws Exception {
60+
if (isEmpty())
61+
throw new Exception("Stack is empty.");
62+
return stackRep[top];
63+
}
64+
65+
// Removes the top element from the stack. This method runs in O(1) time.
66+
public int pop() throws Exception {
67+
int data;
68+
if (isEmpty())
69+
throw new Exception("Stack is empty.");
70+
data = stackRep[top];
71+
stackRep[top--] = Integer.MIN_VALUE; // dereference S[top] for garbage collection.
72+
return data;
73+
}
74+
75+
// Returns a string representation of the stack as a list of elements, with
76+
// the top element at the end: [ ... , prev, top ]. This method runs in O(n)
77+
// time, where n is the size of the stack.
78+
public String toString() {
79+
String s;
80+
s = "[";
81+
if (size() > 0)
82+
s += stackRep[0];
83+
if (size() > 1)
84+
for (int i = 1; i <= size() - 1; i++) {
85+
s += ", " + stackRep[i];
86+
}
87+
return s + "]";
88+
}
89+
}

src/chapter4stacks/LinkedStack.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void push (int data) {
4040
public int pop() throws EmptyStackException{
4141
if (isEmpty())
4242
throw new EmptyStackException();
43-
int result = top.getdata();
43+
int result = top.getData();
4444
top = top.getNext();
4545
length--;
4646
return result;
@@ -53,7 +53,7 @@ public int peek() throws EmptyStackException{
5353
if (isEmpty())
5454
throw new EmptyStackException();
5555

56-
return top.getdata();
56+
return top.getData();
5757
}
5858

5959
// Returns true if this stack is empty and false otherwise.

src/chapter5queues/LinkedQueue.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 : LinkedQueue.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+
import chapter3linkedlists.*;
17+
18+
public class LinkedQueue{
19+
private int length;
20+
private ListNode front, rear;
21+
22+
// Creates an empty queue.
23+
public LinkedQueue(){
24+
length = 0;
25+
front = rear = null;
26+
}
27+
28+
// Adds the specified data to the rear of the queue.
29+
public void enqueue (int data){
30+
ListNode node = new ListNode(data);
31+
if (isEmpty())
32+
front = node;
33+
else
34+
rear.setNext (node);
35+
rear = node;
36+
length++;
37+
}
38+
39+
// Removes the data at the front of the queue and returns a
40+
// reference to it. Throws an Exception if the
41+
// queue is empty.
42+
43+
public int dequeue() throws Exception{
44+
if (isEmpty())
45+
throw new Exception ("queue");
46+
int result = front.getData();
47+
front = front.getNext();
48+
length--;
49+
if (isEmpty())
50+
rear = null;
51+
return result;
52+
}
53+
54+
// Returns a reference to the data at the front of the queue.
55+
// The data is not removed from the queue. Throws an
56+
// Exception if the queue is empty.
57+
public int first() throws Exception{
58+
if (isEmpty())
59+
throw new Exception();
60+
61+
return front.getData();
62+
}
63+
64+
// Returns true if this queue is empty and false otherwise.
65+
public boolean isEmpty(){
66+
return (length == 0);
67+
}
68+
69+
// Returns the number of elements in this queue.
70+
public int size(){
71+
return length;
72+
}
73+
74+
// Returns a string representation of this queue.
75+
public String toString(){
76+
String result = "";
77+
ListNode current = front;
78+
while (current != null){
79+
result = result + current.toString() + "\n";
80+
current = current.getNext();
81+
}
82+
return result;
83+
}
84+
}
85+

0 commit comments

Comments
 (0)