1
1
/**
2
2
* This class implements a SinglyLinked List. This is done
3
3
* using SinglyLinkedList class and a LinkForLinkedList Class.
4
- *
4
+ *
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
- *
11
+ *
12
12
* @author Unknown
13
13
*
14
14
*/
@@ -25,15 +25,23 @@ public SinglyLinkedList(){
25
25
26
26
/**
27
27
* This method inserts an element at the head
28
- *
28
+ *
29
29
* @param x Element to be added
30
30
*/
31
31
public void insertHead (int x ){
32
32
Node newNode = new Node (x ); //Create a new link with a value attached to it
33
33
newNode .next = head ; //Set the new link to point to the current head
34
34
head = newNode ; //Now set the new link to be the head
35
+ Node .indexCount ++; //Count the all indexes of inserted values
35
36
}
36
-
37
+ /**
38
+ * Insert values at spesific position
39
+ * @param number inserted value
40
+ * @param position spesific position of inserted value
41
+ */
42
+ public void addToSpecifiedPosition (int number , int position ) {
43
+ InsertNth (head , number , position );
44
+ }
37
45
38
46
/**
39
47
* Inserts a new node at a specified position
@@ -44,40 +52,37 @@ public void insertHead(int x){
44
52
*/
45
53
46
54
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 ;
54
- }
55
55
56
+ Node newNode = new Node (data );
56
57
Node current = head ;
58
+ int temp = position - Node .getIndexCount ();
57
59
58
- while (--position > 0 ) {
59
- current = current .next ;
60
+ while (temp -- > 0 ) {
61
+ insertHead (0 );
62
+ System .out .println ("Do something " + Node .indexCount );
60
63
}
61
-
62
- newNode .next = current .next ;
63
- current .next = newNode ;
64
+
65
+ newNode .next = current ;
66
+ head = newNode ;
67
+ insertHead (newNode .value );
64
68
return head ;
65
69
}
66
-
70
+
67
71
/**
68
72
* This method deletes an element at the head
69
- *
73
+ *
70
74
* @return The element deleted
71
75
*/
72
76
public Node deleteHead (){
73
77
Node temp = head ;
74
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 ;
75
80
return temp ;
76
81
}
77
82
78
83
/**
79
84
* Checks if the list is empty
80
- *
85
+ *
81
86
* @return true is list is empty
82
87
*/
83
88
public boolean isEmpty (){
@@ -95,10 +100,10 @@ public void display(){
95
100
}
96
101
System .out .println ();
97
102
}
98
-
103
+
99
104
/**
100
105
* Main method
101
- *
106
+ *
102
107
* @param args Command line arguments
103
108
*/
104
109
public static void main (String args []){
@@ -122,19 +127,23 @@ public static void main(String args[]){
122
127
* This class is the nodes of the SinglyLinked List.
123
128
* They consist of a value and a pointer to the node
124
129
* after them.
125
- *
130
+ *
126
131
* @author Unknown
127
132
*
128
133
*/
129
134
class Node {
130
135
/** The value of the node */
131
136
public int value ;
137
+ /**
138
+ * The count of Indexes
139
+ */
140
+ public static int indexCount ;
132
141
/** Point to the next node */
133
142
public Node next ; //This is what the link will point to
134
143
135
144
/**
136
145
* Constructor
137
- *
146
+ *
138
147
* @param valuein Value to be put in the node
139
148
*/
140
149
public Node (int valuein ){
@@ -147,5 +156,10 @@ public Node(int valuein){
147
156
public int getValue (){
148
157
return value ;
149
158
}
150
-
159
+ /**
160
+ * @return the count of indexes
161
+ */
162
+ public static int getIndexCount () {
163
+ return indexCount ;
164
+ }
151
165
}
0 commit comments