hey guys.. am new to circular queues..i wrote a program to push (insert), pop (delete), and elements..but how to check if it is full???and how can i search for an element!!!!i really don't know what to do..please if someone knows how to write the functions, i would appreciate any help..thanx.. i wrote the program using pointers, not arrays..i think u can understand easily; it's simple..the program is: Code: 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<<"circular 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; }
I also don't know how to check if the list is "full" - can you explain what you mean by that? Is there a maximum number of entries the list can have? To find an entry you would start at the beginning, check if it's what you're looking for, if it isn't go to the next and repeat until a suitable end condition occurs. What do you think the end conditions might be? If you draw a list out on a sheet of paper with various data items at each point you can play at being the computer and see how you might find it, then work out how you did that and translate that into an algorithm, which you then translate into code. This is called "dry running", and is an invaluable programming technique. Or you could think of the numbers on a clock face. Let's say we define the beginning to be 9. How would you find 3? What would the program do if you were to try to find 27, and how would you avoid an infinite loop where the program goes round and round the list forever?
I think it is helpful to find any no in circular queue Code: void cirque::display() { if(front==NULL) { cout<<endl<<"queue is empty"; return; } int local = count; node *temp; temp=front; cout<<endl<<"Enter the number which you want to find: "; int no; cin>>no; while(local) { temp=temp->link; count++; if(temp->data==no) { cout<<no<<"Number is found at "<<count<<"position"; } local--; } }