Skip to content

Commit 1701891

Browse files
authored
Merge pull request #20 from code4tomorrow/neha-practice
Ch. 16 practice problems
2 parents 4cea04f + 38d5303 commit 1701891

File tree

30 files changed

+335
-0
lines changed

30 files changed

+335
-0
lines changed

src/com/codefortomorrow/advanced/chapter14/solutions/Bank.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static void withdraw(BankAccount account, int amount) {
5252
}
5353

5454
class BankAccount {
55+
5556
private int balance;
5657

5758
public BankAccount(int balance) {

src/com/codefortomorrow/advanced/chapter15/solutions/MeasurableProblem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ interface Measurable {
4040
}
4141

4242
class Student implements Measurable {
43+
4344
private double GPA;
4445
private String name;
4546

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.codefortomorrow.advanced.chapter16.practice;
2+
/*
3+
Using the methods outlined in the “Benefits of LinkedLists” section,
4+
fill both a LinkedList and an ArrayList with 10,000 elements.
5+
Then, find the time it takes to access the last element of each list.
6+
7+
Determine which type of list can access elements more efficiently.
8+
9+
Hint: You can use the get(int position) method
10+
to access the element at a given position of a list.
11+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codefortomorrow.advanced.chapter16.practice;
2+
/*
3+
Create a LinkedList of Strings, and ask the user to
4+
input 10 names one by one into the list.
5+
6+
Output this list.
7+
8+
Remove each name that has 5 or fewer letters, and output the new list.
9+
10+
Add the word “Apple” to the beginning of the list.
11+
12+
Add the word “Peanut” to the second-to-last position of the list.
13+
14+
(Hint: Make sure to take advantage of LinkedList methods!)
15+
16+
Output the new list.
17+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.codefortomorrow.advanced.chapter16.practice;
2+
/*
3+
Adding an insert method to a SinglyLinkedList
4+
(details in SinglyLinkedList DIY section).
5+
6+
Hint: It might be necessary to have a separate case for
7+
inserting at position 0, because that’s when you want to
8+
insert BEFORE the head. For all other positions (1, 2, etc.),
9+
you can simply traverse down the list, keeping track of the
10+
current node, and when you reach the position you want,
11+
add the node AFTER the current node.
12+
13+
Alternatively, you can always add BEFORE the current node
14+
for all position cases (insert before the head to place the
15+
new node at pos 0, insert before node 1 to place the new
16+
node at pos 1, etc.), but then your exception case would
17+
be when the user wants to insert a node at the very end of the list.
18+
In this case, you’d have to write separate code to insert AFTER
19+
the last node.
20+
21+
Make sure you have a toString() method defined!
22+
23+
The following code should output “{2, 3, 4, 7}”:
24+
SinglyLinkedList lst = new SinglyLinkedList();
25+
lst.add(3);
26+
lst.add(7);
27+
lst.insert(4, 1);
28+
lst.insert(2, 0);
29+
System.out.println(lst);
30+
*/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codefortomorrow.advanced.chapter16.practice;
2+
/*
3+
Write a method which takes in a LinkedList of Strings
4+
and rearranges the elements (without creating a new list)
5+
so it is sorted in ascending order of String length.
6+
Don’t use any pre-existing sort methods… write your own. :)
7+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.codefortomorrow.advanced.chapter16.solutions;
2+
3+
import java.util.*;
4+
5+
/*
6+
Using the methods outlined in the “Benefits of LinkedLists” section,
7+
fill both a LinkedList and an ArrayList with 10,000 elements.
8+
Then, find the time it takes to access the last element of each list.
9+
10+
Determine which type of list can access elements more efficiently.
11+
12+
Hint: You can use the get(int position) method
13+
to access the element at a given position of a list.
14+
*/
15+
16+
public class Access {
17+
18+
public static void main(String[] args) {
19+
long a, b;
20+
21+
// Define lists with 10,000 elements.
22+
LinkedList<Integer> list1 = new LinkedList<Integer>();
23+
for (int i = 0; i < 10000; i++) list1.add(1);
24+
25+
ArrayList<Integer> list2 = new ArrayList<Integer>();
26+
for (int i = 0; i < 10000; i++) list2.add(1);
27+
28+
// Get time for each operation and output the difference.
29+
a = System.nanoTime();
30+
int c = list1.get(9999);
31+
b = System.nanoTime();
32+
System.out.println("\nLinked: " + (b - a) + " ns");
33+
34+
a = System.nanoTime();
35+
int d = list2.get(9999);
36+
b = System.nanoTime();
37+
System.out.println("ArrayList: " + (b - a) + " ns");
38+
// Note that the ArrayList takes less time.
39+
}
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.codefortomorrow.advanced.chapter16.solutions;
2+
3+
import java.util.*;
4+
5+
/*
6+
Create a LinkedList of Strings, and ask the user to
7+
input 10 names one by one into the list.
8+
9+
Output this list.
10+
11+
Remove each name that has 5 or fewer letters, and output the new list.
12+
13+
Add the word “Apple” to the beginning of the list.
14+
15+
Add the word “Peanut” to the second-to-last position of the list.
16+
17+
(Hint: Make sure to take advantage of LinkedList methods!)
18+
19+
Output the new list.
20+
*/
21+
22+
public class Names {
23+
24+
public static void main(String[] args) {
25+
LinkedList<String> list = new LinkedList<String>();
26+
Scanner sc = new Scanner(System.in);
27+
for (int i = 0; i < 10; i++) {
28+
System.out.print("Enter name " + (i + 1) + ": ");
29+
list.add(sc.nextLine());
30+
}
31+
System.out.println(list);
32+
33+
for (int i = 0; i < list.size(); i++) if (
34+
list.get(i).length() <= 5
35+
) list.remove(i--);
36+
System.out.println(list);
37+
38+
list.add(0, "Apple");
39+
list.add(list.size() - 1, "Peanut");
40+
System.out.println(list);
41+
}
42+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.codefortomorrow.advanced.chapter16.solutions;
2+
3+
import java.util.*;
4+
5+
/*
6+
Adding an insert method to a SinglyLinkedList
7+
(details in SinglyLinkedList DIY section).
8+
9+
Hint: It might be necessary to have a separate case for
10+
inserting at position 0, because that’s when you want to
11+
insert BEFORE the head. For all other positions (1, 2, etc.),
12+
you can simply traverse down the list, keeping track of the
13+
current node, and when you reach the position you want,
14+
add the node AFTER the current node.
15+
16+
Alternatively, you can always add BEFORE the current node
17+
for all position cases (insert before the head to place the
18+
new node at pos 0, insert before node 1 to place the new
19+
node at pos 1, etc.), but then your exception case would
20+
be when the user wants to insert a node at the very end of the list.
21+
In this case, you’d have to write separate code to insert AFTER
22+
the last node.
23+
24+
Make sure you have a toString() method defined!
25+
26+
The following code should output “{2, 3, 4, 7}”:
27+
SinglyLinkedList lst = new SinglyLinkedList();
28+
lst.add(3);
29+
lst.add(7);
30+
lst.insert(4, 1);
31+
lst.insert(2, 0);
32+
System.out.println(lst);
33+
*/
34+
35+
// NOTE: You need to make a separate class with a main()
36+
// method to use this List object. This is simply the object
37+
// definition.
38+
39+
public class SinglyLinkedListInsert {
40+
41+
Node head;
42+
43+
// add() method from SinglyLinkedList DIY section, adds to end of list
44+
public void add(int data) {
45+
// Create a new node with given data
46+
Node new_node = new Node(data);
47+
48+
// If the Linked List is empty,
49+
// then make the new node as head
50+
if (head == null) {
51+
head = new_node;
52+
} else {
53+
// Else traverse till the last node
54+
// and insert the new_node there
55+
Node last = head;
56+
while (last.next != null) {
57+
last = last.next;
58+
}
59+
60+
// Insert the new_node at last node
61+
last.next = new_node;
62+
}
63+
}
64+
65+
public void insert(int data, int pos) {
66+
// Verify valid pos number
67+
if (pos < 0) System.out.print("Invalid position");
68+
69+
// Create new node and keep track of current node
70+
Node new_node = new Node(data);
71+
Node current = head;
72+
73+
// If pos is 0, set new node as head
74+
if (pos == 0) {
75+
new_node.next = head;
76+
head = new_node;
77+
} else {
78+
while (pos-- >= 0) {
79+
if (pos == 0) {
80+
// Add new node after current position
81+
new_node.next = current.next;
82+
current.next = new_node;
83+
break;
84+
}
85+
// Check if pos is out of bounds (reaches end)
86+
if (current.next == null) {
87+
System.out.println("Out of bounds");
88+
break;
89+
}
90+
current = current.next;
91+
}
92+
}
93+
}
94+
95+
@Override
96+
public String toString() {
97+
// For empty lists
98+
if (head == null) return "{}";
99+
100+
String str = "{";
101+
102+
// Traverse the list until the last node
103+
Node current = head;
104+
while (current.next != null) {
105+
str += "" + current.data + ", ";
106+
current = current.next;
107+
}
108+
str += current.data + "}";
109+
return str;
110+
}
111+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.codefortomorrow.advanced.chapter16.solutions;
2+
3+
import java.util.*;
4+
5+
/*
6+
Write a method which takes in a LinkedList of Strings
7+
and rearranges the elements (without creating a new list)
8+
so it is sorted in ascending order of String length.
9+
Don’t use any pre-existing sort methods… write your own. :)
10+
*/
11+
12+
public class Sort {
13+
14+
public static void main(String[] args) {
15+
LinkedList<String> list = new LinkedList<String>();
16+
list.addAll(
17+
Arrays.asList(
18+
"Jeannette",
19+
"Sophia",
20+
"Computer",
21+
"Boots",
22+
"Lava",
23+
"Lasagna",
24+
"Dirty",
25+
"Cleanie"
26+
)
27+
);
28+
sort(list);
29+
System.out.println(list);
30+
}
31+
32+
public static void sort(LinkedList<String> list) {
33+
if (list.size() < 2) return;
34+
for (int i = 1; i < list.size(); i++) {
35+
String current = list.get(i);
36+
// Check if current word is smaller than previous word
37+
if (current.length() < list.get(i - 1).length()) {
38+
// Add current word before the node with
39+
// same or greater word size
40+
for (int j = 0; j < i; j++) {
41+
if (list.get(j).length() >= current.length()) {
42+
list.add(j, current);
43+
// Remove duplicate current
44+
list.remove(i + 1);
45+
break;
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)