queue implementation error

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

queue implementation error

 
0
  #1
Oct 29th, 2005
Hello, I've tried to implement a queue using a array which loops. (Circular lists). However something is wrong. When I add a new item to the list , I get the 'case digit' aswell...

heres the code :

[php]

#include <iostream>
#define MAXSIZE 3

using namespace std;

void insert(int array[]);
void print(int array[]);
void del(int array[]);

int main()
{
int selection;
int array[MAXSIZE] = {0};

cout << "Please select:\n 1 - Add\n 2 - Print\n 3 - Delete\n 4 - Quit" << endl;
cin >> selection;

while(selection !=4)
{
switch(selection)
{
case 1:
{
cout << "Calling function insert" << endl;
insert(array);
break;
}

case 2:
{
cout << "printing array function selected" << endl;
print(array);
break;
}

case 3:
{
cout <<"Calling deleting function" << endl;
del(array);
break;
}
default:
cout << "Error!" <<endl;
} // terminate switch


cout << "Please select:\n 1 - Add\n 2 - Print\n 3 - Delete\n 4 - Quit" << endl;

cin >> selection ;
} // terminate while

return 0;
}

void insert(int array[])
{
int value;

int *frontPtr = array; // referenced both pointers to first element into the array
int *rearPtr = array;

cout << "please enter an element you wish to add into the array\n";

cin >> value;

if(((*frontPtr == 0) && (*rearPtr == MAXSIZE)) || (*frontPtr == (*rearPtr + 1)))
{
cout << "Queue is full" << endl;
}

else
if(*frontPtr == 0)
{
*frontPtr = 1;
*rearPtr = 1;
}

else if
(*rearPtr == MAXSIZE)
{
*rearPtr = 1;
}

else
{
*rearPtr = *rearPtr + 1;
}

array[*rearPtr] = value;



}



void print(int array[])
{
cout << "Now printing the array\n"<<endl;

for (int i = 0 ; i <=MAX ; i++)
cout << "element : " << i << " ,has the value : " << array[i] <<" \n" <<endl;
}



void del(int array[])
{
int item;

cout << "Please enter an item to delete" << endl;
cin >> item;

int *frontPtr = array;
int *rearPtr = array;

if(*frontPtr == 0)
{
cerr << "YAK" << endl;
}

else
{
item = array[*frontPtr];
if(*frontPtr == *rearPtr)
{
*frontPtr = 0;
*rearPtr = 0;
}

else if(*frontPtr == MAXSIZE)
{
*frontPtr = 1;
}
else
{
*frontPtr = *frontPtr +1;
}
}



}

[/php]
output:

Please select:
1 add
2 Print
3 - Delete
4 - Quit

(user enters 2, which prints!)

element 0, has the value : 0

element 1, has the value : 0

element 2, has the value : 0

element 3, has the value : 2
---------------------

Why is it storing 2, when in fact it should be 0 hence I've set the array to 0 by array = {0}; and nothing as been entered??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: queue implementation error

 
0
  #2
Oct 29th, 2005
I don't quite understand what it is you're doing here:

  1. void insert(int array[])
  2. {
  3. int value;
  4.  
  5. int *frontPtr = array; // referenced both pointers to first element into the array
  6. int *rearPtr = array;
  7.  
  8. // ...
  9.  
  10. if(((*frontPtr == 0) && (*rearPtr == MAXSIZE)) || (*frontPtr == (*rearPtr + 1)))
  11. {
  12. cout << "Queue is full" << endl;
  13. }
  14. else
  15. if(*frontPtr == 0)
  16. {
  17. *frontPtr = 1;
  18. *rearPtr = 1;
  19. }
  20. else if
  21. (*rearPtr == MAXSIZE)
  22. {
  23. *rearPtr = 1;
  24. }
  25. else
  26. {
  27. *rearPtr = *rearPtr + 1;
  28. }
  29. array[*rearPtr] = value;
  30. }

It seems like you are trying to use the value of zero in the array to denote that it is not part of the list. Implementing a circular queue like that is asking for problems. I would have an array, a start index, and an end index. Also, from your insertion code it does not seem like you understand what the * operator does to pointers. That * dereferences whatever the pointer points to, so when you do *rearPtr = *rearPtr +1, you are adding one to the actual number that rearPtr points to in the circular array. When you do array[*rearPtr] = value, you are taking the value of rearPtr (which is a number in the array), using it as an index in the array, and assigning a value to it. I think that this tutorial on how pointers work would really help you out, you should understand them before continuing:

http://www.cplusplus.com/doc/tutorial/tut3-3.html

-Fredric
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC