#include
#include
#include
using namespace std;
struct node
{
int val;
struct node *next;
}*startNode;
// declaring class
class LinkedList
{
public:
node* create_node(int);
void insert_last();
void sort();
void search();
void reverse();
void display();
LinkedList()
{
startNode = NULL;
}
};
// main
main()
{
int userChoice, linkedListNodes, i;
LinkedList LinkList;
startNode = NULL;
while (1)
{
// displaying menu
cout<<\" 1)Insert\";
cout<<\"2) Remove\";
cout<<\"3) Search\";
cout<<\"4) Display\";
cout<<\"5) Reverse\";
cout<<\"6) Exit \";
// reading user choice
cout<<\"Enter your userChoice : \";
cin>>userChoice;
switch(userChoice)
{
case 1:
cout<<\"Inserting node to linked list: \";
LinkList.insert_begin();
cout;
break;
case 2:
cout<<\"Delete element from linked list: \";
LinkList.delete();
break;
case 3:
cout<<\"Searching data in linked list: \";
LinkList.search();
cout;
break;
case 4:
cout<<\"Displaying linked list data: \";
LinkList.display();
cout;
break;
case 5:
cout<<\"Linked list reversing: \";
LinkList.reverse();
cout;
break;
case 6:
cout<<\"Exiting...\";
exit(1);
break;
default:
cout<<\"Enter correct userChoice\";
}
}
}
// node creating to store into list
node *LinkedList::create_node(int val)
{
struct node *tempNode, *tempNode;
tempNode = new(struct node);
if (tempNode == NULL)
{
cout<<\"No memory allocated \";
return 0;
}
else
{
tempNode->val = val;
tempNode->next = NULL;
return tempNode;
}
}
/*
* Inserting Node at last
*/
void LinkedList::insert_last()
{
int val;
cout<<\"Enter the val to be inserted: \";
cin>>val;
struct node *tempNode, *tempNode;
tempNode = create_node(val);
tempNode = startNode;
while (tempNode->next != NULL)
{
tempNode = tempNode->next;
}
tempNode->next = NULL;
tempNode->next = tempNode;
cout<<\"Element Inserted\";
}
// deleting element
void LinkedList::delete()
{
int position, i, count = 0;
if (startNode == NULL)
{
cout<<\"Linkedlist empty\"<>position;
struct node *tempNode, *ptr;
tempNode = startNode;
if (position == 1)
{
startNode = tempNode->next;
}
else
{
while (tempNode != NULL)
{
tempNode = tempNode->next;
count++;
}
if (position > 0 && position <= count)
{
tempNode = startNode;
for (i = 1;i < position;i++)
{
ptr = tempNode;
tempNode = tempNode->next;
}
ptr->next = tempNode->next;
}
else
{
cout<<\"Enter correct position\";
}
}
}
// search data in linked list
void LinkedList::search()
{
int val, position = 0;
bool flagValue = false;
if (startNode == NULL)
{
cout<<\"List is empty\";
return;
}
cout<<\"Enter the val to search: \";
cin>>val;
struct node *tempNode;
tempNode = startNode;
while (tempNode != NULL)
{
position++;
if (tempNode->val == val)
{
flagValue = true;
cout<<\"Data \"<next;
}
if (!flagValue)
cout<<\"data not found\";
}
// linkedlist reverese
void LinkedList::reverse()
{
struct node *pointer1, *pointer2, *pointer3;
if (startNode == NULL)
{
cout<<\"List is empty\"<next == NULL)
{
return;
}
pointer1 = startNode;
pointer2 = pointer1->next;
pointer3 = pointer2->next;
pointer1->next = NULL;
pointer2->.Read less