Advertisements
Advertisements
प्रश्न
Wnte the definition of a member function AddPac ket() for a class QUEUE in C++, to remove/delete a Packet from a dynamically allocated QUEUE of Packets' considering the following code is already written as a part of the program.
struct Packet
{
int PID;
char Address[20];
Packet *LINK;
};
class QUEUE
{
Packet *Front, *Rear;
public:
QUEUE(){Front=NULL;Rear=NULL;}
void AddPacket();
void DeletePacket();
~QUEUE();
};
उत्तर
void QUEUE::AddPacket()
{
Packet *temp = new Packet;
if(temp==NULL)
{
cout<<“Overflow”<<endl;
}
cout<< “Enter PID”;
cin >> temp->PID;
int n = temp->PID;
temp -> Link=NULL;
// for first node
if(Front == NULL)
{
Front = Rear = temp;
}
else
{
Rear->Link = temp;
Rear = temp;
}
cout<< n << "has been inserted successfully."<<endl;
//display
if(Front= =NULL)
{
cout<< “Underflow.”<<endl;
return;
}
else
{
temp = Front;
//will check until NULL is not found
while(temp)
{
cout << temp -> PID << “ “;
temp = temp -> Link;
}
cout<<endl;
}
}
void QUEUE::DeletePacket()
{
if(Front == NULL)
{
cout<< "underflow"<< endl;
return;
}
cout << Front -> PID <<" is being deleted "endl;
if (Front = = Rear) // if only one node is there
Front = Rear = NULL;
else
Front = Front -> Link;
//display
if(Front == NULL)
{
cout<< "Underflow."<<endl;
return;
}
else
{
Packet *temp=Front;
// will check until NULL is not found
while(temp)
{
cout << temp -> PID << "";
temp = temp -> Link;
}
cout<<endl;
}
}
shaalaa.com
Queue (Array and Linked Implementation)
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?