|
1 | 1 | /** |
2 | 2 | * This class implements a SinglyLinked List. This is done |
3 | 3 | * using SinglyLinkedList class and a LinkForLinkedList Class. |
4 | | - * |
| 4 | + * <p> |
5 | 5 | * A linked list is similar to an array, it hold values. |
6 | 6 | * However, links in a linked list do not have indexes. With |
7 | 7 | * a linked list you do not need to predetermine it's size as |
8 | 8 | * it grows and shrinks as it is edited. This is an example of |
9 | 9 | * a singly linked list. Elements can only be added/removed |
10 | 10 | * at the head/front of the list. |
11 | | - * |
12 | | - * @author Unknown |
13 | 11 | * |
| 12 | + * @author yanglbme |
14 | 13 | */ |
15 | | -class SinglyLinkedList{ |
16 | | - /**Head refered to the front of the list */ |
17 | | - private Node head; |
18 | | - |
19 | | - /** |
20 | | - * Constructor of SinglyLinkedList |
21 | | - */ |
22 | | - public SinglyLinkedList(){ |
23 | | - head = null; |
24 | | - } |
25 | | - |
26 | | - /** |
27 | | - * This method inserts an element at the head |
28 | | - * |
29 | | - * @param x Element to be added |
30 | | - */ |
31 | | - public void insertHead(int x){ |
32 | | - Node newNode = new Node(x); //Create a new link with a value attached to it |
33 | | - newNode.next = head; //Set the new link to point to the current head |
34 | | - head = newNode; //Now set the new link to be the head |
35 | | - } |
36 | | - |
37 | | - |
38 | | - /** |
| 14 | +class SinglyLinkedList { |
| 15 | + /** |
| 16 | + * Head refer to the front of the list |
| 17 | + */ |
| 18 | + private Node head; |
| 19 | + |
| 20 | + /** |
| 21 | + * Count of nodes |
| 22 | + */ |
| 23 | + private int count; |
| 24 | + |
| 25 | + /** |
| 26 | + * This method inserts an element at the head |
| 27 | + * |
| 28 | + * @param x Element to be added |
| 29 | + */ |
| 30 | + public void insertHead(int x) { |
| 31 | + Node newNode = new Node(x); |
| 32 | + newNode.next = head; |
| 33 | + head = newNode; |
| 34 | + ++count; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
39 | 38 | * Inserts a new node at a specified position |
40 | | - * @param head head node of the linked list |
| 39 | + * |
41 | 40 | * @param data data to be stored in a new node |
42 | 41 | * @param position position at which a new node is to be inserted |
43 | | - * @return reference of the head of the linked list |
44 | 42 | */ |
45 | 43 |
|
46 | | - Node InsertNth(Node head, int data, int position) { |
47 | | - |
48 | | - Node newNode = new Node(); |
49 | | - newNode.data = data; |
50 | | - |
51 | | - if (position == 0) { |
52 | | - newNode.next = head; |
53 | | - return newNode; |
| 44 | + public void insertNth(int data, int position) { |
| 45 | + if (position < 0 || position > count) { |
| 46 | + throw new RuntimeException("position less than zero or position more than the count of list"); |
54 | 47 | } |
| 48 | + Node node = new Node(data); |
| 49 | + Node dummy = new Node(-1); |
| 50 | + dummy.next = head; |
| 51 | + Node cur = dummy; |
| 52 | + for (int i = 0; i < position; ++i) { |
| 53 | + cur = cur.next; |
| 54 | + } |
| 55 | + node.next = cur.next; |
| 56 | + cur.next = node; |
| 57 | + ++count; |
| 58 | + } |
55 | 59 |
|
56 | | - Node current = head; |
| 60 | + /** |
| 61 | + * This method deletes an element at the head |
| 62 | + * |
| 63 | + * @return The element deleted |
| 64 | + */ |
| 65 | + public Node deleteHead() { |
| 66 | + if (isEmpty()) { |
| 67 | + throw new RuntimeException("The list is empty!"); |
| 68 | + } |
| 69 | + |
| 70 | + Node temp = head; |
| 71 | + head = head.next; |
| 72 | + --count; |
| 73 | + return temp; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks if the list is empty |
| 78 | + * |
| 79 | + * @return true is list is empty |
| 80 | + */ |
| 81 | + public boolean isEmpty() { |
| 82 | + return count == 0; |
| 83 | + } |
57 | 84 |
|
58 | | - while (--position > 0) { |
| 85 | + /** |
| 86 | + * Prints contents of the list |
| 87 | + */ |
| 88 | + public void display() { |
| 89 | + Node current = head; |
| 90 | + while (current != null) { |
| 91 | + System.out.print(current.value + " "); |
59 | 92 | current = current.next; |
60 | 93 | } |
61 | | - |
62 | | - newNode.next = current.next; |
63 | | - current.next = newNode; |
64 | | - return head; |
| 94 | + System.out.println(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Main method |
| 99 | + * |
| 100 | + * @param args Command line arguments |
| 101 | + */ |
| 102 | + public static void main(String args[]) { |
| 103 | + SinglyLinkedList myList = new SinglyLinkedList(); |
| 104 | + |
| 105 | + assert myList.isEmpty(); |
| 106 | + |
| 107 | + myList.insertHead(5); |
| 108 | + myList.insertHead(7); |
| 109 | + myList.insertHead(10); |
| 110 | + |
| 111 | + myList.display(); // 10 -> 7 -> 5 |
| 112 | + |
| 113 | + myList.deleteHead(); |
| 114 | + |
| 115 | + myList.display(); // 7 -> 5 |
| 116 | + |
| 117 | + myList.insertNth(11, 2); |
| 118 | + |
| 119 | + myList.display(); // 7 -> 5 -> 11 |
65 | 120 | } |
66 | | - |
67 | | - /** |
68 | | - * This method deletes an element at the head |
69 | | - * |
70 | | - * @return The element deleted |
71 | | - */ |
72 | | - public Node deleteHead(){ |
73 | | - Node temp = head; |
74 | | - head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head |
75 | | - return temp; |
76 | | - } |
77 | | - |
78 | | - /** |
79 | | - * Checks if the list is empty |
80 | | - * |
81 | | - * @return true is list is empty |
82 | | - */ |
83 | | - public boolean isEmpty(){ |
84 | | - return(head == null); |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - * Prints contents of the list |
89 | | - */ |
90 | | - public void display(){ |
91 | | - Node current = head; |
92 | | - while(current!=null){ |
93 | | - System.out.print(current.getValue()+" "); |
94 | | - current = current.next; |
95 | | - } |
96 | | - System.out.println(); |
97 | | - } |
98 | | - |
99 | | - /** |
100 | | - * Main method |
101 | | - * |
102 | | - * @param args Command line arguments |
103 | | - */ |
104 | | - public static void main(String args[]){ |
105 | | - SinglyLinkedList myList = new SinglyLinkedList(); |
106 | | - |
107 | | - System.out.println(myList.isEmpty()); //Will print true |
108 | | - |
109 | | - myList.insertHead(5); |
110 | | - myList.insertHead(7); |
111 | | - myList.insertHead(10); |
112 | | - |
113 | | - myList.display(); // 10(head) --> 7 --> 5 |
114 | | - |
115 | | - myList.deleteHead(); |
116 | | - |
117 | | - myList.display(); // 7(head) --> 5 |
118 | | - } |
119 | 121 | } |
120 | 122 |
|
121 | 123 | /** |
122 | 124 | * This class is the nodes of the SinglyLinked List. |
123 | 125 | * They consist of a value and a pointer to the node |
124 | 126 | * after them. |
125 | | - * |
126 | | - * @author Unknown |
127 | 127 | * |
| 128 | + * @author yanglbme |
128 | 129 | */ |
129 | | -class Node{ |
130 | | - /** The value of the node */ |
131 | | - public int value; |
132 | | - /** Point to the next node */ |
133 | | - public Node next; //This is what the link will point to |
134 | | - |
135 | | - /** |
136 | | - * Constructor |
137 | | - * |
138 | | - * @param valuein Value to be put in the node |
139 | | - */ |
140 | | - public Node(int valuein){ |
141 | | - value = valuein; |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * Returns value of the node |
146 | | - */ |
147 | | - public int getValue(){ |
148 | | - return value; |
149 | | - } |
| 130 | +class Node { |
| 131 | + /** |
| 132 | + * The value of the node |
| 133 | + */ |
| 134 | + int value; |
150 | 135 |
|
| 136 | + /** |
| 137 | + * Point to the next node |
| 138 | + */ |
| 139 | + Node next; |
| 140 | + |
| 141 | + /** |
| 142 | + * Constructor |
| 143 | + * |
| 144 | + * @param value Value to be put in the node |
| 145 | + */ |
| 146 | + Node(int value) { |
| 147 | + this.value = value; |
| 148 | + } |
151 | 149 | } |
0 commit comments