Skip to content

Commit e1dc09f

Browse files
committed
Ch. 16 practice problems
1 parent 022ec37 commit e1dc09f

File tree

8 files changed

+304
-0
lines changed

8 files changed

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

0 commit comments

Comments
 (0)