Skip to content

Commit 4f65ab9

Browse files
committed
New implementation of LinkedList
1 parent 629a5c0 commit 4f65ab9

4 files changed

Lines changed: 123 additions & 187 deletions

File tree

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,51 @@ Java data structures
44
> Honorable mention to professor Eustrat Zhupa in the University of
55
> Rochester Computer Science Department.
66
7-
Lists
7+
To-do
88
-----
99

10+
### Lists
11+
1012
- [x] Dynamic array
1113
- [x] Singly linked list
1214
- [ ] Doubly linked list
13-
- [ ] Queue
1415
- [ ] Stack
16+
- [ ] Queue
17+
- [ ] Hash table
1518

16-
Trees
17-
-----
19+
### Trees
1820

19-
- [ ] Tree
20-
- [ ] Binary tree
2121
- [ ] Binary search tree (BST)
22-
- [ ] Heap
22+
- [ ] Binary heap
23+
- [ ] Priority queue
2324

24-
Sorting
25-
-------
25+
### Graphs
2626

27-
- [ ] Insertion sort
28-
- [ ] Selection sort
29-
- [ ] Merge sort
30-
- [ ] Quick sort
27+
- [ ] Graph
28+
- [ ] Trie
29+
30+
### Algorithms
31+
32+
- [ ] Bit manipulation
33+
- [ ] Mergesort
34+
- [ ] Quicksort
35+
- [ ] Heapsort
36+
- [ ] Binary search
37+
- [ ] Find <var>k</var>-th smallest elements
38+
- [ ] Permutations
39+
- [ ] Breadth-first search
40+
- [ ] Depth-first search
41+
- [ ] Dijkstra's algorithm
42+
- [ ] Tree traversal
43+
- [ ] Pre-order
44+
- [ ] In-order
45+
- [ ] Post-order
46+
- [ ] Topological sort
47+
- [ ] Cycle detection
48+
- [ ] in an undirected graph
49+
- [ ] in a directed graph
50+
- [ ] Count connected components in a graph
51+
- [ ] Find strongly connected components in a graph
52+
53+
Content
54+
-------

lists/LinkedList.java

Lines changed: 0 additions & 174 deletions
This file was deleted.

src/main/LinkedList.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
public class LinkedListNode {
2+
public byte data;
3+
protected LinkedListNode next;
4+
5+
public LinkedListNode(byte data, LinkedListNode next) {
6+
this.data = data;
7+
this.next = next;
8+
}
9+
}
10+
11+
public class LinkedList {
12+
private LinkedListNode head;
13+
private int length;
14+
15+
public LinkedList() {
16+
this.head = null;
17+
this.length = 0;
18+
}
19+
20+
public LinkedListNode getHead() {
21+
return this.head;
22+
}
23+
24+
public int getLength() {
25+
return this.length;
26+
}
27+
28+
/**
29+
* Insert new data at a specified index, shifting existing node at the index
30+
* and all nodes after it to the right.
31+
*
32+
* <p>
33+
* O(1) complexity for inserting at the head, O(i) complexity for other cases.
34+
* @param data
35+
* @param index A valid index for the new node that is between 0 and
36+
* <code>this.length</code> (inclusive).
37+
* @return
38+
*/
39+
public LinkedListNode insert(byte data, int index) {
40+
if (index < 0 || index > this.length) return null;
41+
42+
LinkedListNode newNode = new LinkedListNode(data, null);
43+
44+
if (index == 0) {
45+
if (this.head != null) newNode.next = this.head;
46+
this.head = newNode;
47+
48+
return newNode;
49+
}
50+
51+
LinkedListNode prevNode = this.head;
52+
53+
for (int i = 1; i < index; i++)
54+
prevNode = prevNode.next;
55+
56+
if (index != this.length)
57+
newNode.next = prevNode.next;
58+
59+
prevNode.next = newNode;
60+
this.length++;
61+
62+
return newNode;
63+
}
64+
65+
/**
66+
* Deletes the node at the specified index.
67+
*
68+
* <p>
69+
* O(n) complexity
70+
* @param index
71+
* @return
72+
*/
73+
public LinkedListNode delete(int index) {
74+
if (index < 0 || index >= this.length || this.length == 0) return null;
75+
76+
LinkedListNode prevNode = this.head;
77+
78+
for (int i = 1; i < index; i++)
79+
prevNode = prevNode.next;
80+
81+
LinkedListNode node = prevNode.next;
82+
prevNode.next = node.next;
83+
84+
return node;
85+
}
86+
}

0 commit comments

Comments
 (0)