Circular Queue..

harris's Avatar, Join Date: Mar 2009
Go4Expert Member
Code:
 
 /* Circular Queues */
#include<iostream.h>
#include<conio.h>
#include <stdlib.h>
#include <string.h>
const int MAX = 5;
class cqueue
{
      int a[MAX],front,rear;
      public :
     cqueue()
     {
    front=rear=-1;
     }
     void insert(char);  // THIS IS THE PROBLEM, I changed int to char
     int deletion();
     void display();
};
void cqueue :: insert(char TaxLic) // related
{
   if((front==0 && rear==MAX-1) || (rear+1==front))
     cout<<" Circular Queue is Full";
   else
   {
     if(rear==MAX-1)
     rear=0;
     else
    rear++;
     a[rear]=TaxLic; //related
   }
   if(front==-1)
     front=0;
   }
   int cqueue :: deletion()
   {
   int k;
   if(front==-1)
   cout<<"Circular Queue is Empty";
   else
   {
   k=a[front];
   if(front==rear)
      front=rear=-1;
   else
   {
      if(front==MAX-1)
      front=0;
      else
      front++;
   }
   }
   return k;
   }
   void cqueue :: display()
   {
    int i;
    if(front==-1)
    cout<<"Circular Queue is Empty";
    else
    {
    if(rear < front)
    {
    for(i=front;i<=MAX-1;i++)
       cout<<a[i]<<"   ";
    for(i=0;i<=rear;i++)
       cout<<a[i]<<"   ";
    }
    else
    {
    for(i=front;i<=rear;i++)
       cout<<a[i]<<"   ";
    cout<<endl;
    }
    }
   }
int main()
   {
   cqueue c1;
         char TaxLic [7];
         char Dname [20];
         char TaxTyp [15];
   int ch,val;
   char op;
   do
   {
     //clrscr();
     system("cls");
     cout<<"-----------Menu-------------\n";
     cout<<"1.Insertion\n";
           cout<<"2.Deletion\n";
           cout<<"3.Display\n";
           cout<<"4.Exit\n";
     cout<<"Enter Your Choice <1..4> ?";
     cin>>ch;
     switch(ch)
     {
      case 1 : cout<<"Enter Taxi License Number: ";
               fflush(stdin);    
                        gets(TaxLic);
      //cin>>val;
      
      c1.insert(TaxLic); // can't insert because of char type
      break;
      case 2 : val=c1.deletion();
      cout<<"Deleted Element :"<<val<<endl;
      break;
      case 3 : c1.display();
      break;
   }
   cout<<"Do you want to continue<Y/N> ?";
   fflush(stdin);
   cin>>op;
    }while(op=='Y' || op=='y');
     getch();
  }
I put a sign in it.. The coding is working but when i tried to change the date tyoe from int (TaxType) to char, it's not working..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The problem is not with the int and char but the problem it looks like is for char and char array
0
harris's Avatar, Join Date: Mar 2009
Go4Expert Member
Ehm.. could ya be more detail pls?? still very new to c obviously i am..

Basically i just want to change from int datatype to char but not really sure how to it...

By default it's int TaxLic, i want to change it into char TaxLic but it gives me invalid conversion errors..
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Code:
c1.insert(TaxLic); // can't insert because of char type
in this inline the variable passed is char array and not char
0
harris's Avatar, Join Date: Mar 2009
Go4Expert Member
so, what's the solution??
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
If you want to deal with characters pass the character and not the array
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
You're also confused about the difference between char and char[]. A char represents a *single* character, such as 'a'. You cannot store multiple characters in a char, you need to use an array of char (aka char[] or char*), with a NULL terminator. "Hello" is equivalent to 'H','e','l','l','o','\0' and is therefore SIX bytes long (that's why you get +1's all over C strings).

So c1.insert(TaxLic); is fine.
void insert(char); needs changing to void insert(char*);
and void cqueue :: insert(char TaxLic) to void cqueue :: insert(char* TaxLic)

Now, a[rear]=TaxLic; is a lot more complicated and this particular enhancement should be left alone until you're more familiar with C. However if you want to proceed anyway... The variable a needs to be an array of char* - read that correctly - a must contain multiple pointers to char. So the definition of a will need to be char *a[MAX]. Or if you're happy to use fixed length strings, let's say 10 characters each, it would be char a[MAX][10] (or char a[10][MAX] - I can never remember if char[x][y] defines x strings of y length or y strings of x length; usually a core dump sorts it out...).

If you're going to use variable length strings then char *a[MAX] is fine, BUT you will need to manage the memory pointed to by each entry in a. I suggest you initialise each member of a in the constructor to zero and use that to determine whether or not any given element points to valid memory.

So a[rear]=TaxLic; needs to become:
Code:
{
  if (a[rear]) { free(a[rear]); a[rear]=0; }
  a[rear]=malloc(strlen(TaxLic]+1);
  strcpy(a[rear],TaxLic);
}
You will also need to alter the code for any other references to a. This will show you the need to use more sensible variable names; search the code for any reference to "a" and you will soon get annoyed with hitting Search Again over and over and hitting a's embedded in other words and variables. If a was instead called even something simple like "arr" this would be a lot easier.

So k=a[front]; won't work either and deletion() will need substantially rewriting to solve this one, IF it really needs to return what it has deleted. If you don't really need the deleted value to be returned then just change k=a[front]; to { free(a[front]); a[front]=0; }, change the return type of deletion to void and remove the return statement.

If you really want to return something then define deletion to return a char*, malloc the memory in deletion and return the pointer to that memory, BUT you must remember to free() that pointer somewhere otherwise you will have a memory leak because NOTHING - not even obvious stuff - is done automatically for you in C.
Code:
char *cqueue::deletion()
{
  char *k=malloc(strlen(a[front])+1);
  strcpy(k,a[front]);
  // other code
  free(a[front]);
  a[front]=0;
  // other code
  return k;
}

// then maybe something like:

case 2 : 
{ 
  char *val=c1.deletion();
  cout<<"Deleted Element :"<<val<<endl;
  free(val);
  break;
}
If you're using fixed length strings then cqueue::display() doesn't get much simpler unfortunately. You can't return a stack variable to the calling function because the stack frame is invalid when the function returns, so you can't do
Code:
char *cqueue::display()
{
  char k[10];
  //...
  return k;
}
and instead k should be defined by the caller, for example:
Code:
case 2 : 
{ 
  char k[10];
  c1.deletion(k);
  cout<<"Deleted Element :"<<val<<endl;
  free(val);
  break;
}

// and:

void cqueue::display(char *k)
{
  // other code
  // change k=a[front]; to:
  strcpy(k,a[front]);
  // other code
}
0
hanann's Avatar, Join Date: Oct 2009
Light Poster
if i wont to use queue of strings (store/ delete/ and print)
How does this work?
what are the main function here!
can I use char** array
??