Queue problem
There something wrong in the code. I can enter the number, but i can't remove and show the number. Pleas help me to solve this code. TQ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include <iostream>
//using namespace std;
const int size=10;
class Queue
{
private:
int front; // index at front
int back; // index at rear queue
char items[size]; //store item in Q
public:
Queue(); // Constructor - create Q
~Queue(); // Destructor - destroy Q
bool isEmpty(); // check Q empty
bool isFull(); // check Q full
void enQueue(char); // insert into Q
void deQueue(char); // remove item from Q
void show();
char getFront(); // get item at Front
char getRear(); // get item at back Q
};
Queue::Queue()
{ front = 0;
back = -1;
}
Queue::~Queue()
{
delete[]items;
}
bool Queue::isEmpty()
{
return bool (back < front);
}
bool Queue::isFull()
{return bool(back==size-1);
}
void Queue::enQueue(char insertItem)
{ if (isFull())
cout<< "\nCannot Insert. Queue is full!";
else
{ //insert at back
back++;
items[back] = insertItem;
} // end else if
}
char Queue:: getFront() // get item at Front
{
return items[front] ;
}
char Queue::getRear() // get item at Back
{
return items[back] ;
}
void Queue::deQueue(char deletedItem)
{ if (isEmpty())
cout<< "\nCannot remove item. Empty Queue!";
else
{ //retrieve item at front
deletedItem = items[front];
front++;
} // end else if
}
void Queue::show()
{
if(front==back)
{
cout <<" queue empty";
return;
}
for(int x=front+1;x<=back;x++)
cout<<items[x]<<" ";
}
int main ()
{
int choose;
Queue queue;
while(1)
{
cout<<"\n";
cout<<"\n\t\t MENU\n\n";
cout<<"1:ADD, 2:REMOVE, 3:SHOW, 4:EXIT";
cout<<"\nChoose the menu: ";
cin>>choose;
switch(choose)
{
case 1: cout <<"enter the element: ";
cin >> choose;
queue.getFront();break;
case 2: cout<<" "<<queue.getRear();break;
case 3: queue.show(); break;
//case 4: exit(0);
}
}
}
|
The problem is that you don't add the number to the queue.
1 2 3 4 5
|
case 1:
cout <<"enter the element: ";
cin >> choose;
queue.getFront();
break;
|
Also you can't delete a static array.
1 2 3 4
|
Queue::~Queue()
{
delete [] items;
}
|
I just wonder why it doesn't crash.
do you have any idea to solve this problem ? can share with me
1 2 3 4 5
|
case 1:
cout <<"enter the element: ";
cin >> choose;
queue.enQueue(choose);
break;
|
Also delete the line
delete[]items;
in the destructor.
Did you write the class Queue?
i have tried to change the code, but still can't display deQueue and show in the output. Is there a mistake in the program ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream>
using namespace std;
const int size=10;
class Queue
{
private:
int front; // index at front
int back; // index at rear queue
char items[size]; //store item in Q
public:
Queue(); // Constructor - create Q
//~Queue(); // Destructor - destroy Q
//bool isEmpty(); // check Q empty
//bool isFull(); // check Q full
void enQueue(char); // insert into Q
void deQueue(char); // remove item from Q
void show();
//char getFront(); // get item at Front
//char getRear(); // get item at back Q
};
Queue::Queue()
{ front = 0;
back = -1;
}
/* Queue::~Queue()
{
delete[]items;
}
bool Queue::isEmpty()
{
return bool (back < front);
}
bool Queue::isFull()
{return bool(back==size-1);
}*/
void Queue::enQueue(char insertItem)
{ if (back==size-1)
cout<<"\nCannot Insert. Queue is full!";
else
{ //insert at back
cout<<"enter data to enqueue"<<insertItem;
back++;
items[back] = insertItem;
} // end else if
}
/* char Queue:: getFront() // get item at Front
{
return items[front] ;
}
char Queue::getRear() // get item at Back
{
return items[back] ;
}
*/ void Queue::deQueue(char deletedItem)
{ if (back<front)
cout<< "\nCannot remove item. Empty Queue!";
else
{ //retrieve item at front
cout<<"the data dequeue is: "<<deletedItem;
deletedItem = items[front];
front++;
} // end else if
}
void Queue::show()
{
if(front==back)
{
cout <<" queue empty";
return;
}
for(int x=front+1;x<=back;x++)
cout<<items[x]<<" ";
}
int main ()
{
int choose;
Queue queue;
while(1)
{
cout<<"\n";
cout<<"\n\t\t MENU\n\n";
cout<<"1:ADD, 2:REMOVE, 3:SHOW, 4:EXIT";
cout<<"\nChoose the menu: ";
cin>>choose;
switch(choose)
{
case 1: cout <<"enter the element: ";
cin >> choose;
queue.enQueue(choose);
break;
case 2: queue.deQueue(choose); break;
case 3: queue.show(); break;
case 4:
exit(0);
break;
default:
cout<<"\nInvalid Input. Try again! \n";
break;
}
}
return 0;
}
|
Topic archived. No new replies allowed.