0% found this document useful (0 votes)
12 views6 pages

Lab 10

This document provides C++ code to implement a linked list data structure. It includes functions to insert nodes at different positions in the list, remove nodes from specific positions, display the list, search for elements, and get the size of the list. These functions are used in a main method to create a grocery list linked list, add and remove items, display the list, check its size, and search for an item.

Uploaded by

chanthida.choup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Lab 10

This document provides C++ code to implement a linked list data structure. It includes functions to insert nodes at different positions in the list, remove nodes from specific positions, display the list, search for elements, and get the size of the list. These functions are used in a main method to create a grocery list linked list, add and remove items, display the list, check its size, and search for an item.

Uploaded by

chanthida.choup
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ឈ្មោះ ជួប ចាន់ធីតា (14)

ថ្នា ក់ 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;
}

Node* newNode = new Node(value);


Node* temp = head;
for (int i = 0; i < index - 1; ++i) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
size++;
}
// Insertion at the end of the list
void insertAtEnd(int value) {
insertAtIndex(value, size);
}
// Remove the first node
void removeFirst() {
if (head == nullptr)
return;
Node* temp = head;
head = head->next;
delete temp;
size--;
}
// Remove the last node
void removeLast() {
if (head == nullptr)
return;
if (size == 1) {
removeFirst();
return;
}
Node* temp = head;
while (temp->next->next != nullptr) {
temp = temp->next;
}
delete temp->next;
temp->next = nullptr;
size--;
}
// Remove node at a specific index
void removeAtIndex(int index) {
if (index < 0 || index >= size)
return;
if (index == 0) {
removeFirst();
return;
}
if (index == size - 1) {
removeLast();
return;
}
Node* temp = head;
for (int i = 0; i < index - 1; ++i) {
temp = temp->next;
}
Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
delete nodeToDelete;
size--;
}
// Display the linked list
void display() {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
// Search for an element
bool search(int value) {
Node* temp = head;
while (temp != nullptr) {
if (temp->data == value)
return true;
temp = temp->next;
}
return false;
}
// Get the size of the linked list
int getSize() {
return size;
}
};
int main() {
LinkedList groceryList;
// Adding items to the grocery list
groceryList.insertAtEnd(1); // Milk
groceryList.insertAtEnd(2); // Bread
groceryList.insertAtEnd(3); // Eggs
groceryList.insertAtEnd(4); // Apples
groceryList.insertAtEnd(5); // Bananas
// Displaying the grocery list
std::cout << "Grocery Shopping List:" << std::endl;
groceryList.display();
// Checking the size of the list
std::cout << "Size of the list: " << groceryList.getSize() << std::endl;
// Searching for an item
int itemToFind = 3; // Eggs
if (groceryList.search(itemToFind))
std::cout << "Item " << itemToFind << " found in the list." << std::endl;
else
std::cout << "Item " << itemToFind << " not found in the list." << std::endl;
// Removing an item
groceryList.removeAtIndex(2); // Removing Eggs
std::cout << "After removing an item:" << std::endl;
groceryList.display();

return 0;
}

You might also like