Skip to content

Commit b123975

Browse files
added new field and modified some methods
1 parent 6cc1414 commit b123975

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

DataStructures/Lists/SinglyLinkedList.java

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
22
* This class implements a SinglyLinked List. This is done
33
* using SinglyLinkedList class and a LinkForLinkedList Class.
4-
*
4+
*
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.
11-
*
11+
*
1212
* @author Unknown
1313
*
1414
*/
@@ -25,15 +25,23 @@ public SinglyLinkedList(){
2525

2626
/**
2727
* This method inserts an element at the head
28-
*
28+
*
2929
* @param x Element to be added
3030
*/
3131
public void insertHead(int x){
3232
Node newNode = new Node(x); //Create a new link with a value attached to it
3333
newNode.next = head; //Set the new link to point to the current head
3434
head = newNode; //Now set the new link to be the head
35+
Node.indexCount++; //Count the all indexes of inserted values
3536
}
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+
}
3745

3846
/**
3947
* Inserts a new node at a specified position
@@ -44,40 +52,37 @@ public void insertHead(int x){
4452
*/
4553

4654
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-
}
5555

56+
Node newNode = new Node(data);
5657
Node current = head;
58+
int temp = position - Node.getIndexCount();
5759

58-
while (--position > 0) {
59-
current = current.next;
60+
while (temp-- > 0) {
61+
insertHead(0);
62+
System.out.println("Do something " + Node.indexCount);
6063
}
61-
62-
newNode.next = current.next;
63-
current.next = newNode;
64+
65+
newNode.next = current;
66+
head = newNode;
67+
insertHead(newNode.value);
6468
return head;
6569
}
66-
70+
6771
/**
6872
* This method deletes an element at the head
69-
*
73+
*
7074
* @return The element deleted
7175
*/
7276
public Node deleteHead(){
7377
Node temp = head;
7478
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;
7580
return temp;
7681
}
7782

7883
/**
7984
* Checks if the list is empty
80-
*
85+
*
8186
* @return true is list is empty
8287
*/
8388
public boolean isEmpty(){
@@ -95,10 +100,10 @@ public void display(){
95100
}
96101
System.out.println();
97102
}
98-
103+
99104
/**
100105
* Main method
101-
*
106+
*
102107
* @param args Command line arguments
103108
*/
104109
public static void main(String args[]){
@@ -122,19 +127,23 @@ public static void main(String args[]){
122127
* This class is the nodes of the SinglyLinked List.
123128
* They consist of a value and a pointer to the node
124129
* after them.
125-
*
130+
*
126131
* @author Unknown
127132
*
128133
*/
129134
class Node{
130135
/** The value of the node */
131136
public int value;
137+
/**
138+
* The count of Indexes
139+
*/
140+
public static int indexCount;
132141
/** Point to the next node */
133142
public Node next; //This is what the link will point to
134143

135144
/**
136145
* Constructor
137-
*
146+
*
138147
* @param valuein Value to be put in the node
139148
*/
140149
public Node(int valuein){
@@ -147,5 +156,10 @@ public Node(int valuein){
147156
public int getValue(){
148157
return value;
149158
}
150-
159+
/**
160+
* @return the count of indexes
161+
*/
162+
public static int getIndexCount() {
163+
return indexCount;
164+
}
151165
}

0 commit comments

Comments
 (0)