-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.h
54 lines (46 loc) · 1.18 KB
/
LinkedList.h
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
#ifndef LINKEDLIST_H
//#define LINKEDLIST_H
#include <iostream>
#include "ADT.h"
using namespace std;
class LinkedList : public ADT {
Node* head;
public:
LinkedList() : head(nullptr) {}
void insert(int val) override {
Node* newNode = new Node;
newNode->val = val;
newNode->next = head;
head = newNode;
}
void remove() override {
if (!head) {
cout << "Error: List is empty, cannot remove." << endl;
return;
} else {
Node* temp = head;
head = head->next;
delete temp;
}
}
void print() const override {
Node* temp = head;
while (temp) {
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
}
// Insert after specific node
void insertAfter(Node* node, int val) {
if (node == nullptr) {
cout << "Given previous node cannot be NULL" << endl;
return;
}
Node* newNode = new Node;
newNode->val = val;
newNode->next = node->next;
node->next = newNode;
}
};
#endif