-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.cpp
More file actions
180 lines (158 loc) · 4.55 KB
/
Copy pathlinkedList.cpp
File metadata and controls
180 lines (158 loc) · 4.55 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
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
180
// LinkedLists.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
// The linked list stores data in sequential storage, like arrays.Though the data are stored sequentially, the memory locations are not contiguous.
// Unlike an array, the linked list can store data of different data types.
#include <iostream>
// Node class to represent
// a node of the linked list.
class Node {
public:
int data;
Node* next;
Node* prev;
// Default constructor
Node()
{
data = 0;
next = NULL;
prev = NULL;
}
// Parameterised Constructor
Node(int data)
{
this->data = data;
this->next = NULL;
this->prev = NULL;
}
};
// Linked list class to
// implement a linked list.
class Linkedlist {
Node* head;
Node* tail;
public:
Linkedlist() {
head = nullptr; // Initialize head to nullptr
tail = nullptr; // Initialize tail to nullptr
}
int findNode(int data)
{
Node* temp = head;
if (head == NULL) {
return -1;
}
int nodeOffset = 0;
// Traverse the list to find the node.
while (temp != NULL) {
if (temp->data == data) {
return nodeOffset;
}
temp = temp->next;
nodeOffset++;
}
return -1;
}
// insert a node at the end of the list
void insertNode(int);
void printList();
void printListReverse();
void deleteNode(int);
};
// Function to delete the
// node at given position
void Linkedlist::deleteNode(int nodeOffset) {
if (head == nullptr) return;
Node* temp = head;
int ListLen = 0;
while (temp && ListLen < nodeOffset) {
temp = temp->next;
ListLen++;
}
if (!temp) return; // Out of range
if (temp->prev) temp->prev->next = temp->next;
else head = temp->next; // Deleting head
if (temp->next) temp->next->prev = temp->prev;
else tail = temp->prev; // Deleting tail
delete temp;
}
void Linkedlist::insertNode(int data) // Function to insert a new node.
{
// Create the new Node.
Node* newNode = new Node(data);
if (head == NULL) { // Assign to head
head = tail = newNode; // If list is empty, assign head and tail to new node?
return;
}
if (data < head->data) { // Insert at the beginning
newNode->next = head; // Update next pointer of new node
head->prev = newNode; // Update previous pointer of head
head = newNode; // Update head
return;
}
Node* temp = head; // Traverse till end of list
while (temp->next != NULL) {
if (data > temp->data && data < temp->next->data) {
newNode->next = temp->next; // Insert in between
newNode->prev = temp; // Update previous pointer of new node
temp->next->prev = newNode; // Update previous pointer of next node
temp->next = newNode;
return;
}
temp = temp->next; // Update temp
}
temp->next = newNode; // Insert at the last.
newNode->prev = temp; // Update previous pointer of new node
tail = newNode; // Update tail to the new node
}
// Function to print the
// nodes of the linked list.
void Linkedlist::printList()
{
Node* temp = head;
// Check for empty list.
if (head == NULL) {
std::cout << "List empty, head empty" << std::endl;
return;
}
// Traverse the list.
while (temp != NULL) {
std::cout << temp->data << " ";
temp = temp->next;
}
}
void Linkedlist::printListReverse() {
Node* temp = tail;
while (temp) {
std::cout << temp->data << " ";
temp = temp->prev;
}
std::cout << std::endl;
}
int main()
{
Linkedlist list;
list.insertNode(1);
list.insertNode(2);
list.insertNode(70);
list.insertNode(93);
list.insertNode(10);
list.insertNode(64);
list.insertNode(15);
list.insertNode(42);
std::cout << "Elements of the list are: ";
list.printList();
std::cout << std::endl;
list.deleteNode(2);
std::cout << "Elements of the list are: ";
list.printList();
std::cout << std::endl;
std::cout << "Input a number to search for: ";
int search;
std::cin >> search;
if (list.findNode(search) != -1) {
std::cout << "Found " << search << " at index: " << list.findNode(search) << std::endl;
} else {
std::cout << search << " not found in the list." << std::endl;
}
return 0;
}