Skip to content

Commit

Permalink
linked_list.java: Add Linked List in Java
Browse files Browse the repository at this point in the history
  • Loading branch information
rugglcon authored and sangamcse committed Oct 26, 2018
1 parent 8eff91d commit b950e53
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This repository contains examples of various algorithms written on different pro
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | [:octocat:](queue/Cpp) | | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:octocat:](stack/C ) | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | [:octocat:](linked_list/Cpp) | | [:octocat:](linked_list/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | [:octocat:](linked_list/Cpp) | [:octocat:](linked_list/Java) | [:octocat:](linked_list/Python) |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | [:octocat:](avl_tree/Cpp) | [:octocat:](avl_tree/Java) | [:octocat:](avl_tree/Python) |
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | [:octocat:](binary_search_tree/Cpp) | | |
| [Fenwick Tree](https://en.wikipedia.org/wiki/Fenwick_tree) | | | [:octocat:](fenwick_tree/java) | [:octocat:](fenwick_tree/Python) |
Expand Down
107 changes: 107 additions & 0 deletions linked_list/Java/linked_list.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
public class linked_list<T> {
public static void main(String[] args) {
linked_list<String> list = new linked_list<String>("head");
list.add("second");
list.add("third");
list.add("last");
System.out.println(list.length()); // should be 4
System.out.println(list.remove("third")); // should print "third"
System.out.println(list.length()); // should be 3
}

private Node<T> head;

public linked_list(T head) {
this.head = new Node<T>(head);
}

public linked_list() {
}

/**
* Adds a value to the end of the list
*
* @param val value to be added
* @return true if add succeeds; false otherwise
*/
public boolean add(T val) {
Node<T> tmp = this.head;
if (tmp == null) {
this.head = new Node<T>(val);
return true;
}

while (tmp.next() != null) {
tmp = tmp.next();
}

tmp.setNext(new Node<T>(val));
return true;
}

/**
* Returns the value that was removed if the requested value
* was found. Otherwise returns null if the value doesn't exist
* in the list
* @param val
* @return T
*/
public T remove(T val) {
if (this.head != null) {
if (this.head.getValue() == val) {
Node<T> trash = this.head;
this.head = this.head.next();
trash.setNext(null);
return trash.getValue();
}
} else {
return null;
}

Node<T> prev = this.head;
Node<T> tmp = prev.next();
while (tmp != null) {
if (tmp.getValue() == val) {
prev.setNext(tmp.next());
tmp.setNext(null);
return tmp.getValue();
}
tmp = tmp.next();
prev = prev.next();
}

return null;
}

public int length() {
int len = 0;
Node<T> tmp = this.head;
while (tmp != null) {
len++;
tmp = tmp.next();
}

return len;
}

private class Node<T> {
private Node<T> nextNode;
private T value;

public Node(T val) {
this.value = val;
}

public Node<T> next() {
return this.nextNode;
}

public void setNext(Node<T> next) {
this.nextNode = next;
}

public T getValue() {
return this.value;
}
}
}

0 comments on commit b950e53

Please sign in to comment.