i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code
#include <iostream>
#include <stdlib.h>
using namespace std ;
struct Node{
int data;
Node *link ;};
struct Node *head=NULL, *tail=NULL; /* pointers to Node*/
void InsertFront();
void InsertRear();
void DeleteFront();
void DeleteRear();
int main(){
int choice;
do{
cout << "1: Insert item at front\n";
cout << "2: Insert item at rear\n";
cout << "3: Delete item from front\n";
cout << "4: Delete item from rear\n";
cout << "6: Exit\n";
cout << "Enter your choice: ";
cin>>choice ;
switch (choice){
case 1:
InsertFront( ) ;
break ;
case 2:
InsertRear( ) ;
break ;
case 3:
DeleteFront( ) ;
break ;
case 4:
DeleteRear( ) ;
break ;
case 5:
cout<<"Exiting Program\n" ;
break ;
default:
cout<<"Error! wrong choice \n" ;
} }while (choice != 6);
return 0;}
void InsertFront(){
Node *temp = new Node;// Create new Employee
cout<< "Enter Integer Data: " ;
cin>>temp->data ;
temp->link = NULL ;
if (head==NULL){
head=temp;
tail=temp;
}else{temp->link=head;
head=temp;
}}
void InsertRear( ){
Node *temp = new Node;// Create new Employee
cout<< "Enter Integer Data: " ;
cin>> temp->data ;
temp->link = NULL ;
if (head==NULL) {
head=temp;
tail=temp;
}else{
tail->link=temp;
tail=temp;}}
void DeleteFront() {
Node *temp = head ;
if(head==NULL) cout<<"List is empty\n" ;
else{if(head == tail){head = NULL ;tail=NULL ;}
else{head = head->link ;}
cout<< temp->data << " has been deleted\n" ;
delete temp; }
cout << endl;}
void DeleteRear() {
Node *temp = tail;
if(head==NULL) cout<< "List is empty\n" ;
else
{Node *cur= head ;
if(head == tail)
{head=NULL;
tail = NULL;}
else {while(cur->link!=tail)
cur = cur->link ;
cur->link = NULL ;
tail = cur ;}
cout<< temp->data << " has been deleted\n";
delete temp ; }
cout << endl;}
#include <iostream>
#include <stdlib.h>
using namespace std ;
struct Node{
int data;
Node *link ;};
struct Node *head=NULL, *tail=NULL; /* pointers to Node*/
void InsertFront();
void InsertRear();
void DeleteFront();
void DeleteRear();
void InsertNth(Node*);
void printList(Node*);
int main(){
int choice;
do{
cout << "1: Insert item at front\n";
cout << "2: Insert item at rear\n";
cout << "3: Delete item from front\n";
cout << "4: Delete item from rear\n";
cout << "5: Insert item in middle\n";
cout << "6: Print the linked list\n";
cout << "7: Exit\n";
cout << "Enter your choice: ";
cin>>choice ;
switch (choice){
case 1:
InsertFront( ) ;
break ;
case 2:
InsertRear( ) ;
break ;
case 3:
DeleteFront( ) ;
break ;
case 4:
DeleteRear( ) ;
break ;
case 5:
InsertNth(head); // Call the InsertNth function.
break ;
case 6:
printList(head); // Call the printList function.
break;
default:
cout<<"Error! wrong choice \n" ;
} }while (choice != 7);
return 0;}
void InsertFront(){
Node *temp = new Node;// Create new Employee
cout<< "Enter Integer Data: " ;
cin>>temp->data ;
temp->link = NULL ;
if (head==NULL){
head=temp;
tail=temp;
}else{temp->link=head;
head=temp;
}}
void InsertRear( ){
Node *temp = new Node;// Create new Employee
cout<< "Enter Integer Data: " ;
cin>> temp->data ;
temp->link = NULL ;
if (head==NULL) {
head=temp;
tail=temp;
}else{
tail->link=temp;
tail=temp;}}
void DeleteFront() {
Node *temp = head ;
if(head==NULL) cout<<"List is empty\n" ;
else{if(head == tail){head = NULL ;tail=NULL ;}
else{head = head->link ;}
cout<< temp->data << " has been deleted\n" ;
delete temp; }
cout << endl;}
void DeleteRear() {
Node *temp = tail;
if(head==NULL) cout<< "List is empty\n" ;
else
{Node *cur= head ;
if(head == tail)
{head=NULL;
tail = NULL;}
else {while(cur->link!=tail)
cur = cur->link ;
cur->link = NULL ;
tail = cur ;}
cout<< temp->data << " has been deleted\n";
delete temp ; }
cout << endl;}
// Insert the node at particular position
void InsertNth(Node *head)
{
int position;
Node *add = new Node;
cout<< "Enter Integer Data: " ;
cin>>add->data ;
add->link = NULL;
cout << "Enter the position: ";
cin>>position;
if(head == NULL)
head = add;
else if(position == 1)
{
add->link = head;
head = add;
}
else
{
Node *cur = head;
int d = 2;
while(cur != NULL)
{
if(d == position)
{
add->link = cur->link;
cur->link = add;
break;
}
cur = cur->link;
d++;
}
}
}
// For print the linked list value.
void printList(Node* n)
{
while (n != NULL) {
cout << n->data << " ";
n = n->link;
}
cout << endl;
}
Output :
Get Answers For Free
Most questions answered within 1 hours.