Linked List
#include <iostream>
using namespace std;
class Node
{
public:
float data;
Node *next;
};
class LinkedList : public Node
{
public:
Node *head;
Node *tail;
LinkedList(); // Constructer Declaration
void CreateLinkedList();
void LinkedListTraversal();
void InsertAtFirst();
void InsertAtLast();
void InsertAtRandom(int);
void DeleteAtFirst();
void DeleteAtLast();
void DeleteAtRandom(int);
};
LinkedList ::LinkedList()
{
head = NULL;
tail = NULL;
}
int i = 1;
void LinkedList ::CreateLinkedList()
{
Node *temp = NULL;
temp = new (nothrow) Node;
if (!temp)
{
cout << "Memory Allocation Failed\n";
}
else
{
float element;
cout << "\nEnter The Value Of Element " << i << endl;
cin >> element;
// Handling Wrong UserInput
while (!cin.good())
{
cout << "Wrong Input, Please Enter Integers Or Floats Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Value Of Element " << i << endl;
cin >> element;
}
temp->data = element;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = head;
}
else
{
tail->next = temp;
tail = temp;
}
i++;
}
}
void LinkedList ::LinkedListTraversal()
{
Node *temp = head;
if (temp == NULL)
{
cout << "List Is Empty\n";
}
else
{
while (temp != NULL)
{
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL\n";
}
}
void LinkedList ::InsertAtFirst()
{
Node *temp = NULL;
temp = new (nothrow) Node;
if (!temp)
{
cout << "Memory Allocation Failed\n";
}
else
{
float element;
cout << "\nEnter The Value Of Element You Want To Insert At First\n";
cin >> element;
// Handling Wrong UserInput
while (!cin.good())
{
cout << "Wrong Input, Please Enter Integers Or Floats Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Value Of Element You Want To Insert At First\n";
cin >> element;
}
temp->data = element;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = head;
}
else
{
temp->next = head;
head = temp;
}
}
}
void LinkedList ::InsertAtLast()
{
Node *temp = NULL;
temp = new (nothrow) Node;
if (!temp)
{
cout << "Memory Allocation Failed\n";
}
else
{
float element;
cout << "\nEnter The Value Of Element You Want To Insert At Last\n";
cin >> element;
// Handling Wrong UserInput
while (!cin.good())
{
cout << "Wrong Input, Please Enter Integers Or Floats Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Value Of Element You Want To Insert At First\n";
cin >> element;
}
temp->data = element;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = head;
}
else
{
tail->next = temp;
tail = temp;
}
}
}
void LinkedList ::InsertAtRandom(int index)
{
Node *temp = NULL;
temp = new (nothrow) Node;
int count = 0;
Node *ptr = head;
if (!temp)
{
cout << "Memory Allocation Failed\n";
}
else
{
float element;
cout << "\nEnter The Value Of Element You Want To Insert At Index " << index << endl;
cin >> element;
// Handling Wrong UserInput
while (!cin.good())
{
cout << "Wrong Input, Please Enter Integers Or Floats Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Value Of Element You Want To Insert At First\n";
cin >> element;
}
temp->data = element;
temp->next = NULL;
while (count < (index - 1))
{
ptr = ptr->next;
count++;
}
temp->next = ptr->next;
ptr->next = temp;
}
}
void LinkedList ::DeleteAtFirst()
{
Node *temp = head;
if (temp == NULL)
{
cout << "There Is No Element In The Linked List To Delete\n";
}
else
{
head = head->next;
delete temp;
}
}
void LinkedList ::DeleteAtLast()
{
Node *temp = tail;
Node *ptr = head;
if (temp == NULL)
{
cout << "There Is No Element In The Linked List To Delete\n";
}
else
{
do
{
ptr = ptr->next;
} while (ptr->next != temp);
tail = ptr;
ptr->next = NULL;
delete temp;
}
}
void LinkedList ::DeleteAtRandom(int index)
{
Node *ptr = head;
Node *temp = head->next;
int count = 0;
if (ptr == NULL)
{
cout << "There Is No Element In The Linked List To Delete\n";
}
else
{
while (count < (index - 1))
{
ptr = ptr->next;
temp = temp->next;
count++;
}
ptr->next = temp->next;
delete temp;
}
}
int main()
{
int num = 0;
int userInput1;
char userInput2;
// Handling Wrong UserInput
while (!cin.good() >> num)
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Number Of Elements You Want To Add In The LinkedList\n";
cin >> num;
}
LinkedList lst;
while (true)
{
cout << "1. Press 1 To Create LinkedList\n"
<< "2. Press 2 To Traverse Linked List\n"
<< "3. Press 3 To Insert Element At First\n"
<< "4. Press 4 To Insert Element At Last\n"
<< "5. Press 5 To Insert Element At Random Index\n"
<< "6. Press 6 To Delete Element At First\n"
<< "7. Press 7 To Delete Element At Last\n"
<< "8. Press 8 To Delete Element At Random Index\n";
cin >> userInput1;
while (!cin >> userInput1)
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\n1. Press 1 To Create LinkedList\n"
<< "2. Press 2 To Traverse Linked List\n"
<< "3. Press 3 To Insert Element At First\n"
<< "4. Press 4 To Insert Element At Last\n"
<< "5. Press 5 To Insert Element At Random Index\n"
<< "6. Press 6 To Delete Element At First\n"
<< "7. Press 7 To Delete Element At Last\n"
<< "8. Press 8 To Delete Element At Random Index\n";
cin >> userInput1;
}
switch (userInput1)
{
case 1:
cout << "\nEnter The Number Of Elements In The Linked List\n";
cin >> num;
for (int i = 0; i < num; i++)
{
lst.CreateLinkedList();
}
break;
case 2:
cout << "The Elements In The LinkedList Are : \n";
lst.LinkedListTraversal();
break;
case 3:
lst.InsertAtFirst();
cout << "The Elements Of LinkedList After Inserting At First : \n";
lst.LinkedListTraversal();
break;
case 4:
lst.InsertAtLast();
cout << "The Elements Of LinkedList After Inserting At Last : \n";
lst.LinkedListTraversal();
break;
case 5:
int index1;
cout << "\nEnter The Index Where You Want To Insert The Element\n";
cin >> index1;
if (index1 >= num)
{
cout << "List Index Out Of Range\n\n";
}
else
{
// Handling Wrong Input
while (!cin.good() >> index1)
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Value Of Element You Want To Insert At Index " << index1 << endl;
cin >> index1;
}
lst.InsertAtRandom(index1);
cout << "The Elements Of LinkedList After Inserting At Index " << index1 << endl;
lst.LinkedListTraversal();
}
break;
case 6:
lst.DeleteAtFirst();
if (lst.head == NULL)
{
cout << "List Is Empty\n";
}
else
{
cout << "The Elements Of LinkedList After Deleting At First : \n";
lst.LinkedListTraversal();
}
break;
case 7:
lst.DeleteAtLast();
if (lst.head == NULL)
{
cout << "List Is Empty\n";
}
else
{
cout << "The Elements Of LinkedList After Deleting At Last : \n";
lst.LinkedListTraversal();
}
break;
case 8:
int index2;
cout << "\nEnter The Index Where You Want To Delete The Element\n";
cin >> index2;
if (lst.head == NULL)
{
cout << "List Is Empty\n\n";
}
else if (index2 >= num)
{
cout << "List Index Out Of Range\n\n";
}
else
{
// Handling Wrong Input
while (!cin.good() >> index2)
{
cout << "Wrong Input, Please Enter Integers Only!!\n";
cin.clear();
cin.ignore(100, '\n');
cout << "\nEnter The Value Of Element You Want To Delete At Index " << index2 << endl;
cin >> index2;
}
lst.DeleteAtRandom(index2);
cout << "\nThe Elements Of LinkedList After Deleting At Index " << index2 << endl;
lst.LinkedListTraversal();
}
break;
default:
cout << "Please Enter Correct Input\n";
break;
}
cout << "\nEnter 'c' To Continue And 'e' To Exit\n";
cin >> userInput2;
if (userInput2 == 'c')
{
continue;
}
else if (userInput2 == 'e')
{
exit(0);
}
else
{
cout << "Please Enter Correct Input\n";
cout << "\nEnter 'c' To Continue And 'e' To Exit\n";
cin >> userInput2;
if (userInput2 == 'c')
{
continue;
}
else if (userInput2 == 'e')
{
exit(0);
}
}
}
return 0;
}
Comments
Post a Comment