943,962 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2799
  • C++ RSS
Oct 29th, 2005
0

queue implementation error

Expand Post »
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:
Quote ...

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??
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Oct 29th, 2005
0

Re: queue implementation error

I don't quite understand what it is you're doing here:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Daishi is offline Offline
80 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: sorting algorithm not working correctly
Next Thread in C++ Forum Timeline: New to C++ need some help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC