Skip to content

Commit f348e18

Browse files
authored
Update SinglyLinkedList.java
1 parent b123975 commit f348e18

File tree

1 file changed

+110
-126
lines changed

1 file changed

+110
-126
lines changed
+110-126
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,149 @@
11
/**
22
* This class implements a SinglyLinked List. This is done
33
* using SinglyLinkedList class and a LinkForLinkedList Class.
4-
*
4+
* <p>
55
* A linked list is similar to an array, it hold values.
66
* However, links in a linked list do not have indexes. With
77
* a linked list you do not need to predetermine it's size as
88
* it grows and shrinks as it is edited. This is an example of
99
* a singly linked list. Elements can only be added/removed
1010
* at the head/front of the list.
1111
*
12-
* @author Unknown
13-
*
12+
* @author yanglbme
1413
*/
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-
Node.indexCount++; //Count the all indexes of inserted values
36-
}
14+
class SinglyLinkedList {
15+
/**
16+
* Head refer to the front of the list
17+
*/
18+
private Node head;
19+
3720
/**
38-
* Insert values at spesific position
39-
* @param number inserted value
40-
* @param position spesific position of inserted value
21+
* Count of nodes
4122
*/
42-
public void addToSpecifiedPosition(int number, int position) {
43-
InsertNth(head, number, position);
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;
4435
}
4536

46-
/**
37+
/**
4738
* Inserts a new node at a specified position
48-
* @param head head node of the linked list
39+
*
4940
* @param data data to be stored in a new node
5041
* @param position position at which a new node is to be inserted
51-
* @return reference of the head of the linked list
5242
*/
5343

54-
Node InsertNth(Node head, int data, int position) {
55-
56-
Node newNode = new Node(data);
57-
Node current = head;
58-
int temp = position - Node.getIndexCount();
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");
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+
}
5959

60-
while (temp-- > 0) {
61-
insertHead(0);
62-
System.out.println("Do something " + Node.indexCount);
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!");
6368
}
6469

65-
newNode.next = current;
66-
head = newNode;
67-
insertHead(newNode.value);
68-
return head;
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;
6983
}
7084

71-
/**
72-
* This method deletes an element at the head
73-
*
74-
* @return The element deleted
75-
*/
76-
public Node deleteHead(){
77-
Node temp = head;
78-
head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head
79-
--Node.indexCount;
80-
return temp;
81-
}
82-
83-
/**
84-
* Checks if the list is empty
85-
*
86-
* @return true is list is empty
87-
*/
88-
public boolean isEmpty(){
89-
return(head == null);
90-
}
91-
92-
/**
93-
* Prints contents of the list
94-
*/
95-
public void display(){
96-
Node current = head;
97-
while(current!=null){
98-
System.out.print(current.getValue()+" ");
99-
current = current.next;
100-
}
101-
System.out.println();
102-
}
103-
104-
/**
105-
* Main method
106-
*
107-
* @param args Command line arguments
108-
*/
109-
public static void main(String args[]){
110-
SinglyLinkedList myList = new SinglyLinkedList();
111-
112-
System.out.println(myList.isEmpty()); //Will print true
113-
114-
myList.insertHead(5);
115-
myList.insertHead(7);
116-
myList.insertHead(10);
117-
118-
myList.display(); // 10(head) --> 7 --> 5
119-
120-
myList.deleteHead();
121-
122-
myList.display(); // 7(head) --> 5
123-
}
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 + " ");
92+
current = current.next;
93+
}
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
120+
}
124121
}
125122

126123
/**
127124
* This class is the nodes of the SinglyLinked List.
128125
* They consist of a value and a pointer to the node
129126
* after them.
130127
*
131-
* @author Unknown
132-
*
128+
* @author yanglbme
133129
*/
134-
class Node{
135-
/** The value of the node */
136-
public int value;
130+
class Node {
137131
/**
138-
* The count of Indexes
132+
* The value of the node
139133
*/
140-
public static int indexCount;
141-
/** Point to the next node */
142-
public Node next; //This is what the link will point to
143-
144-
/**
145-
* Constructor
146-
*
147-
* @param valuein Value to be put in the node
148-
*/
149-
public Node(int valuein){
150-
value = valuein;
151-
}
152-
153-
/**
154-
* Returns value of the node
155-
*/
156-
public int getValue(){
157-
return value;
158-
}
134+
int value;
135+
136+
/**
137+
* Point to the next node
138+
*/
139+
Node next;
140+
159141
/**
160-
* @return the count of indexes
142+
* Constructor
143+
*
144+
* @param value Value to be put in the node
161145
*/
162-
public static int getIndexCount() {
163-
return indexCount;
146+
Node(int value) {
147+
this.value = value;
164148
}
165149
}

0 commit comments

Comments
 (0)