i need help in making the size of the array to n size in queues.i have made a program help me in it.

////##############/////
#include <iostream>

using namespace std;

#define MAX 5           


class queue
{
 private:
 	int t[MAX];
 	int al;      
 	int dl;      

 public:
  queue()
  {
  	dl=-1;
  	al=-1;
  }

  void remove()
  {
     int tmp;
     if(dl==-1)
     {
	    cout<<"underflow Queue";
     }
     else
     {
		for(int j=0;j<=al;j++)
		{
	  		if((j+1)<=al)
	  		{
	  			tmp=t[j+1];
	  			t[j]=tmp;
	  		}
	  		else
	  		{
	  			al--;

	  		if(al==-1)
		 		dl=-1;
		 	else
		 		dl=0;
	  		}
		}
     }
  }

void insert(int item)
{
 	if(dl==-1 && al==-1)
 	{
  		dl++;
  		al++;
 	}
   else
   {
  		al++;
  		if(al==MAX)
   	{
			cout<<"overflow Queue\n";
			al--;
			return;
  		}
 	}
 	t[al]=item;

}

  void display()
  {
  	if(dl!=-1)
   {
   	for(int iter=0 ; iter<=al ; iter++)
      	cout<<t[iter]<<" ";
   }
   else
   	cout<<"EMPTY";
  }

};

int main()
{
 queue a;
 int data[5]={32,23,45,99,24};

 cout<<"Queue before inserting Elements: ";
 a.display();
 cout<<endl<<endl;

 for(int iter = 0 ; iter < 5 ; iter++)
 {
   a.insert(data[iter]);
   cout<<"inserting Number : "<<(iter+1)<<" : ";
   a.display();
   cout<<endl;
 }
 cout<<endl;
 cout<<"Queue after inserting Elements: ";
 a.display();
 cout<<endl<<endl;

 for(iter=0 ; iter < 5 ; iter++)
 {
   a.remove();
   cout<<"removing Number : "<<(iter+1)<<" : ";
   a.display();
   cout<<endl;
 }
 return 0;
}

Dani AI

Generated

The class in the first post uses a compile-time fixed array, which is why capacity is stuck at MAX. There are two practical paths to make the queue size ā€œnā€ at runtime: use the STL (recommended), or implement a dynamic circular buffer yourself. was right to ask whether you want to convert the static array to a dynamic one; both options are straightforward.

Easiest (use STL): let the standard library manage storage and edge cases. For basic FIFO behavior use std::queue (which uses std::deque by default) so you never worry about capacity:

#include <queue>

std::queue<int> q;
q.push(10);
q.push(20);
q.pop();

See std::queue and std::deque.

If you need an array-backed implementation with O(1) push/pop and runtime-resizable capacity, use a circular buffer on top of std::vector and grow it when full. Example pattern: keep head, tail, and count; on push, if count==buf.size() allocate a new vector (2x), copy elements starting from head, reset head to 0 and tail to count, then append. On pop, advance head modulo capacity and decrement count. This avoids shifting every element on removal and is far more efficient.

Quick troubleshooting notes for and :

  • Use && for logical AND; a single & is bitwise and often a logic bug in conditions.
  • Check capacity before incrementing indices; do bounds checks early.
  • Prefer size_t for sizes/indices.
  • If you use new[], implement a destructor and rule-of-three/five; otherwise prefer std::vector/std::queue to avoid manual memory errors.

If a simple, safe queue is fine, use the STL. If custom semantics or performance constraints require an array-backed queue, implement the circular-buffer pattern and grow the buffer on demand.

Recommended Answers

All 2 Replies

i am sorry i didnot get the idea do you want to convert the static array to dynamic one or what?

Explain in details how your program works. Did you find any error?

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.