#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) : data(value), next(nullptr) {}
};
class LinkedListSeq {
private:
Node* head;
int length;
public:
LinkedListSeq() : head(nullptr), length(0) {}
void insert(int index, int value) {
if (index < 0 || index > length) {
cout << "Index out of bounds." << endl;
return;
}
Node* newNode = new Node(value);
if (index == 0) {
newNode->next = head;
head = newNode;
} else {
Node* current = head;
for (int i = 0; i < index - 1; ++i) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
length++;
}
void remove(int index) {
if (index < 0 || index >= length) {
cout << "Index out of bounds." << endl;
return;
}
Node* toDelete;
if (index == 0) {
toDelete = head;
head = head->next;
} else {
Node* current = head;
for (int i = 0; i < index - 1; ++i) {
current = current->next;
}
toDelete = current->next;
current->next = toDelete->next;
}
delete toDelete;
length--;
}
void update(int index, int value) {
if (index < 0 || index >= length) {
cout << "Index out of bounds." << endl;
return;
}
Node* current = head;
for (int i = 0; i < index; ++i) {
current = current->next;
}
current->data = value;
}
int search(int value) {
Node* current = head;
for (int i = 0; i < length; ++i) {
if (current->data == value) {
return i;
}
current = current->next;
}
return -1;
}
void print() {
Node* current = head;
while (current) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
~LinkedListSeq() {
Node* current = head;
while (current) {
Node* nextNode = current->next;
delete current;
current = nextNode;
}
}
};
int main() {
LinkedListSeq seqList;
seqList.insert(0, 10);
seqList.insert(1, 20);
seqList.insert(1, 15);
seqList.print();
seqList.update(1, 25);
seqList.print();
int index = seqList.search(20);
cout << "Value 20 is at index: " << index << endl;
seqList.remove(1);
seqList.print();
return 0;
}