3.e) Define member functions queins( ) to insert nodes and quedel ( ) to delete nodes of the linked list implemented class queue, where each node has the following structure:
struct node
{ char name[20] ;
int age ;
node *Link ;
} ;
class queue
{ node *rear, *front ;
public :
queue( ) { rear = NULL;
front = NULL} ;
void queins( ) ;
void quedel( ) ;
} ;
Solution:
void queue::queins( )
{ node *ptr;
ptr=new node;
if(ptr= = NULL)
{
cout<<”\nNo memory to create a new node….”;
exit(1);
}
cout<<”\nEnter the name….”;
gets(ptr → name);
cout<<”\nEnter the age…”;
cin>>ptr → age;
ptr → Link=NULL;
if(rear= = NULL)
front=rear=ptr;
else
{
rear → Link=ptr;
rear=ptr;
}
}
void queue::quedel( )
{ node *temp;
if(front= = NULL)
cout<<”Queue Underflow”;
else
{ cout<<”\nThe name of the element to delete: “<<front → name;
cout<<”\nThe age of the element to delete: “<<front → age;
temp=front;
front=front → Link;
delete temp;
}
}
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )