#include <iostream>
#include <stdlib.h>
using namespace std;
int LIST_SIZE=0;
int No_of_List=0;
struct node {
int data;
node *next;
}*head1=NULL,*head2=NULL,*temp,*temp1;
bool Empty_List(struct node *head);
void Create(struct node **head);
void Insert(struct node *head);
void Delete(struct node *head);
void Merge(struct node *head, struct node *headtemp);
void Display(struct node *head);
void Operations();
int main() {
Create(&head1);
return 0;
}
bool Empty_List(struct node *head) {
if (head==NULL)
return true;
return false;
}
void Operations() {
int a,l;
if (No_of_List==2)
{
cout<<"\n1. Merge both lists (sorted)";
}
else
{
cout<<"\n1. Create another list";
}
cout<<"\n2. Insert value\n3. Delete value\n4. Display list\n5. Exit\n";
cout<<"Please select an operation (input no.): ";
cin>>a;
if (a==1)
{
if (No_of_List==0)
{
Create(&head1);
}
else if (No_of_List==1)
{
Create(&head2);
}
else Merge(head1,head2);
}
if (a==5)
{
exit(0);
}
if ((a==2 || a==3 || a==4) && No_of_List==2)
{
cout<<"\nChoose the list you want to operate on (1/2): ";
cin>>l;
}
if (l==1 || No_of_List==1)
{
switch(a) {
case 2: Insert(head1); break;
case 3: Delete(head1); break;
case 4: Display(head1); break;
default:
{
cout<<"Invalid choice. Try again";
Operations();
}
}
}
else if (l==2)
{
switch(a) {
case 2: Insert(head2); break;
case 3: Delete(head2); break;
case 4: Display(head2); break;
default:
{
cout<<"Invalid choice. Try again";
Operations();
}
}
}
}
void Create(struct node **head) {
if (No_of_List==2)
{
cout<<"\nCan't create more lists\n";
Operations();
}
int n;
cout<<"Create a linked list.\n\nEnter the number of elements in the Linked List: ";
cin>>n;
if (n!=0)
{
No_of_List++;
}
LIST_SIZE=n;
while(n--)
{
cout<<"Enter the value to be inserted: ";
node *new_node= new node;
cin>>new_node->data;
new_node->next=0;
if(Empty_List(*head)==1)
*head=new_node;
else
{
temp=*head;
while (temp->next!=NULL)
temp=temp->next;
temp->next=new_node;
}
}
Operations();
}
void Insert(struct node *head) {
int choice;
cout<<"\n1. Insert at index\n";
cout<<"2. Insert after data\n";
cout<<"Please choose an option: ";
cin>>choice;
node *new_node = new node;
cout<<"Enter the value to be inserted: ";
cin>>new_node->data;
new_node->next=NULL;
if (choice==1)
{
int index;
cout<<"Enter the index: ";
cin>>index;
if (index==0) // CONDITION NOT WORKING
{
new_node->next=head;
head=new_node;
cout<<"Value inserted successfully.\n";
LIST_SIZE++;
goto l;
return;
}
if (index>LIST_SIZE)
{
cout<<"Invalid index.";
goto l;
return;
}
temp=head;
temp1=NULL;
if (Empty_List(head)==1)
{
cout<<"\nLinked List empty.\n";
}
for (int i=0;i<index;i++)
{
temp1=temp;
temp=temp->next;
}
temp1->next=new_node;
new_node->next=temp;
cout<<"Value inserted successfully.\n";
LIST_SIZE++;
}
else if (choice==2)
{
int a,i=0;
cout<<"Enter the value after which new node is to be added: ";
cin>>a;
temp1=NULL;
temp=head;
while (temp!=0)
{
if (a==temp->data)
{
temp1=temp;
temp=temp->next;
temp1->next=new_node;
new_node->next=temp;
cout<<"Value inserted successfully\n";
LIST_SIZE++;
goto l;
}
temp1=temp;
temp=temp->next;
}
cout<<"Value not found in the list.";
}
else cout<<"Invalid choice.";
l:char c;
cout<<"\nWould you like to insert another number?(Y/N): ";
cin>>c;
if (c=='Y' || c=='y')
Insert(head);
else Operations();
}
void Delete(struct node *head) {
int choice;
cout<<"\n1. Delete at index\n";
cout<<"2. Delete after data\n";
cout<<"Please choose an option: ";
cin>>choice;
if (choice==1)
{
int index;
cout<<"Enter the index: ";
cin>>index;
if (index>LIST_SIZE)
{
cout<<"Invalid index.";
goto m;
return;
}
temp=head;
temp1=NULL;
if (Empty_List(head)==1)
{
cout<<"\nLinked List empty.\n";
}
if (index==0)
{
*temp=*head;
*head=*head->next;
goto n;
return;
}
if (index>=LIST_SIZE)
{
cout<<"\nInvalid Index.";
goto m;
}
for (int i=0;i<index;i++)
{
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
free(temp);
n: cout<<"Value deleted successfully.\n";
LIST_SIZE--;
}
else if (choice==2)
{
int a,i=0;
cout<<"Enter the value after which node is to be deleted: ";
cin>>a;
temp1=NULL;
temp=head;
while (temp!=0)
{
if (a==temp->data)
{
temp1->next=temp->next;
free(temp);
cout<<"Value deleted successfully\n";
LIST_SIZE--;
goto m;
}
temp1=temp;
temp=temp->next;
}
cout<<"Value not found in the list.";
}
else cout<<"Invalid choice.";
m: char c;
cout<<"\nWould you like to delete another number?(Y/N)";
cin>>c;
if (c=='Y' || c=='y')
Delete(head);
else Operations();
}
void Merge(struct node *head, struct node *head1) {
cout<<"\nList 1: ";
temp=head;
while (temp->next!=NULL)
{
cout<<"->"<<temp->data;
temp=temp->next;
}
cout<<"\nList 2: ";
temp1=head1;
while (temp1->next!=NULL)
{
cout<<"->"<<temp1->data;
temp1=temp1->next;
}
temp->next=head1;
for(temp=head;temp!=0;temp=temp->next)
{
for(temp1=temp->next;temp1!=0;temp1=temp1->next)
{
if(temp->data > temp1->data)
{
int tmp=temp->data;
temp->data=temp1->data;
temp1->data= tmp;
}
}
}
cout<<"\nMerged";
Display(head);
}
void Display(struct node *head) {
cout<<"\nLinked List : ";
temp=head;
while(temp!=0)
{
cout<<"->"<<temp->data;
temp=temp->next;
}
cout<<endl;
Operations();
}