forked from AhmadEnan/Data-Structures-Project-01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
85 lines (78 loc) · 1.43 KB
/
Copy pathLinkedList.cpp
File metadata and controls
85 lines (78 loc) · 1.43 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
#include "LinkedList.h"
#include <iostream>
LinkedList::LinkedList()
{
head = nullptr;
}
LinkedList::~LinkedList()
{
Node* current = head;
while(current)
{
Node* temp = current;
current = current->next;
delete temp;
}
}
void LinkedList::insertAtHead(int value)
{
if(!head)
{
head = new Node(value);
}
else
{
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
}
void LinkedList::insertAtEnd(int value)
{
if(!head)
{
head = new Node(value);
}
else
{
Node* current = head;
while(current->next)
{
current = current->next;
}
current->next = new Node(value);
}
}
void LinkedList::deleteValue(int value)
{
while (head && head->data == value)
{
Node* temp = head;
head = head->next;
delete temp;
}
Node* current = head;
while (current && current->next)
{
if (current->next->data == value)
{
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
else
{
current = current->next;
}
}
}
void LinkedList::display() const
{
Node* current = head;
while(current)
{
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}