# [data structure and algorithm] - linked list

Keywords: Java data structure linked list

2. special column
3. java
4. Article details
0

# [data structure and algorithm] - linked list

benntty74 Published 13 minutes ago

## 1. What is a linked list

Linked list is a kind of linear list. Linear table means literally that the relationship between data is connected by a line.

Linear tables are divided into address space continuous and address space discontinuous.

For example, arrays are continuous linear tables in address space. They are a continuous data block in memory. Blocks are close to each other, just like the relationship of [- 1,0,1] on the X-axis.

The linked list is a linear list with discontinuous address space. They are not continuous blocks in memory, but scattered everywhere, and then connected through a curved line.

The following is a more vivid description.

• Address space continuous linear table:

|ðŸ˜€ |ðŸ˜‚|ðŸ˜†|ðŸ™ƒ|ðŸ˜‰|

• Address space discontinuous linear table

ðŸ˜€---------->ðŸ˜‚------->ðŸ˜†--->ðŸ™ƒ------------>ðŸ™ƒ>ðŸ˜‰

Linked list is divided into single linked list and double linked list. A single linked list only knows the location of the next node (the above figure is a single linked list), while a double linked list can know the location of the previous and next nodes.

Each node in a single linked list contains two parts, one is the data on the linked list node, and the other is the line (pointer) pointing to the next node.

Each node on the double linked list contains three parts. One part is the data on the linked list node, one part is the line (pointer) pointing to the next node, and the other part is the line (pointer) pointing to the previous node.

• New nodes: at the beginning, middle and end
• Delete node: at beginning, middle and end
• Query node: query the node data of the ith position

## 3. java implementation (double linked list)

```public class LinkedList<E> implements List<E>, Queue<E>, Stack<E> {

private int size = 0;

private Node<E> tail;

}

@Override
public boolean isEmpty() {
return size() == 0;
}

@Override
public int size() {
return size;
}

@Override
return offer(e);
}

@Override
public boolean remove(E e) {
int idx = 0;
while (tmp != null) {
if (tmp.e.equals(e)) {
remove(idx);
return true;
}
idx++;
tmp = tmp.next;
}
return false;
}

@Override
public boolean contains(E e) {
while (tmp != null) {
if (tmp.e.equals(e)) {
return true;
}
tmp = tmp.next;
}
return false;
}

@Override
public E get(int i) {
if (i > size) {
throw new IndexOutOfBoundsException("Index i " + i + "out of range " + size);
}
if (i == 0) {
}
if (i == size - 1) {
return tail.e;
}
for (int j = 0; j < i; j++) {
tmp = tmp.next;
}
return tmp.e;
}

@Override
public E remove(int i) {
if (i >= size) {
throw new IndexOutOfBoundsException("Index i " + i + "out of range " + size);
}
if (i == 0) {
return poll();
}
if (i == size - 1) {
return pop();
}
for (int j = 0; j < i; j++) {
tmp = tmp.next;
}
E e = tmp.next.e;
tmp.next.next.prev = tmp;
tmp.next = tmp.next.next;
return e;
}

@Override
public boolean add(E e, int i) {
if (i > size) {
throw new IndexOutOfBoundsException("Index i " + i + "out of range " + size);
}
Node<E> node = new Node<>(e);
if (i == size) {
return offer(e);
}
if (i == 0) {
node.prev = null;
} else {
for (int j = 0; j <= i; j++) {
tmp = tmp.next;
}
tmp.next = node;
node.prev = tmp;
}
size++;
return false;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
while (node != null) {
sb.append(node.e).append("<->");
node = node.next;
}
sb.append("null }");
return sb.toString();
}

private static class Node<E> {

E e;

Node<E> next;
Node<E> prev;

public Node(E e) {
this.e = e;
}
}

}```

### 4. Data structure series code address

Individuals have also implemented other data structures and algorithms. Interested partners can view the detailed and complete code and test on Github or blog.

Bugkit blog: http://bugkit.cn or IP

Reading 10 was published 13 minutes ago
Like collection
##### benntty74

Small transparent

34 prestige
0 fans
Focus on the author
You know what?