-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsingly_linked_list.cpp
More file actions
108 lines (89 loc) · 2.33 KB
/
singly_linked_list.cpp
File metadata and controls
108 lines (89 loc) · 2.33 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream>
using namespace std;
struct Node {
int val;
Node* next;
Node(int val) {
this -> val = val;
this -> next = NULL;
}
};
void print_list(Node* head) {
struct Node* pCrawl = head;
while (pCrawl != NULL) {
cout << (pCrawl -> val) << " -> ";
pCrawl = pCrawl -> next;
}
cout << "END\n";
}
void insert_at_head(Node* &head, int val) {
if (head == NULL) { // Empty List
head = new Node(val);
return;
}
Node* newNode = new Node(val);
newNode -> next = head;
head = newNode;
}
void insert_at_position(Node* &head, int val, int pos) {
struct Node* pCrawl = head;
for (int i = 0; i < pos - 1; i++)
pCrawl = pCrawl -> next;
Node *A = pCrawl;
Node *B = pCrawl->next;
Node* C = new Node(val);
A -> next = C;
C -> next = B;
}
void insert_at_end(Node* &head, int val) {
// List is empty
if (head == NULL) {
head = new Node(val);
return ;
}
struct Node* pCrawl = head;
while(pCrawl->next != NULL) { // iterate to last node
pCrawl = pCrawl -> next;
}
pCrawl -> next = new Node(val);
}
// Time complexity: O(N)
bool search(Node* &head, int val) {
struct Node* pCrawl = head;
while (pCrawl != NULL) {
if(pCrawl -> val == val)
return true;
pCrawl = pCrawl -> next;
}
return false;
}
void delete_head(Node* &head) {
Node* temp = head;
head = head->next;
delete temp;
}
// Time complexity: O(N)
void delete_at_position(Node* &head, int pos) {
struct Node* A = head;
for (int i = 0; i < pos - 1; i++) {
A = A->next;
}
Node *C = A->next;
Node *B = A->next->next;
A->next = B;
delete C;
}
int main() {
Node* head = NULL;
cout << boolalpha;
insert_at_head(head, 4); print_list(head);
insert_at_head(head, 1); print_list(head);
insert_at_position(head, 2, 1); print_list(head);
insert_at_position(head, 3, 2); print_list(head);
insert_at_end(head, 9); print_list(head);
insert_at_end(head, 12); print_list(head);
cout << "Searching for 9: " << search(head, 9) << "\n";
cout << "Searching for 5: " << search(head, 5) << "\n";
delete_head(head); print_list(head);
delete_at_position(head, 3); print_list(head);
}