-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
94 lines (82 loc) · 1.91 KB
/
linkedlist.cpp
File metadata and controls
94 lines (82 loc) · 1.91 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
#include <iostream>
#include "linkedlist.h"
using namespace std;
Nodel::Nodel(string value) : data(value), next(nullptr) {};
linkedlist::linkedlist() : head(nullptr) {};
void linkedlist::insert(string value)
{
Nodel* newNode = new Nodel(value);
if (head == nullptr) {
head = newNode;
return;
}
Nodel* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
void linkedlist::remove(string value)
{
if (head == nullptr) return;
if (head->data == value) {
Nodel* temp = head;
head = head->next;
delete temp;
return;
}
Nodel* temp = head;
while (temp->next != nullptr && temp->next->data != value) {
temp = temp->next;
}
if (temp->next == nullptr) return;
Nodel* nodeToDelete = temp->next;
temp->next = temp->next->next;
delete nodeToDelete;
}
void linkedlist::display()
{
Nodel* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
int linkedlist::getNum()
{
int number = 0;
Nodel* temp = head;
if(temp == 0)
{
return 0;
}
while (temp != nullptr)
{
number++;
temp = temp->next;
}
return number;
}
string linkedlist::asLine()
{
Nodel* temp = head;
string tempLine;
while (temp != nullptr) {
tempLine += temp->data;
tempLine += " ";
temp = temp->next;
}
return tempLine;
}
bool linkedlist::searchInLinkedlist(string value)
{
Nodel* temp = head;
while (temp != nullptr) {
if (temp->data == value) {
return true;
}
temp = temp->next;
}
return false;
}