Source Code For Circular Queue Using Arrays And Pointers In C++ Can You Help?

3

3 Answers

Rajat Dhande Profile
Rajat Dhande answered

Array
1- Array allocates space automatically
2- It cannot be resized
3- It cannot be reassigned
4- sizeof (arrayname) gives the number of bytes occupied by the array.
Pointer
1-Explicitly assigned to point to an allocated space.
2-It can be sized using realloc()
3-pointer can be reassigned.
4-sizeof (p) returns the number of bytes used to store the pointer variable p.

for more info on this visit

Sudhanshu Sharma Profile
/* circular queue program using pointer*/
/* every case is tested and shown in program*/
#include<iostream>
using namespace std;
struct node
{
 int data;
 node *link;
};
class cirque
{
private : Node *front,*rear;
    int count;
public:   cirque();
    void push(int n);
    void pop();
    void display();
    ~cirque();
};
cirque::cirque()
{
 front=rear=NULL;
 count=0;
}
void cirque::push(int n)
{
 node *temp;
 temp=new node;
 if(temp==NULL)
 {
  cout<<"memory is less";
  return;
 }
 temp->data=n;
 if(front==NULL)
 {
  front=rear=temp;
 }
 else
 {
 rear->link=temp;
 rear=rear->link;
 }
 rear->link=front;
 count++;
 
}
void cirque::pop()
{
 if(front==NULL)
 {
  cout<<"ciccular queue is empty";
  return;
 }
 node *temp;
 temp=front;
 cout<<endl<<"deleted item is"<<front->data;
 if(count>0)
 front=temp->link;
 rear->link=front;
 count--;
 delete temp;
}
void cirque::display()
{
 if(front==NULL)
 {
  cout<<endl<<"queue is empty";
  return;
 }
 int local = count;
 node *temp;
 temp=front;
 cout<<endl<<"queue elements are ";
 while(local)
 {
  cout<<" "<<temp->data;
  temp=temp->link;
  local--;
 }
}
cirque::~cirque()
{
 if(front==NULL)
 {
  cout<<endl<<"no memory used";
  return;
 }
 node *temp;
 int n=0;
 while(front->link!=rear->link)
 {
  temp=front;
  front=front->link;
  delete temp;
  cout<<endl<<++n<<" "<<"deleted ";
 }
}
int main()
{
 cirque q;
 q.push(1);
 q.display();
 q.push(2);
 q.display();
 q.push(3);
 q.display();
 q.push(4);
 q.display();
 q.pop();
 q.pop();
 return 0;
}
 
/*second program using array*/
#include<iostream>
using namespace std;
#define MAX 7  /*for array size*/
class cirq
{
private:int arr[MAX];
  int front,rear,count;  /*count to know the no of ele. Present in array.*/
public:
 cirq();
 void push(int n);
 void pop();
 void display();
 ~cirq();
};
cirq::cirq()
{
 front = rear =-1;
 count=0;
}
void cirq:: Push(int n)
{
 if(count<MAX)
 {
  if(front ==-1) 
  {
   front=rear=0;
  }
 else if(rear==MAX-1)  /* if rear is at maximum position then bring it in starting*/
   rear=0;
  else
   rear++;
  arr[rear]=n;
  count++;
 }
 else
 {
  cout<<endl<<"queue is full don't enter now";
 }
}
void cirq::pop()
{
 if(count>0)
 {
  cout<<endl<<"deleted item is"<<arr[front];
  if(front==MAX-1)  /* if front is at maximum position then bring it in starting*/
   front=0;
  else
   front++;
  count--;
 }
 else
  cout<<endl<<"circular queue is empty";
}
void cirq::display()
{
 int local = count;
 int front1=front;
 if(local<0)    /*if there is no element in quque*/
 {
  cout<<endl<<"circular queue is empty";
 }
 else
 {
  cout<<endl<<"circular queue elements are: ";
  while(local)
  {
   cout<<endl<<arr[front1];
   if(front1==MAX-1)   /* if front is at maximum position then bring it in starting*/
    front1=0;
   else
    front1++;
   local--;
  }
 }
}
cirq::~cirq()
{
}
int main()
{
 cirq q;
 q.push(1);
 q.push(2);
 q.push(3);
 q.push(4);
 q.push(5);
 q.push(6);
 q.push(7);
 q.display();
 q.pop();
 q.display();
 q.push(8);
 q.display();
 q.push(9);
 q.display();
 return 0;
}
 

 

 
Anonymous Profile
Anonymous answered
/* Program of circular queue using array in C*/
# include
#include
# define MAX 5

int cqueue_arr[MAX];
int front = -1;
int rear = -1;

main()
{
int choice;
while(1)
{
printf("1.Insertn");
printf("2.Deleten");
printf("3.Displayn");
printf("4.Quitn");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
Insert();
break;
case 2 :
Del();
break;
case 3:
Display();
break;
case 4:
Exit(1);
default:
Printf("Wrong choicen");
}/*End of switch*/
}/*End of while */
}/*End of main()*/

insert()
{
int added_item;
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow n");
return;
}
if (front == -1) /*If queue is empty */
{
front = 0;
rear = 0;
}
else
if(rear == MAX-1)/*rear is at last position of queue */
rear = 0;
else
rear = rear+1;
printf("Input the element for insertion in queue : ");
scanf("%d", &added_item);
cqueue_arr[rear] = added_item ;
}/*End of insert()*/

del()
{
if (front == -1)
{
printf("Queue Underflown");
return ;
}
printf("Element deleted from queue is : %dn",cqueue_arr[front]);
if(front == rear) /* queue has only one element */
{
front = -1;
rear=-1;
}
else
if(front == MAX-1)
front = 0;
else
front = front+1;
}/*End of del() */

display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is emptyn");
return;
}
printf("Queue elements :n");
if( front_pos

Answer Question

Anonymous