Answer to Question #1576 in C++ for Nelson Mwaniki

Question #1576
Write a C++ program that adds and deletes items in a queue
1
Expert's answer
2011-03-07T09:07:00-0500
// Que.cpp : Defines the entry point for the console application.//

#include "stdafx.h"
#include #include using namespace std;
#define MAX 5 // MAXIMUM CONTENTS IN QUEUE


class queue
{
private:
int t[MAX];
int al; // Addition End
int dl; // Deletion End

public:
queue()
{
dl=-1;
al=-1;
}

void deleteItem()
{
int tmp;
if(dl==-1)
{
cout<<"Queue is Empty";
}
else
{
for(int j=0;j<=al;j++)
{
if((j+1)<=al)
{
tmp=t[j+1];
t[j]=tmp;
}
else
{
al--;

if(al==-1)
dl=-1;
else
dl=0;
}
}
}
}

void addItem(int item)
{
if(dl==-1 && al==-1)
{
dl++;
al++;
}
else
{
al++;
if(al==MAX)
{
cout<<"Queue is Full
";
al--;
return;
}
}
t[al]=item;

}

void display()
{
if(dl!=-1)
{
for(int iter=0 ; iter<=al ; iter++)
cout<<<" "; }
else
cout<<"EMPTY";
}

};
int _tmain(int argc, _TCHAR* argv[])
{
queue a;
int number_of_elements;
int data;
cout<<"Queue before adding Elements: ";
a.display();
cout<<<<"Enter number of elements in queue "; cin>>number_of_elements;

for(int iter = 0 ; iter < number_of_elements ; iter++)
{
cout<<"
Eneter elements ";
cin>>data;
a.addItem(data);
}
cout<<<"Queue after adding Elements: "; a.display();
cout<<<<"Deleting Elements:

"; for(int iter=0 ; iter < number_of_elements ; iter++)
{
a.deleteItem();
cout<<"Deletion Number : "<<(iter+1)<<" : ";
a.display();
cout<<<"
Press any key to exit"; getch();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS