Skip to content

update SinglyLinkedList #1069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 53 additions & 41 deletions DataStructures/Lists/SinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,16 @@ public void insert(int data) {
* @param data data to be stored in a new node
* @param position position at which a new node is to be inserted
*/

public void insertNth(int data, int position) {
if (position < 0 || position > getSize()) {
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
} else {
Node cur = head;
Node node = new Node(data);
for (int i = 0; i < position; ++i) {
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
size++;
checkBounds(position, 0, size);
Node cur = head;
for (int i = 0; i < position; ++i) {
cur = cur.next;
}
Node node = new Node(data);
node.next = cur.next;
cur.next = node;
size++;
}

/**
Expand All @@ -90,19 +86,28 @@ public void delete() {
* This method deletes an element at Nth position
*/
public void deleteNth(int position) {
if (position < 0 || position > size - 1) {
throw new IndexOutOfBoundsException("position less than zero or position more than the count of list");
} else {
Node cur = head;
for (int i = 0; i < position; ++i) {
cur = cur.next;
}

Node destroy = cur.next;
cur.next = cur.next.next;
destroy = null; // clear to let GC do its work

size--;
checkBounds(position, 0, size - 1);
Node cur = head;
for (int i = 0; i < position; ++i) {
cur = cur.next;
}

Node destroy = cur.next;
cur.next = cur.next.next;
destroy = null; // clear to let GC do its work

size--;
}

/**
* @param position to check position
* @param low low index
* @param high high index
* @throws IndexOutOfBoundsException if {@code position} not in range {@code low} to {@code high}
*/
public void checkBounds(int position, int low, int high) {
if (position < low || position > high) {
throw new IndexOutOfBoundsException(position + "");
}
}

Expand Down Expand Up @@ -134,25 +139,27 @@ public boolean isEmpty() {
return size == 0;
}

/**
* Prints contents of the list
*/
public void display() {
Node current = head.next;
while (current != null) {
System.out.print(current.value + " ");
current = current.next;
}
System.out.println();
}

/**
* Returns the size of the linked list
*/
public int getSize() {
return size;
}

@Override
public String toString() {
if (size == 0) {
return "";
}
StringBuilder builder = new StringBuilder();
Node cur = head.next;
while (cur != null) {
builder.append(cur.value).append("->");
cur = cur.next;
}
return builder.replace(builder.length() - 2, builder.length(), "").toString();
}

/**
* Main method
*
Expand All @@ -167,19 +174,24 @@ public static void main(String args[]) {
myList.insertHead(7);
myList.insertHead(10);

myList.display(); // 10 -> 7 -> 5
System.out.println(myList);; // 10 -> 7 -> 5

myList.deleteHead();

myList.display(); // 7 -> 5
System.out.println(myList);; // 7 -> 5

myList.insertNth(11, 2);

myList.display(); // 7 -> 5 -> 11
System.out.println(myList);; // 7 -> 5 -> 11

myList.deleteNth(1);

myList.display(); // 7-> 11
System.out.println(myList);; // 7-> 11

myList.clear();
assert myList.isEmpty();

System.out.println(myList); // ""

}
}
Expand Down