-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
112 lines (89 loc) · 3.44 KB
/
Main.java
File metadata and controls
112 lines (89 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// adapted from: http://www.makeinjava.com/insert-elementnode-sorted-singly-linked-list-java-example-algorithm/
// abstracted template example at: http://www.java2novice.com/data-structures-in-java/linked-list/singly-linked-list/
// tested at: https://www.onlinegdb.com/edit/B1ftX1H2f
/*
* An element (node) in a list of singly-linked objects that store integers
*/
class ListNode {
public int data; // integer value
public ListNode next = null; // single link to next element in list
public ListNode(int num) {
this.data = num;
}
}
/*
* A list of singly-linked objects that illustrates sorted insertion
* of integer values by printing the list on insert
*/
class SortedSinglyLinkedList {
ListNode head = null; // initially, the list is empty
public void insert(ListNode newNode) {
int insertedValue = newNode.data; // save for debug print output
// if list is empty or newNode is smaller than head (special cases)
if (null == this.head || newNode.data < head.data) {
if (null != this.head)
{
newNode.next = this.head; // keep our head when replacing it
}
this.head = newNode; // assign first node to head
this.print(insertedValue);
return; // no sorting required, so skip the rest of the method
}
// if we get here, list is not empty and new node is bigger than head
ListNode temp = null;
ListNode currentNode = head; // start at the beginning
// loop past smaller values until we run out of nodes
while (currentNode != null && currentNode.data < newNode.data) {
temp = currentNode; // save current node
currentNode = currentNode.next; // iterate to next node
}
newNode.next = temp.next; // copy old node's next to new node's next
temp.next = newNode; // redirect old node's next to point at new node
this.print(insertedValue); // print the list after the value has been inserted
}
public void print(int insertedValue) {
System.out.printf("\n\nSingle linked list after inserting %d is: \nhead -->", insertedValue);
ListNode temp = this.head;
while (temp != null) {
System.out.printf(" data(%d) --> ", temp.data);
if (null != temp.next)
{
System.out.printf("next(%d),", temp.next.data);
}
else
{
System.out.printf("next(null),");
}
temp = temp.next;
}
}
}
/*
* The main Java program entry-point of execution
* to run tests against the SortedSinglyLinkedList
*/
public class Main {
public static void main(String[] args) {
//int[] testData = { 1, 3, 5, 9 }; // sort of cheating to pre-sort, no?
int[] testData = { 5, 3, 9, 4 }; // start testing with an unsorted array
SortedSinglyLinkedList testList = new SortedSinglyLinkedList();
for (int count = 0; count < testData.length; count++) {
testList.insert(new ListNode(testData[count]));
}
int newData = 7; // test adding an intermediate value
ListNode newNode = new ListNode(newData);
testList.insert(newNode);
newData = 1; // test adding a smaller value
newNode = new ListNode(newData);
testList.insert(newNode);
newData = 10; // test adding a bigger value
newNode = new ListNode(newData);
testList.insert(newNode);
newData = -1; // test adding a negative value
newNode = new ListNode(newData);
testList.insert(newNode);
newData = 0; // test adding zero
newNode = new ListNode(newData);
testList.insert(newNode);
}
}