-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cpp
105 lines (86 loc) · 2.04 KB
/
LinkedList.cpp
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
// You can use this code in any purpose you need without any mention
// Author: Mohamed Hesham
// Email: [email protected]
// Implementing LinkedList with struct
// In the below code I will implement 5 methods related to it
// Create, Insert, Remove, Search and Print
// It will be Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int value;
Node* next = NULL;
Node* prev = NULL;
};
struct LinkedList {
Node* head = NULL;
};
LinkedList *create() {
LinkedList *temp = new LinkedList;
return temp;
}
Node *insert(Node *&prevItem, int x) {
Node *temp = new Node;
temp->value = x;
temp->prev = prevItem;
if(prevItem != NULL) {
temp->next = prevItem->next;
if(prevItem->next) {
prevItem->next->prev = temp;
}
prevItem->next = temp;
} else {
// it is the head
prevItem = temp;
}
return temp;
}
void remove(Node *&item) {
if(item->next) {
item->next->prev = item->prev;
}
Node* temp1 = item->next;
Node* temp2 = item->prev;
delete[] item;
if(temp2 != NULL) {
temp2->next = temp1;
} else {
temp2 = temp1;
}
}
Node *search(LinkedList *list, int x) {
Node *it = list->head;
if(it == NULL) return NULL;
while(it->next != NULL) {
it = it->next;
if(it->value == x) return it;
}
return NULL;
}
void print(LinkedList *list) {
Node *it = list->head;
if(!it) return;
while(it != NULL) {
cout << it->value << " ";
it = it->next;
}
cout << endl;
}
int main() {
// some testing
// creating the list
LinkedList *myList = create();
// adding some elements to the list
Node *i = insert(myList->head, 6);
Node *j = insert(i, 7);
Node *k = insert(i, 8);
Node *l = insert(j, 9);
// search for an element
if(Node *it = search(myList, 8)) {
insert(it, 10);
}
// removing an element
remove(j);
// print all the list elements
print(myList);
}