0% found this document useful (0 votes)
2 views3 pages

Linked List

The document contains a C++ implementation of a singly linked list with functionalities to insert, remove, display, calculate the sum and average of the elements, and count even numbers. It defines a Node structure and a LinkedList class that manages the linked list operations. The main function demonstrates the creation of a linked list, insertion of values, and calls to the sum and countEven methods.

Uploaded by

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

Linked List

The document contains a C++ implementation of a singly linked list with functionalities to insert, remove, display, calculate the sum and average of the elements, and count even numbers. It defines a Node structure and a LinkedList class that manages the linked list operations. The main function demonstrates the creation of a linked list, insertion of values, and calls to the sum and countEven methods.

Uploaded by

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

#include <iostream>

using namespace std;


struct Node
{
int data;
Node *next;
};

class LinkedList
{
private:
Node *head;

public:
LinkedList() : head(nullptr) {}

void insert(int value)


{
Node *newNode = new Node;
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr)
{
head = newNode;
return;
}
Node *temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = newNode;
}

void remove(int value)


{
if (head == nullptr)
return;

if (head->data == value)
{
Node *temp = head;
head = head->next;
delete temp;
return;
}

Node *current = head;


Node *previous = nullptr;
while (current && current->data != value)
{
previous = current;
current = current->next;
}

if (current)
{
previous->next = current->next;
delete current;
}
}

// Display the linked list


void display() const
{
Node *temp = head;
while (temp)
{
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "nullptr" << std::endl;
}
void sum()
{
int calSum = 0;
Node *temp = head;
int i = 0;
while (temp)
{
calSum += temp->data;
temp = temp->next;
i++;
}
cout << "The sum is : " << calSum << endl;
cout << "The average is : " << calSum / i << endl;
delete temp;
}
int countEven()
{
Node *temp = head;
int i = 0;
while (temp)
{
int count = temp->data;
temp = temp->next;
if (count % 2 == 0)
{
i++;
}
}
return i;
}
~LinkedList()
{
while (head)
{
remove(head->data);
}
}
};

int main()
{
LinkedList list;

list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);
list.insert(49);

cout << "Linked List: ";


list.display();

// list.remove(20);
// cout << "After removing 20: ";
// list.display();
list.sum();
cout << "The even no in linked List " << list.countEven() << endl;
return 0;
}

You might also like