Implement a class that realizes linked lists consisting of nodes with integer values.a) bool isEmpty(); b) int length(); returns the number of nodes in the list, which is 0 for the empty list. c) void print(); print the content of all nodes. d) void addAsHead(int i); creates a new node with the integer and adds it to the beginning of the list. e) void addAsTail(int i); creates a new node with the integer and adds it to the end of the list. f) Node find(int i); returns the first node with val i. g) void reverse(); reverses the list. h) int popHead(); returns the value of the head of the list and removes the node, if the list is nonempty, otherwise returns NULL. i) void removeFirst(int i); removes the first node with val i. j) void removeAll(int i); removes all nodes with val i. k) void addAll(List l); appends the list l to the last element of the current list, if the current list is nonempty, or lets the head of the current list point to the first element of l if the current list is empty.
#include <iostream>
struct Node {
int value;
Node* next;
};
class List {
private:
Node* head;
public:
List() : head(nullptr) {}
bool isEmpty() {
return head == nullptr;
}
int length() {
int length = 0;
Node* ptr = head;
while (ptr != nullptr) {
ptr = ptr->next;
++length;
}
return length;
}
void print() {
Node* ptr = head;
std::cout << '[';
while (ptr != nullptr) {
if (ptr != head) {
std::cout << ", ";
}
std::cout << ptr->value;
ptr = ptr->next;
}
std::cout << ']';
}
void addAsHead(int i) {
head = new Node{i, head};
}
void addAsTail(int i) {
if (head == nullptr) {
head = new Node{i, nullptr};
return;
}
Node* ptr = head;
while (ptr->next != nullptr) {
ptr = ptr->next;
}
Node* node = new Node{i, nullptr};
ptr->next = node;
}
Node* find(int i) {
Node* ptr = head;
while (ptr != nullptr) {
if (ptr->value == i) {
return ptr;
}
ptr = ptr->next;
}
return nullptr;
}
void reverse() {
if (head == nullptr || head->next == nullptr) {
return;
}
Node* ptr = head;
Node* ptr1 = head->next;
ptr->next = nullptr;
while (ptr1 != nullptr) {
Node* temp = ptr;
ptr = ptr1;
ptr1 = ptr1->next;
ptr->next = temp;
}
head = ptr;
}
int popHead() {
if (head == nullptr){
return -1;
}
Node* temp = head;
head = head->next;
return temp->value;
}
void removeFirst(int i) {
Node* ptr = head;
Node* ptr1 = head;
while (ptr != nullptr) {
if (ptr->value == i) {
if (ptr == head) {
head = head->next;
delete ptr;
return;
}
ptr1->next = ptr->next;
delete ptr;
return;
}
else ptr1 = ptr;
ptr = ptr->next;
}
}
void removeAll(int i) {
Node* ptr = head;
Node* ptr1 = head;
while (ptr != nullptr) {
if (ptr->value == i) {
if (ptr == head) {
head = head->next;
delete ptr;
ptr = head;
continue;
}
ptr1->next = ptr->next;
Node* temp = ptr;
ptr = ptr->next;
delete temp;
}
else {
ptr1 = ptr;
ptr = ptr->next;
}
}
}
void addAll(List& l) {
if (l.head == nullptr) {
return;
}
Node* ptr = head;
if (ptr == nullptr) {
ptr = new Node{l.head->value, nullptr};
head = ptr;
}
else {
while (ptr->next != nullptr) {
ptr = ptr->next;
}
Node* temp = ptr;
ptr = new Node{l.head->value, nullptr};
temp->next = ptr;
}
Node* ptr1 = l.head->next;
while (ptr1 != nullptr) {
Node* temp = ptr;
ptr = new Node{ptr1->value, nullptr};
temp->next = ptr;
ptr1 = ptr1->next;
}
}
~List() {
Node* ptr = head;
while (ptr != nullptr) {
Node* temp = ptr;
ptr = ptr->next;
delete temp;
}
}
};
int main() {
List list;
std::cout << "List before filling: ";
list.print();
std::cout << ", isEmpty: " << list.isEmpty() << ", length: " << list.length();
for (int i = 0; i < 10; ++i) {
list.addAsTail(i % 5 + 1);
}
std::cout << "\nList after filling: ";
list.print();
std::cout << ", isEmpty: " << list.isEmpty() << ", length: " << list.length();
list.addAsHead(11);
std::cout << "\nAfter adding 11 as head: ";
list.print();
list.reverse();
std::cout << "\nReversed list: ";
list.print();
int head = list.popHead();
std::cout << "\nRemoved from head value: " << head << '\n';
std::cout << "List: ";
list.print();
list.removeFirst(3);
std::cout << "\nAfter removing 3: ";
list.print();
list.removeAll(2);
std::cout << "\nAfter removing all 2's: ";
list.print();
List list2;
for (int i = 0; i < 5; ++i) {
list2.addAsHead(i);
}
std::cout << "\nSecond list: ";
list2.print();
list.addAll(list2);
std::cout << "\nConcatenated lists: ";
list.print();
Node* node = list.find(3);
std::cout << "\nFound node with value of 3: address = " << node << ", {value: " << node->value << ", next: " << node->next << "}\n";
return 0;
}
Comments
Leave a comment