-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
179 lines (152 loc) · 3.83 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
Author: Ammar Arif
Feb 2018
Purpose: The file contains implementation of Ordered Singly and Doubly linked list classes.
Doubly Linked List inherits from Singly lists.
Contains implementation of sorted insert, delete and search functions for both lists.
*/
#include "iostream"
using namespace std;
struct node {
double data;
node* next = nullptr;
node* prev = nullptr;
};
class singleLinkedList
{
private:
public:
node* head = nullptr;
// Prints list data to screen
void Read()
{
node* temp = head;
while (temp != nullptr)
{
cout << temp->data << " " ;
temp = temp->next;
}
}
// Searches and returns the node containing desired data. If not present, returns Not Found.
node* Search(double data)
{
node* current = head;
while (current->next != nullptr)
{
if (current->data == data)
{
return current;
}
current = current->next;
}
cout << data << " Not Found." << endl;
return nullptr;
}
// Inserts node in a sorted order
void Insert(double data)
{
if (head == nullptr) // if list is empty
{
node* temp;
node* n = new node;
head = n;
head->data = data;
temp = n;
}
else if (data < head->data) // if to be inserted before head node
{
node* temp;
node* n = new node;
n->data = data;
n->next = head;
head = n;
}
else // in case to be added after head node
{
node* n = new node;
n->data = data;
node* trailing = head;
node* current = head->next;
while (current != nullptr && data > current->data)
{
trailing = current;
current = current->next;
}
trailing->next = n;
n->next = current;
}
}
};
class doubeLinkedList: public singleLinkedList
{
public:
// Inserts node in doubly linked list in a sorted order
void Insert(double data)
{
if (head == nullptr) // if list is empty
{
node* n = new node;
n->data = data;
head = n;
}
else if (data < head->data) // if to be added before head node
{
node* temp = head;
node* n = new node;
n->data = data;
n->next = head;
head = n;
temp->prev = n;
}
else // in case to be added after head node
{
node* current = head;
node* n = new node;
n->data = data;
while (current->next != nullptr && current->next->data < n->data)
{
current = current->next;
}
n->next = current->next;
if (current->next != nullptr)
{
n->next->prev = n;
}
current->next = n;
n->prev = current;
}
}
// Deletes node containing desired data. If not found, prints Not Found.
void Delete(double data)
{
node* current = Search(data);
if (current == nullptr)
{
return;
}
else
{
cout << "Deleting " << current->data << endl;
current->prev->next = current->next;
current->next->prev = current->prev;
}
}
};
int main()
{
doubeLinkedList L;
L.Insert(54.15);
L.Insert(11.42);
L.Insert(12.19);
L.Insert(83.40);
L.Insert(15.27);
L.Insert(60.17);
L.Insert(-63.81);
L.Insert(-1.394);
L.Insert(-22.60);
L.Read();
cout<< endl;
L.Delete(12.19);
L.Delete(27.33);
L.Read();
}