0

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 :

#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;
			}
	}
	


}

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??

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Daishi
0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.