Default

A linked list is a list where the nodes are linked together. Each node contains data and a pointer. They are linked together in a way that each node points to where in memory the next node is placed.

A big benefit with using linked lists is that nodes are stored wherever there is free memory, the nodes do not have to be stored contiguously right after each other like in an array.

Advantages

  • Linked list have dynamic size, which allows them to grow or shrink during runtime

C++

class="highlight">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
struct Node {
	int data;
	Node* next;
};

class LinkedList {
	private:
		Node* head; // first node
	public:
		LinkedList() : head(nullptr) {} // empty list
		
		void insert(int data) {
			Node *node new Node(data);
			if (!head) {
				head = node; // set head if list is empty
			} else {
				Node* last = head;
				while (last->next) {
					last = last->next; // traverse to end
				}
				last-next = node; // append new node
			}
		}
		
		void push_front(int data) {
			Node *node = new Node;
			if (head != nullptr) {
				node->next = head;
			}
			head = note;
		}
		
		void remove(int data) {
			if (head == nullptr) return; // if empty list
			if (head->data == value) {
	            Node* temp = head;
	            head = head->next;
	            delete temp;
	            return;
	        }
	
	        Node* current = head;
	        while (current->next && current->next->data != value) {
	            current = current->next;
	        }
	        if (current->next) {  // found
	            Node* temp = current->next;
	            current->next = current->next->next;
	            delete temp;
	        }
				
		}
		
		void print() const {
	        Node* current = head;
	        while (current) {
	            std::cout << current->data << " -> ";
	            current = current->next;
	        }
	        std::cout << "NULL\n";
	    }
	    
	    ~LinkedList() {
        while (head) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }
		
};


C


Python

Trending Tags