#include<stdio.h>
#include<stdlib.h>

#define MAX 5
char queue[MAX][30];

int front, rear, temp;

void init()
{
	front = -1;
	rear = -1;
}

void insert()
{
	if ( rear >= MAX-1 && front == -1 )
	{
			printf("\nQueue overflow");
			return ;
	}

	temp = rear;
	rear = ( rear + 1 ) % MAX ;
	printf( "\nEnter name to be inserted :\n") ;
	scanf( "%s", queue[rear]) ;
	
}

void delete()
{
	if (front == rear || rear == -1)
		printf("\nError : Underflow");
	else {
	front = (front + 1) % MAX;
	printf("\nDeleted name from the CQ is %s",queue[front]);
	}
}

void display()
{
	int i;
	printf("\nThe queue content is :\n");
	if (front > rear)
		{
			for (i = front+1 ; i < MAX; i++)
				printf("%s\t",queue[i]);
			for (i = 0; i <= rear; i++)
				printf("%s\t",queue[i]);
		}
	else
		for (i = front +1 ; i <= rear; i++)
			printf("%s\t",queue[i]);
	printf("\n");
}

void front_rear()
{
	printf("Front = %d \n", front);
	printf("Rear = %d \n", rear);
}
	
int main()
{
	int choice;
	init();
	while(1)
	{
		printf("\nMENU\n1. Insert\n2. Delete\n3. Display\n4. Exit\n5.front_rear_pos\n");
		printf("\nYour choice = ?");
		scanf("%d",&choice);
		switch(choice)
		{
		case 1:
			insert();
			break;

		case 2:
			delete();
			break;

		case 3:
		      if (rear==front)
		         printf("\nNo elements in the list");
		   else
		           display();
		           break;

		case 4:
			exit(0);
		case 5:
			front_rear();
                                                break;

		default:
			printf("Invalid Choice");
                                                break;
		}
	}
   return 0;
}

i have written a code for circular Queue using array.

the probelm with this is :
1. when i enter all the 5 elements
the rear value is 4 and front value is -1 if i dont delete any element.
it( insert function) works fine .
10 20 30 40 50 are intial elements
here rear = 4 and front = -1;
then i deteted one element
now display works fine : 20 30 40 50 ( rear = 4 anf front = 0)

as its a circular queue the program should allow me to enter the number and store in the zero position as one element is deleted.

but when i display the list after inserting an element , it displays no elements.

i tried my level best but could not find the correct control statement .

i want my circular queue to be flexible .
when i delete an element it should allow me to store the value there and display all the elements including the previous elements.

plz sugget me how to solve it.
thanks,
Danian

#include<iostream.h>
      #include<conio.h>

      const int MAX = 5;
      class cqueue
      {
           int a[MAX],front,rear;
        public :
           cqueue()
           {
             front=rear=-1;
           }
           void insert(int );
           int deletion();
           void display();
      };

      void cqueue :: insert(int val)
      {
         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]=val;
         }
         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;
             }
          }
      }

      void main()
      {
         cqueue c1;

         int ch,val;
         char op;
         do
         {
           clrscr();
           cout<<"-----------Menu-------------
";
           cout<<"1.Insertion
2.Deletion
3.Display
4.Exit
";
           cout<<"Enter Your Choice <1..4> ?";
           cin>>ch;
           switch(ch)
           {
               case 1 : cout<<"Enter Element to Insert ?";
                        cin>>val;
                        c1.insert(val);
                        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> ?";
            cin>>op;
          }while(op=='Y' || op=='y');
           getch();
        }


/*this is in C++ hope u will find some idea from it*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.