0% found this document useful (0 votes)
7 views8 pages

Assignment 02 2

The document describes a C++ program that implements operations on a linked list such as insertion at the beginning and end, display, search, deletion from beginning and end. The main function tests these operations by taking user inputs to insert and delete elements and display the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Assignment 02 2

The document describes a C++ program that implements operations on a linked list such as insertion at the beginning and end, display, search, deletion from beginning and end. The main function tests these operations by taking user inputs to insert and delete elements and display the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Assignment-02

(Operations in a linked list)


Name: Emon Hossen
ID: 2211106042
Section: 08
#include <iostream>
using namespace std;

template <class ItemType>


class LinkedList
{
private:
struct Node
{
ItemType data;
Node* next;
};

Node* head;
int length;

public:
LinkedList();
~LinkedList();

void InsertBegin(ItemType item);


void InsertEnd(ItemType item);
void Display();
bool Search(ItemType item);
void DeleteBegin();
void DeleteEnd();
};

template <class ItemType>


LinkedList<ItemType>::LinkedList()
{
head = nullptr;
length = 0;
}

template <class ItemType>


LinkedList<ItemType>::~LinkedList()
{
while (head != nullptr)
{
Node* temp = head;
head = head->next;
delete temp;
}
}

template <class ItemType>


void LinkedList<ItemType>::InsertBegin(ItemType item)
{
Node* newNode = new Node;
newNode->data = item;
newNode->next = head;
head = newNode;
length++;
}

template <class ItemType>


void LinkedList<ItemType>::InsertEnd(ItemType item)
{
Node* newNode = new Node;
newNode->data = item;
newNode->next = nullptr;

if (head == nullptr)
{
head = newNode;
}
else
{
Node* current = head;
while (current->next != nullptr)
{
current = current->next;
}
current->next = newNode;
}
length++;
}

template <class ItemType>


void LinkedList<ItemType>::Display()
{
Node* current = head;
while (current != nullptr)
{
cout << current->data << " ";
current = current->next;
}
cout << endl;
}

template <class ItemType>


bool LinkedList<ItemType>::Search(ItemType item)
{
Node* current = head;
while (current != nullptr)
{
if (current->data == item)
{
return true;
}
current = current->next;
}
return false;
}

template <class ItemType>


void LinkedList<ItemType>::DeleteBegin()
{
if (head != nullptr)
{
Node* temp = head;
head = head->next;
delete temp;
length--;
}
}

template <class ItemType>


void LinkedList<ItemType>::DeleteEnd()
{
if (head == nullptr)
{
return;
}
if (head->next == nullptr)
{
delete head;
head = nullptr;
}
else
{
Node* current = head;
while (current->next->next != nullptr)
{
current = current->next;
}
delete current->next;
current->next = nullptr;
}
length--;
}

int main()
{
LinkedList<int> L;

// Insert at the beginning


int userInput1;
cout << "Input an integer element to insert at the beginning : ";
cin >> userInput1;
L.InsertBegin(userInput1);
int userInput2;
cout << "Input an integer element to insert at the beginning : ";
cin >> userInput2;
L.InsertBegin(userInput2);

// Insert at the end


int valueToInsert1;
cout << "Input an integer element to insert at the end : ";
cin >> valueToInsert1;
L.InsertEnd(valueToInsert1);

int valueToInsert2;
cout << "Input an integer element to insert at the end: ";
cin >> valueToInsert2;
L.InsertEnd(valueToInsert2);

// Display the list


cout << "List after inserting at the beginning and end: ";
L.Display();

// Search for an item


int searchValue;
cout << "Enter an integer element to search: ";
cin >> searchValue;

if (L.Search(searchValue))
{
cout << searchValue << " found in the list." << endl;
}
else
{
cout << searchValue << " not found in the list." << endl;
}

// Delete from the beginning


L.DeleteBegin();
cout << "After deleting an item from the beginning of the list" << endl;
L.Display();

// Delete from the end


L.DeleteEnd();
cout << "After deleting an item from the end of the list" << endl;
L.Display();

return 0;
}

You might also like