Skip to content

Commit 4bb58f7

Browse files
committed
New Problems
New Problems
1 parent 9dabdd9 commit 4bb58f7

File tree

5 files changed

+147
-3
lines changed

5 files changed

+147
-3
lines changed

bin/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
/chapter06trees/
77
/chapter07priorityqueues/
88
/chapter08disjointsets/
9-
/chapter09graphs/
109
/chapter10sorting/
1110
/chapter11searching/
1211
/chapter12selectionalgorithms/
-6 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*Copyright (c) Feb 21, 2016 CareerMonk Publications and others.
2+
* E-Mail : [email protected]
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : CommonElementsInSortedLinkedLists.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 chapter03linkedlists;
16+
17+
public class CommonElementsInSortedLinkedLists {
18+
public static ListNode commonElement(ListNode list1, ListNode list2) {
19+
ListNode temp = new ListNode(0);
20+
ListNode head = temp;
21+
while (list1 != null && list2 != null) {
22+
if (list1.data == list2.data) {
23+
head.next = new ListNode(list1.data); // Copy common element.
24+
list1 = list1.next;
25+
list2 = list2.next;
26+
head = head.next;
27+
} else if (list1.data > list2.data) {
28+
list2 = list2.next;
29+
} else { // list1.data < list2.data
30+
list1 = list1.next;
31+
}
32+
}
33+
return temp.next;
34+
}
35+
}

src/chapter03linkedlists/DoublyLinkedList.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public int length() {
5757

5858
// Add a new value to the front of the list.
5959
public void insert(int newValue) {
60-
DLLNode newNode = new DLLNode(newValue,head,head.getNext());
60+
DLLNode newNode = new DLLNode(newValue,null,head.getNext());
6161
newNode.getNext().setPrev(newNode);
62-
head.setNext(newNode);
62+
head = newNode;
6363
length += 1;
6464
}
6565

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*Copyright (c) Jan 10, 2016 CareerMonk Publications and others.
2+
* E-Mail : [email protected]
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : RomanToDecimal.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 chapter21miscconcepts;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
public class RomanToDecimal {
21+
public static int romanToDecimal(java.lang.String romanNumber) {
22+
int decimal = 0;
23+
int lastNumber = 0;
24+
if(romanNumber.isEmpty())
25+
return 0;
26+
String romanNumeral = romanNumber.toUpperCase();
27+
28+
/* operation to be performed on upper cases even if user enters Roman values in lower case chars */
29+
for (int x = romanNumeral.length() - 1; x >= 0 ; x--) {
30+
char convertToDecimal = romanNumeral.charAt(x);
31+
32+
switch (convertToDecimal) {
33+
case 'M':
34+
decimal = processDecimal(1000, lastNumber, decimal);
35+
lastNumber = 1000;
36+
break;
37+
38+
case 'D':
39+
decimal = processDecimal(500, lastNumber, decimal);
40+
lastNumber = 500;
41+
break;
42+
43+
case 'C':
44+
decimal = processDecimal(100, lastNumber, decimal);
45+
lastNumber = 100;
46+
break;
47+
48+
case 'L':
49+
decimal = processDecimal(50, lastNumber, decimal);
50+
lastNumber = 50;
51+
break;
52+
53+
case 'X':
54+
decimal = processDecimal(10, lastNumber, decimal);
55+
lastNumber = 10;
56+
break;
57+
58+
case 'V':
59+
decimal = processDecimal(5, lastNumber, decimal);
60+
lastNumber = 5;
61+
break;
62+
63+
case 'I':
64+
decimal = processDecimal(1, lastNumber, decimal);
65+
lastNumber = 1;
66+
break;
67+
68+
default:
69+
return -1;
70+
71+
}
72+
}
73+
74+
return decimal;
75+
}
76+
77+
public static int processDecimal(int decimal, int lastNumber, int lastDecimal) {
78+
if (lastNumber > decimal) {
79+
return lastDecimal - decimal;
80+
} else {
81+
return lastDecimal + decimal;
82+
}
83+
}
84+
85+
public static void main(java.lang.String args[]) {
86+
List<String> romanList = new ArrayList<String>();
87+
romanList.add("");
88+
romanList.add("MMDCDXLIV");
89+
romanList.add("MMLVII");
90+
romanList.add("100");
91+
romanList.add("MMDCDXLV");
92+
romanList.add("VIV");
93+
romanList.add("MIX");
94+
romanList.add("LID");
95+
romanList.add("DIX");
96+
romanList.add("LICX");
97+
romanList.add("CLIX");
98+
romanList.add("MMDLIV");
99+
romanList.add("MMXDLIV");
100+
romanList.add("MCMXCX");
101+
romanList.add("CCCXCIX5");
102+
for (int i=0; i<romanList.size();i++){
103+
int decimalValue = romanToDecimal(romanList.get(i));
104+
if (decimalValue == -1)
105+
System.out.println("Not a valid Roman Number.");
106+
else
107+
System.out.println(decimalValue);
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)