linklist
#include <iostream>
class Node {
public:
char data;
Node* next;
Node(char value) {
data = value;
next = nullptr;
}
};
class LinkedList {
public:
Node* head;
LinkedList() {
head = nullptr;
}
void insertAtBeginning(char value) {
Node* newNode = new Node(value);
newNode->next = head;
head = newNode;
}
Node* search(char key) {
Node* current = head;
while (current) {
if (current->data == key) {
return current;
}
current = current->next;
}
return nullptr; // Key not found
}
void printList() {
Node* current = head;
while (current) {
std::cout << current->data << ” “;
current = current->next;
}
std::cout << std::endl;
}
int sizeOfList() {
int count = 0;
Node* current = head;
while (current) {
count++;
current = current->next;
}
return count;
}
};
int main() {
LinkedList list;
// Repeatedly call InsertAtBeginning() to create the linked list
list.insertAtBeginning(‘C’);
list.insertAtBeginning(‘B’);
list.insertAtBeginning(‘A’);
// Printing the list
std::cout << “Linked List: “;
list.printList();
// Searching for a node with a key
char key = ‘B’;
Node* foundNode = list.search(key);
if (foundNode) {
std::cout << “Node with key ‘” << key << “‘ found.” << std::endl;
} else {
std::cout << “Node with key ‘” << key << “‘ not found.” << std::endl;
}
// Getting the size of the list
int size = list.sizeOfList();
std::cout << “Size of the linked list: ” << size << std::endl;
return 0;
}