单向链表的核心逻辑
单向链表的每个节点存储当前的值和指向下个节点的指针,插入一个节点的时候需要找到最后一个节点。
#include "log.h"
#ifndef YGQ_TAG
#define YGQ_TAG
template<class E>
struct Node {
E value;
Node<E> *next;
Node(E value, Node<E> *next) {
this->value = value;
this->next = next;
}
};
template<class E>
class LinkedList {
private:
int len = 0;
Node<E> *head = nullptr;
public:
void push(E e);
Node<E> *findNode(int index);
E get(int index);
int size();
};
template<class E>
E LinkedList<E>::get(int index) {
return findNode(index)->value;
}
template<class E>
Node<E> *LinkedList<E>::findNode(int index) {
Node<E> *last = head;
for (int i = 0; i < index; i++) {
last = last->next;
}
return last;
}
template<class E>
int LinkedList<E>::size() {
return len;
}
template<class E>
void LinkedList<E>::push(E e) {
LOGI("push before linkedList size:%d", len);
Node<E> *new_node = new Node<E>(e, nullptr);
if (head) {
Node<E> *last = findNode(len - 1);
last->next = new_node;
} else {
head = new_node;
}
len++;
}
#endif
运行效果
打印结果
push before linkedList size:0
push before linkedList size:1
push before linkedList size:2
index:0,value:0
index:1,value:1
index:2,value:2