linklist.cpp

 #include<iostream.h>
#include<new.h>
#include<process.h>

struct node
{
float num;
node *link;
};

node * insertbegin(float item, node *p)
{
if(p==NULL)
{
p=new node;
p->num=item;
p->link=NULL;
return(p);
}
node *temp;
temp=new(node);
temp->num=item;
temp->link=p;
p=temp;
return(p);
}

void insertend(float item, node *p)
{
node *temp;
temp=new(node);
temp->num=item;
temp->link=NULL;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
}

void insertafter(float item, float search, node *p)
{
node *temp;
temp=new(node);
temp->num=item;
temp->link=NULL;
while(p->num!=search)
{
p=p->link;
}
temp->link=p->link;
p->link=temp;
}

void insertbefore(float item, float search, node *p)
{
node *temp,*x;
temp=new(node);
temp->num=item;
temp->link=NULL;
while(p->num!=search)
{
x=p;
p=p->link;
}
temp->link=x->link;
x->link=temp;
}

node * insertbpos(float item, int pos, node *p)
{
node *temp,*x,*y;
temp=new(node);
temp->num=item;
temp->link=NULL;
y=p;
for(int i=1;i<pos;i++)
{
x=y;
y=y->link;
}
if(x==p)
{
p=insertbegin(item,p);
return(p);
}
temp->link=x->link;
x->link=temp;
return(p);
}

node * insertatpos(float item, int pos, node *p)
{
node *temp,*y,*x;
temp=new(node);
temp->num=item;
temp->link=NULL;
y=p;
for(int i=1;i<pos;i++)
{
x=y;
y=y->link;
}
if(y==p)
{
p=insertbegin(item,p);
return(p);
}
temp->link=y;
x->link=temp;
return(p);
}

node * upgrad(float item, int pos, node *p)
{
node *y;
y=p;
for(int i=1;i<pos;i++)
{
y=y->link;
}
if(y==p)
{
p->num=item;
return(p);
}
y->num=item;
return(p);
}

node * delnum(float delnum, node *p)
{
node *y,*temp;
temp=p;
while(temp->num!=delnum)
{
y=temp;
temp=temp->link;
}
if(temp==p)
{
temp=temp->link;
return(temp);
}
y->link=temp->link;
return(p);
}

node * delpos(int pos, node *p)
{
node *y,*temp;
temp=p;
for(int i=1;i<pos;i++)
{
y=temp;
temp=temp->link;
}
if(temp==p)
{
temp=temp->link;
return(temp);
}
y->link=temp->link;
return(p);
}

void display(node *p, int k)
{
int count=0;
cout<<"\n\nThe elements in the list are:\n "<<endl<<endl;
while(p!=NULL)
{
cout<<p->num;
if(count<k-1)
cout<<"->";
p=p->link;
count++;
}
}

int search(node *p, float data)
{
int i,flag;
i=flag=0;
if(p==NULL)
{
cout<<"The list is empty"<<endl;
return NULL;
}
while(p!=NULL)
{
i++;
if(p->num==data)
{
flag=1;
break;
}
p=p->link;
}
if(flag==1)
{
cout<<"The element is found at position "<<i<<endl;
return(flag);
}
else
{
cout<<"The element is not found in the list"<<endl;
return(flag);
}
}


void main()
{
int n,pos,k,m=0;
float item,data;
k=0;
node *p;
p=NULL;
while(m!=1)
{
cout<<"\n\nEnter the choice you want to perform"<<endl;
cout<<"1. Insert in the begining"<<endl
<<"2. Insert a node before a node"<<endl
<<"3. Insert a node after a node"<<endl
<<"4. Insert in the end"<<endl
<<"5. To insert a node before a position"<<endl
<<"6. To insert a node at a position"<<endl
<<"7. Delete a num"<<endl
<<"8. To delete a node from a given postion"<<endl
<<"9. To upgrad a node in a postion"<<endl
<<"10. To display"<<endl
<<"11. To search an element"<<endl
<<"12. To exit"<<endl;
cin>>n;
switch(n)
{
case 1: cout<<"\nEnter the num to be inserted: ";
cin>>item;
p=insertbegin(item,p);
k++;
break;

case 2: cout<<"\nEnter the num after which to be entered: ";
cin>>data;
if(search(p,data))
{
cout<<"\nEnter the data to be entered :";
cin>>item;
insertbefore(item,data,p);
k++;
}
else
{
cout<<"\nPlease try again"<<endl;
}
break;

case 3: cout<<"\nEnter the num after which to be entered: ";
cin>>data;
if(search(p,data))
{
cout<<"\nEnter the data to be entered :";
cin>>item;
insertafter(item,data,p);
k++;
}
else
{
cout<<"\nPlease try again"<<endl;
}
break;


case 4: cout<<"\nEnter the num to be entered: ";
cin>>item;
insertend(item,p);
k++;
break;

case 5: cout<<"\nEnter the position before which the node to be inserted: ";
cin>>pos;
if(pos<=k)
{
cout<<"\nEnter the num to be entered: ";
cin>>item;
p=insertbpos(item,pos,p);
k++;
}
else
cout<<"The position is not valid"<<endl;
break;

case 6: cout<<"\nEnter the position at which the node to be inserted: ";
cin>>pos;
if(pos<=k+1)
{
cout<<"\nEnter the num to be entered: ";
cin>>item;
p=insertatpos(item,pos,p);
k++;
}
else
cout<<"The position is not valid"<<endl;
break;

case 7: cout<<"\nEnter the num to be deleted: ";
cin>>item;
if(search(p,item))
{
p=delnum(item,p);
k--;
cout<<"\nThe element is deleted"<<endl;
}
else
cout<<"\nPlease try again"<<endl;
break;

case 8: cout<<"\nEnter the position of which the node to be deleted :";
cin>>pos;
if(pos<=k)
{
p=delpos(pos,p);
k--;
cout<<"\nThe element is deleted"<<endl;
}
else
cout<<"\nThe entered postion is not valid"<<endl;
break;

case 9: cout<<"\nEnter the position where upgradion is needed :";
cin>>pos;
if(pos<=k)
{
cout<<"\nEnter the num to be entered: ";
cin>>item;
p=upgrad(item,pos,p);
}
else
cout<<"\nThe entered position is not valid"<<endl;
break;

case 10: if(k==0)
{
cout<<"The list is empty"<<endl<<endl;
}
else
{
display(p,k);
cout<<"\nThere are "<<k<<" nodes in the list"<<endl;
}
break;

case 11: cout<<"\n\nEnter the element to be searched :";
     cin>>data;
search(p,data);
     break;

case 12: m=1;
break;

default: cout<<"\nThe entered choice is not valid"<<endl;
     break;
}
}
}


Project Homepage: