DELETION_AT_ANYWHERE

DELETION_AT_ANY_POSITION

HOME
/*PROGRAM
FOR DELETION OF ELEMENTS AT ANYWHERE*/


#include”iostream.h”

class
List//NAME OF THE
CLASS

{
private: 
//DATA MEMBERS

  
 int data;

  
 List *next;

public:       
 

  
 void insert_end(int);

  
 int del_any();  //MEMBER FUNCTIONS

   
void display();

}
*head;  //OBJECT
ACTS AS NODE

//FUNCTION
DECLARATION

void
List::insert_end(int ele)

{

 
List *n,*temp;

   
n=new(List);//MEMORY
ALLOCATION

  
 n->data=ele;

  
 n->next=NULL;

/*IF
THE LIST IS EMPTY THEN ADD 1st NODE AS      
HEADER NODE*/

if(head==NULL)
  
 head=n;

else
{
  
temp=head;

while(temp->next!=NULL)
  
 temp=temp->next;

  
  temp->next=n;

}

}

/*FUNCTION
FOR DELETING NODE AT SPECIFIED   POSITION*/

int
List::del_any()

{
  
 int z,flag=1;

List
*temp,*old;

cout<<“ENTER
THE ELEMENT TO BE DELETED”;

cin>>z;
temp=old=head;
while(temp!=NULL)
{
  
     

if(temp->data==z)
  
     

{
flag=0;
if(temp==head)
 head=temp->next;
else
old->next=temp->next;
  
         

delete
temp;

return(0);
  
         

}
 else
{
old=temp;
temp=temp->next;
}
  
 

}
if(flag==1)
cout<<“ELEMENT
NOT FOUND\n”;

return(0);
}
void
List::display()
//DISPLAY THE RESULT

{
List 
*temp;

  
 

if(head==NULL)
  
     

cout<<“LIST
IS EMPTY\n”;

else
{
temp=head;
while(temp!=NULL)
{
  
         

cout<<temp->data<<endl;
temp=temp->next;
}
}
}

void
main() //main()
FUNCTION STARTS

{
head=NULL;//INITIALIZATION OF HEADER
NODE

List
l; //OBJECT OF THE
CLASS List

int
x,a[20];

cout<<“ENTER
THE NO OF ELEMENTS TO BE INSERTED\n”;

cin>>x;
 cout<<“ENTER
“<<x<<“ELEMENTS\n”;

for(int
i=0;i<x;i++)

{
cin>>a[i];
l.insert_end(a[i]);
 
/*PASING THE ELEMENTS
TO THE FUNCTION THROUGH OBJECT*/

}
cout<<“THE
ELEMENTS IN THE LIST ARE\n”;

l.display();
l.del_any();
cout<<“THE
LIST IS\n”;

l.display();

/*CALLING
FUNCTION TO DISPLAY RESULT THROUGH OBJECT*/

}
//PROGRAM
ENDS

/*
OUTPUT:
ENTER
THE NO OF ELEMENTS TO BE INSERTED

5
ENTER
5ELEMENTS

1
2
3
4
5
THE
ELEMENTS IN THE LIST ARE

1
2
3
4
5
ENTER
THE ELEMENT TO BE DELETED3

THE
LIST IS

1
2
4
5
Press
any key to continue

*/

HOME

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: