Skip to content

Commit ccf8fb6

Browse files
committed
Updates
Updates
1 parent 6d91473 commit ccf8fb6

File tree

9 files changed

+116
-50
lines changed

9 files changed

+116
-50
lines changed
247 Bytes
Binary file not shown.

bin/chapter4stacks/LinkedStack.class

1.87 KB
Binary file not shown.

bin/chapter6trees/ListNode.class

-760 Bytes
Binary file not shown.
24 Bytes
Binary file not shown.

src/chapter3linkedlists/ListNode.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,44 @@
1212

1313
package chapter3linkedlists;
1414

15-
class ListNode {
16-
int data;
17-
ListNode next;
18-
19-
ListNode(int x) {
20-
data = x;
15+
public class ListNode{
16+
public ListNode next;
17+
public int data;
18+
19+
// Creates an empty node.
20+
public ListNode(){
2121
next = null;
22+
data = Integer.MIN_VALUE;
2223
}
23-
public int getData(){
24-
return this.data;
25-
}
26-
public void setData(int data){
27-
this.data = data;
24+
25+
// Creates a node storing the specified data.
26+
public ListNode (int elem){
27+
next = null;
28+
data = elem;
2829
}
30+
31+
// Returns the node that follows this one.
2932
public ListNode getNext(){
30-
return this.next;
33+
return next;
3134
}
32-
public void setNext(ListNode node){
33-
this.next = node;
35+
36+
// Sets the node that follows this one.
37+
public void setNext (ListNode node){
38+
next = node;
39+
}
40+
41+
// Returns the data stored in this node.
42+
public int getdata(){
43+
return data;
44+
}
45+
46+
// Sets the data stored in this node.
47+
public void setdata (int elem){
48+
data = elem;
3449
}
35-
}
50+
51+
// Sets the data stored in this node.
52+
public String toString (){
53+
return Integer.toString(data);
54+
}
55+
}

src/chapter4stacks/LinkedStack.java

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

src/chapter6trees/ListNode.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/chapter6trees/SortedArrayToBST.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
package chapter6trees;
14-
14+
import chapter3linkedlists.*;
1515
public class SortedArrayToBST {
1616
// bottom up
1717
public BinarySearchTreeNode sortedListToBST(ListNode head) {

src/chapter9graphs/GraphAdjacencyMatrix.java

Whitespace-only changes.

0 commit comments

Comments
 (0)