-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist.cpp
120 lines (107 loc) · 2.63 KB
/
linklist.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
using namespace std;// Making a node struct containing an int data and a pointer// to next node
struct Node {
int data;
Node *next;// Parameterised constructor with default argument
Node(int val=0) :data(val),next(nullptr){}// Parameterise constructor
Node(int val, Node *tempNext):data(val),next(tempNext){}
};
class LinkedList
{ // Head pointer
Node* head;
public:// default constructor. Initializing head pointer
LinkedList():head(nullptr)
{
}
// inserting elements (At start of the list)
void insert(int val)
{
// make a new node
Node* new_node = new Node(val);
// If list is empty, make the new node, the head
if (head == nullptr)
{
head = new_node;
}
// else, make the new_node the head and its next, the previous
// head
else
{
new_node->next = head;
head = new_node;
}
}
// loop over the list. return true if element found
bool search(int val)
{
Node* temp = head;
while(temp != nullptr)
{
if (temp->data == val)
return true;
temp = temp->next;
}
return false;
}
void remove(int val)
{
Node* temp = head;
// If the head is to be deleted
if (temp != nullptr && temp->data == val)
{
head = temp->next;
delete temp;
return;
}
// Else loop over the list and search for the node to delete
else
{
Node* curr = head;
while(temp != nullptr && temp->data != val)
{
// When node is found, delete the node and modify the pointers
curr = temp;
temp = temp->next;
}
// If values is not found in the linked list
if(!temp)
{
cout << "Value not found" << endl;
return;
}
curr->next = temp->next;
delete temp;
}
}
void display()
{
Node* temp = head;
while(temp != nullptr)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList l;
// inserting elements
l.insert(6);
l.insert(9);
l.insert(1);
l.insert(3);
l.insert(7);
cout << "Current Linked List: ";
l.display();
cout << "Deleting 1: ";
l.remove(1);
l.display();
cout << "Deleting 13: ";
l.remove(13);
cout << "Searching for 7: ";
cout << l.search(7) << endl;
cout << "Searching for 13: ";
cout << l.search(13) << endl;
return 0;
}