hello,
ok i have this simple linked list program, as u can see below i got three functions: one to insert a node in the first of the list, the second to insert a node in the middle and the third to insert a node in the last of the list.
And now i want to merge all of them in only one function that will sort the list and decide where to put the new node according to its value.
Can you help me please?
#include<iostream.h>
class node{
public:
int data;
node* next;
node(int x){
data=x;
next=NULL;};
void display(){
cout<<data<<" ";
};
};
class linked_list{
private:
node* first;
public:
linked_list(){
first=NULL;};
void insert_last(int d){
node* newnode=new node(d);
node* current=first ;
if(first==NULL)
first=newnode;
else{
while(current->next!=NULL)
current=current->next;
current->next=newnode;
}};
void insert_first(int z){
node* newnode= new node(z);
if(first==NULL)
first=newnode;
else{
newnode->next=first;
first=newnode;}
};
void display(){
node* current=first;
while(current!=NULL){
current->display();
current=current->next;}
};
void insert_mid(int d){
node* newnode=new node(d);
node* current=first;
node* temp;
while(current->next->data<d)
current=current->next;
temp=current->next;
current->next=newnode;
newnode->next=temp;
};
int l_delete(int x){
node* current=first;
if(first==NULL){
cout<<"List is empty"<<endl;
return -1;}
if(first->data==x)
first=first->next;
else{
while((current->next->data!=x)&&(current->next!=NULL))
current=current->next;}
if(current->next==NULL)
cout<<"error"<<endl;
else{
current->next=current->next->next;
delete current;}
return x;};
};
main(){
linked_list list;
int no, g, h, d, e;
char c, a0, a1, a2, a3;
do{
cout<<"Please choose the number of the option"<<endl<<endl;
cout<<"\t\t______________________ MENU _____________________"<<endl;
cout<<"\t\t1. Add new node to the end of the list"<<endl;
cout<<"\t\t2. Add new node to the beginning of the list"<<endl;
cout<<"\t\t3. Add new node in the middle of the list"<<endl;
cout<<"\t\t4. Delete a node from the list"<<endl;
cout<<"\t\t5. Display the list"<<endl;
cout<<"\t\t_________________________________________________"<<endl;
cin>>no;
switch(no){
case 1: do{
cout<<"Please enter the element you want to insert to the node:"<<endl;
cin>>g;
list.insert_last(g);
cout<<"Do you want to add more to the end of the list? y/n"<<endl;
cin>>a0;}
while(a0=='y');
break;
case 2: do{
cout<<"Please enter the element you want to insert to the node:"<<endl;
cin>>h;
list.insert_first(h);
cout<<"Do you want to add more to the beginning of the list? y/n"<<endl;
cin>>a1;}
while(a1=='y');
break;
case 3: do{
cout<<"Please enter the element you want to insert to the node:"<<endl;
cin>>d;
list.insert_mid(d);
cout<<"Do you want to add more to the mid of the list? y/n"<<endl;
cin>>a2;}
while(a2=='y');
break;
case 4: do{
cout<<"Please enter the value of the element you want to delete:"<<endl;
cin>>e;
list.l_delete(e);
cout<<"Do you want to delete more from the list? y/n"<<endl;
cin>>a3;}
while(a3=='y');
break;
case 5: list.display();
cout<<endl;
break;
default:
cout<<"Error in number"<<endl;
break;}
cout<<"Do you want to continue? y/n"<<endl;
cin>>c;}
while(c=='y');
}