Skip to content

Commit 048a426

Browse files
committed
Circular Linked Lists
Circular Linked Lists
1 parent d122569 commit 048a426

File tree

6 files changed

+226
-1
lines changed

6 files changed

+226
-1
lines changed

bin/chapter3linkedlists/CLLNode.class

1 KB
Binary file not shown.
2.88 KB
Binary file not shown.
0 Bytes
Binary file not shown.

src/chapter3linkedlists/CLLNode.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 : CLLNode.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 chapter3linkedlists;
16+
17+
public class CLLNode {
18+
public CLLNode next;
19+
public int data;
20+
21+
// Creates an empty node.
22+
public CLLNode(){
23+
next = null;
24+
data = Integer.MIN_VALUE;
25+
}
26+
27+
// Creates a node storing the specified data.
28+
public CLLNode (int data){
29+
next = null;
30+
this.data = data;
31+
}
32+
33+
// Returns the node that follows this one.
34+
public CLLNode getNext(){
35+
return next;
36+
}
37+
38+
// Sets the node that follows this one.
39+
public void setNext (CLLNode node){
40+
next = node;
41+
}
42+
43+
// Returns the data stored in this node.
44+
public int getData(){
45+
return data;
46+
}
47+
48+
// Sets the data stored in this node.
49+
public void setData (int elem){
50+
data = elem;
51+
}
52+
53+
// Sets the data stored in this node.
54+
public String toString (){
55+
return Integer.toString(data);
56+
}
57+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 : CircularLinkedList.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 chapter3linkedlists;
16+
17+
public class CircularLinkedList{
18+
protected CLLNode tail;
19+
protected int length;
20+
21+
// Constructs a new circular list
22+
public CircularLinkedList(){
23+
tail = null;
24+
length = 0;
25+
}
26+
27+
// Adds data to beginning of list.
28+
public void add(int data){
29+
addToHead(data);
30+
}
31+
32+
// Adds element to head of list
33+
public void addToHead(int data){
34+
CLLNode temp = new CLLNode(data);
35+
if (tail == null) { // first data added
36+
tail = temp;
37+
tail.setNext(tail);
38+
} else { // element exists in list
39+
temp.setNext(tail.getNext());
40+
tail.setNext(temp);
41+
}
42+
length++;
43+
}
44+
45+
// Adds element to tail of list
46+
public void addToTail(int data){
47+
// new entry:
48+
addToHead(data);
49+
tail = tail.getNext();
50+
}
51+
52+
// Returns data at head of list
53+
public int peek(){
54+
return tail.getNext().getData();
55+
}
56+
57+
// Returns data at tail of list
58+
public int tailPeek(){
59+
return tail.getData();
60+
}
61+
62+
// Returns and removes data from head of list
63+
public int removeFromHead(){
64+
CLLNode temp = tail.getNext(); // ie. the head of the list
65+
if (tail == tail.getNext()) {
66+
tail = null;
67+
} else {
68+
tail.setNext(temp.getNext());
69+
temp.setNext(null); // helps clean things up; temp is free
70+
}
71+
length--;
72+
return temp.getData();
73+
}
74+
75+
// Returns and removes data from tail of list
76+
public int removeFromTail(){
77+
if (isEmpty()){
78+
return Integer.MIN_VALUE;
79+
}
80+
CLLNode finger = tail;
81+
while (finger.getNext() != tail) {
82+
finger = finger.getNext();
83+
}
84+
// finger now points to second-to-last data
85+
CLLNode temp = tail;
86+
if (finger == tail) {
87+
tail = null;
88+
} else {
89+
finger.setNext(tail.getNext());
90+
tail = finger;
91+
}
92+
length--;
93+
return temp.getData();
94+
}
95+
96+
// Returns true if list contains data, else false
97+
public boolean contains(int data){
98+
if (tail == null) return false;
99+
CLLNode finger;
100+
finger = tail.getNext();
101+
while (finger != tail && (!(finger.getData() == data))){
102+
finger = finger.getNext();
103+
}
104+
return finger.getData() == data;
105+
}
106+
107+
// Removes and returns element equal to data, or null
108+
public int remove(int data){
109+
if (tail == null) return Integer.MIN_VALUE;
110+
CLLNode finger = tail.getNext();
111+
CLLNode previous = tail;
112+
int compares;
113+
for (compares = 0; compares < length && (!(finger.getData() == data)); compares++) {
114+
previous = finger;
115+
finger = finger.getNext();
116+
}
117+
if (finger.getData() == data) {
118+
// an example of the pigeon-hole principle
119+
if (tail == tail.getNext()) {
120+
tail = null; }
121+
else {
122+
if (finger == tail)
123+
tail = previous;
124+
previous.setNext(previous.getNext().getNext());
125+
}
126+
// finger data free
127+
finger.setNext(null); // to keep things disconnected
128+
length--; // fewer elements
129+
return finger.getData();
130+
}
131+
else return Integer.MIN_VALUE;
132+
}
133+
134+
// Return the current length of the CLL.
135+
public int size(){
136+
return length;
137+
}
138+
139+
// Return the current length of the CLL.
140+
public int length() {
141+
return length;
142+
}
143+
144+
// Returns true if no elements in list
145+
public boolean isEmpty(){
146+
return tail == null;
147+
}
148+
149+
// Remove everything from the CLL.
150+
public void clear(){
151+
length = 0;
152+
tail = null;
153+
}
154+
155+
// Return a string representation of this collection, in the form: ["str1","str2",...].
156+
public String toString(){
157+
String result = "[";
158+
if (tail == null) {
159+
return result+"]";
160+
}
161+
result = result + tail.getData();
162+
CLLNode temp = tail.getNext();
163+
while (temp != tail) {
164+
result = result + "," + temp.getData();
165+
temp = temp.getNext();
166+
}
167+
return result + "]";
168+
}
169+
}

src/chapter3linkedlists/LinkedList.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public void remove(int position) {
151151
if (position == 0) {
152152
head = head.getNext();
153153
}
154-
155154
// else advance to the correct position and remove
156155
else {
157156
ListNode temp = head;

0 commit comments

Comments
 (0)