Lab 10
Lab 10
ថ្នា ក់ A2
Homework Week 10
Example:
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
class LinkedList {
private:
Node* head;
int size;
public:
LinkedList() {
head = nullptr;
size = 0;
}
// Insertion at the beginning of the list
void insertAtBegin(int value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
size++;
}
// Insertion at a specific index
void insertAtIndex(int value, int index) {
if (index < 0 || index > size)
return;
if (index == 0) {
insertAtBegin(value);
return;
}
return 0;
}