#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
// function definitions
void insertFirst();
void insertLast();
void insertSpecified();
void deleteFirst();
void deleteLast();
void deleteSpecified();
void display();
node *head = NULL; //empty linked list
int info = 0, node_number = 0;
int ch;
int main()
{
char choice;
cout<<"Implementation of Linked List "<<endl<<endl;
cout<<"Choose any of the following option "<<endl<<endl;
do
{
cout<<"1: Insertion "<<endl;
cout<<"2: Deletion "<<endl<<endl;
cout<<"Choice:>> ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nopions to insert items in a Link List "<<endl<<endl;
cout<<"1: Insert from First "<<endl;
cout<<"2: Insert from Last "<<endl;;
cout<<"3: Insert in specific place "<<endl<<endl;
int no;
cout<<"choose:>> ";
cin>>no;
if(no==1)
insertFirst(); // calling insert function
else if(no==2)
insertLast(); // calling insertion function
else
insertSpecified();
display();
break;
}
case 2:
{
cout<<"\nOptions to delete items in a Link List "<<endl<<endl;
cout<<"1: delete from First "<<endl;
cout<<"2: delete from Last "<<endl;;
cout<<"3: delete in specific place "<<endl<<endl;
int no1;
cout<<"choose:>> ";
cin>>no1;
if(no1==1)
deleteFirst();
else if(no1==2)
deleteLast();
else
deleteSpecified();
display();
break;
}
default:
cout<<"Wrong Option, Plese Enter only 1 or 2"<<endl;
exit(0);
}
cout<<"\n\nDo you want to repeat the program? Enter Y/N: ";
cin>>choice;
}while(choice == 'y' || choice == 'Y');
return 0;
}
void insertFirst()
{
cout<<"Enter the number to insert in Link List:>> ";
cin>>info; // take input data
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp->data = info; // store data(first field)
temp->next=head; // store the address of the pointer head(second field)
head = temp; // transfer the address of 'temp' to 'head'
}
void insertLast()
{
// to check linked list is empty or not
if(head==NULL)
{
cout<<"Enter the number to insert in Link List:>> ";
cin>>info;
cout<<"Input data: "<<info;
node *temp; // to create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null
head = temp; // transfer the address of 'temp' to 'head'
}
else
{
cout<<"Enter the number to insert in Link List:>> ";
cin>>info; // take input data
cout<<"Input data: "<<info;
node *temp1; // create a temporary node
temp1=(node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; // transfer the address of 'head' to 'temp'
while(temp1->next!=NULL) // go to the last node
temp1 = temp1->next; //tranfer the address of 'temp->next' to 'temp'
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node));// allocate space for node
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null(last node)
temp1->next = temp; // 'temp' node will be the last node
//break;
}
}
void insertSpecified()
{
if(head==NULL)
{
cout<<"The Linked List is empty"<<endl;
//break;
}
cout<<"Enter the number to insert in Link List:>> :";
cin>>info; // take input data
cout<<"Input data: "<<info<<endl;
cout<<"Enter the number of of NODE :>> ";
cin>>node_number; // take the node number from user
node *temp1; // create a temporary node
temp1 = (node*)malloc(sizeof(node)); // allocate space for node
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next; // go to the next node
if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
break;
}
}
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp->data = info; // store data(first field)
temp->next = temp1->next;//transfer the address of temp1->next to temp->next
temp1->next = temp; //transfer the address of temp to temp1->next
}
void deleteFirst()
{
// check linked list is empty
if(head==NULL)
{
cout<<"The Linked List is empty"<<endl;
// break;
}
else // check linked list has only one node
if(head->next==NULL)
{
head = NULL;
cout<<"The first node of the Linked List is deleted"<<endl;
cout<<"The Linked List is empty"<<endl;
}
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp = head; // transfer the address of 'head' to 'temp'
head = temp->next; // transfer the address of 'temp->next' to 'head'
free(temp);
cout<<"The first node of the Linked List is deleted"<<endl;
}
void deleteLast()
{
// check linked list is empty
if(head==NULL)
{
cout<<"The Linked List is empty"<<endl;
}
else // check linked list has only one node
if(head->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
cout<<"The Linked List is empty"<<endl;
}
node *temp1; // create a temporary node
temp1 = (node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; //transfer the address of head to temp1
node *old_temp; // create a temporary node
old_temp = (node*)malloc(sizeof(node)); // allocate space for node
while(temp1->next!=NULL) // go to the last node
{
old_temp = temp1; // transfer the address of 'temp1' to 'old_temp'
temp1 = temp1->next; // transfer the address of 'temp1->next' to 'temp1'
}
old_temp->next = NULL; // previous node of the last node is null
free(temp1);
cout<<"The last node of the Linked List is deleted"<<endl;
}
void deleteSpecified()
{
// Check if link list is empty
if(head==NULL)
{
cout<<"The Linked List is empty"<<endl;
//reak;
}
else // check linked list has only one node
if(head->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
cout<<"The Linked List is empty"<<endl;
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number; // take location
// Check if node is exist
node *temp1; // create a temporary node
temp1 = (node*)malloc(sizeof(node)); // allocate space for node
temp1 = head; // transfer the address of 'head' to 'temp1'
node *old_temp; // create a temporary node
old_temp = (node*)malloc(sizeof(node)); // allocate space for node
old_temp = temp1; // transfer the address of 'temp1' to 'old_temp'
// Check node number is 1
if( node_number == 1 )
{
head = temp1->next; // transfer the address of 'temp1->next' to 'head'
free(temp1);
cout<<node_number<<" node of the Linked List is deleted"<<endl;
}
// Go to the node number of the node
for( int i = 1 ; i < node_number ; i++ )
{
old_temp = temp1; // store previous node
temp1 = temp1->next; // store current node
}
old_temp->next = temp1->next;// transfer the address of 'temp1->next' to 'old_temp->next'
free(temp1);
cout<<node_number<<" node of the Linked List is deleted"<<endl;
}
void display()
{
node *temp1; // create a temporary node
temp1 = head; // transfer the address of 'head' to 'temp'
if(temp1 == NULL)
{
cout<<endl<<"The linked list is empty"<<endl;
}
else
{
cout<<"Linked list: ";
while( temp1!=NULL )
{
cout<<temp1->data<<"::"; // show the data in the linked list
temp1 = temp1->next; // tranfer the address of 'temp->next' to 'temp'
}
}