| | |
queue implementation error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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:
---------------------
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??
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??
•
•
Join Date: Aug 2005
Posts: 80
Reputation:
Solved Threads: 2
I don't quite understand what it is you're doing here:
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
C++ Syntax (Toggle Plain Text)
void insert(int array[]) { int value; int *frontPtr = array; // referenced both pointers to first element into the array int *rearPtr = array; // ... 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; }
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
![]() |
Similar Threads
- Looking for open source message queue implementation (Java)
- helpe with classes (C++)
- Linker Error (C++)
- External queue struct using queue.c (C++)
- CPU Scheduling Algorithm (Java)
- How to free up memory used for queues? (C)
Other Threads in the C++ Forum
- Previous Thread: sorting algorithm not working correctly
- Next Thread: New to C++ need some help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





