Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff0c7fd

Browse files
Add files via upload
1 parent 3722610 commit ff0c7fd

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/* Ignore this file: Instructor Code (For Testing) */
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import com.udemy.ucp.*;
6+
7+
public class Evaluate {
8+
// Test 6 of the following functionalities using just one simple LL [1,2,3,4,5,6]
9+
// Print relavant output
10+
// 1. Test insertion at (1.1) Beginning, (1.2) middle, and (1.3) at last positions.
11+
@Test
12+
public void test_insert_beginning_1(){
13+
SinglyLinkedList list1 = new SinglyLinkedList();
14+
// Test Insertion
15+
list1.insertFirst(10);
16+
System.out.println("Linked List:");
17+
list1.displayList();
18+
assertEquals(10, list1.getFirst().data);
19+
}
20+
@Test
21+
public void test_insert_beginning_2(){
22+
int[] input = new int[]{1,2,3,4,5,6};
23+
SinglyLinkedList list1 = new SinglyLinkedList();
24+
for(int i=0; i<input.length; i++) {
25+
list1.insertLast(input[i]);
26+
}
27+
System.out.println("Linked List Input:");
28+
list1.displayList();
29+
// Test Insertion
30+
list1.insertFirst(10);
31+
System.out.println("Linked List after Insertion:");
32+
list1.displayList();
33+
Node testFirst = list1.getFirst();
34+
assertEquals(10, testFirst.data);
35+
}
36+
@Test
37+
public void test_insert_middle_1(){
38+
int[] input = new int[]{10,5};
39+
SinglyLinkedList list1 = new SinglyLinkedList();
40+
for(int i=0; i<input.length; i++) {
41+
list1.insertLast(input[i]);
42+
}
43+
System.out.println("Linked List Input:");
44+
list1.displayList();
45+
// Test Insertion
46+
list1.insertAfterPosition(8, 1);
47+
System.out.println("Linked List after Insertion:");
48+
list1.displayList();
49+
Node currentNode = list1.getFirst();
50+
currentNode = currentNode.next;
51+
assertEquals(8, currentNode.data);
52+
}
53+
@Test
54+
public void test_insert_middle_2(){
55+
int[] input = new int[]{1,2,3,4,5,6};
56+
SinglyLinkedList list1 = new SinglyLinkedList();
57+
for(int i=0; i<input.length; i++) {
58+
list1.insertLast(input[i]);
59+
}
60+
System.out.println("Linked List Input:");
61+
list1.displayList();
62+
// Test Insertion
63+
list1.insertAfterPosition(100, 3);
64+
System.out.println("Linked List after Insertion:");
65+
list1.displayList();
66+
Node currentNode = list1.getFirst();
67+
int counter = 1;
68+
while(counter<4) {
69+
currentNode = currentNode.next;
70+
counter++;
71+
}
72+
assertEquals(100, currentNode.data);
73+
}
74+
@Test
75+
public void test_insert_end(){
76+
int[] input = new int[]{1,2,3,4,5,6};
77+
SinglyLinkedList list1 = new SinglyLinkedList();
78+
for(int i=0; i<input.length; i++) {
79+
list1.insertLast(input[i]);
80+
}
81+
System.out.println("Linked List Input:");
82+
list1.displayList();
83+
// Test Insertion
84+
list1.insertLast(37);
85+
Node testLast = list1.getLast();
86+
assertEquals(37, testLast.data);
87+
System.out.println("Linked List after Insertion:");
88+
list1.displayList();
89+
}
90+
// 2. Test (2.1) first & (2.2) last property getters
91+
// 2.1 Test if first is pointing to first node using first_getter()
92+
// 2.2 Test if last is pointing to last node using last_getter() and a while loop
93+
@Test
94+
public void test_first_getter(){
95+
int[] input = new int[]{1,2,3,4,5,6};
96+
SinglyLinkedList list1 = new SinglyLinkedList();
97+
for(int i=0; i<input.length; i++) {
98+
list1.insertLast(input[i]);
99+
}
100+
assertEquals(input[0], list1.getFirst().data);
101+
System.out.println("Linked List Input:");
102+
list1.displayList();
103+
System.out.println("First Node Data:" + list1.getFirst().data);
104+
}
105+
106+
@Test
107+
public void test_last_getter(){
108+
int[] input = new int[]{1,2,3,4,5,6};
109+
SinglyLinkedList list1 = new SinglyLinkedList();
110+
for(int i=0; i<input.length; i++) {
111+
list1.insertLast(input[i]);
112+
}
113+
assertEquals(input[input.length-1], list1.getLast().data);
114+
System.out.println("Linked List Input:");
115+
list1.displayList();
116+
System.out.println("Last Node Data:" + list1.getLast().data);
117+
}
118+
@Test
119+
public void test_delete_first(){
120+
int[] input = new int[]{10,8,5};
121+
SinglyLinkedList list1 = new SinglyLinkedList();
122+
for(int i=0; i<input.length; i++) {
123+
list1.insertLast(input[i]);
124+
}
125+
System.out.println("Linked List Input:");
126+
list1.displayList();
127+
// Test Deletion
128+
list1.deleteFirst();
129+
System.out.println("Linked List after Deletion:");
130+
list1.displayList();
131+
Node currentNode = list1.getFirst();
132+
assertEquals(input[1], list1.getFirst().data);
133+
}
134+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class Node {
2+
public int data;
3+
public Node next;
4+
5+
public void displayNode(){
6+
System.out.println("{ "+ data + " } ");
7+
}
8+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
public class SinglyLinkedList {
2+
private Node first;
3+
private Node last;
4+
5+
public SinglyLinkedList() {
6+
first = null;
7+
last = null;
8+
}
9+
10+
// Getter for first property
11+
public Node getFirst() {
12+
return first;
13+
}
14+
15+
// Getter for last property
16+
public Node getLast() {
17+
return last;
18+
}
19+
20+
public boolean isEmpty() {
21+
return (first == null);
22+
}
23+
24+
// used to insert at the beginning of the list
25+
public void insertFirst(int data) {
26+
Node newNode = new Node();
27+
newNode.data = data;
28+
if(isEmpty() == true){
29+
last = newNode;
30+
}
31+
newNode.next = first;
32+
first = newNode;
33+
}
34+
35+
public void insertLast(int data) {
36+
Node newNode = new Node();
37+
newNode.data = data;
38+
if(isEmpty() == true){
39+
first = newNode;
40+
last = newNode;
41+
} else {
42+
last.next = newNode; // the next value of the last node will point to the new node
43+
last = newNode; // we make the new node we created be the last one in the list
44+
}
45+
}
46+
47+
public void insertAfterPosition(int data, int position) {
48+
Node currentFirstNode = first;
49+
int counter = 1;
50+
while (currentFirstNode != null) {
51+
if (position == counter) {
52+
Node newNode = new Node();
53+
newNode.data = data;
54+
newNode.next = currentFirstNode.next;
55+
currentFirstNode.next = newNode;
56+
break;
57+
}
58+
else {
59+
currentFirstNode = currentFirstNode.next;
60+
counter++;
61+
}
62+
}
63+
}
64+
65+
public int deleteFirst() {
66+
int temp = first.data;
67+
if(first.next == null){
68+
last = null;
69+
}
70+
first = first.next;
71+
return temp;
72+
}
73+
74+
public void displayList() {
75+
System.out.println("List (first --> last) ");
76+
Node current = first;
77+
while (current != null) {
78+
current.displayNode();
79+
current = current.next;
80+
}
81+
System.out.println();
82+
}
83+
}
84+
85+
86+
87+
/* Credits:
88+
// In this file, the method insertAfterPosition() was contributed by Arijit Basuin from Q&A Section
89+
// Q&A Thread: https://www.udemy.com/course/practical-data-structures-algorithms-in-java/learn/lecture/4784644#questions/1724238
90+
// Arijit Basu: https://www.udemy.com/user/arijitbasu4/
91+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /