Deleting from Array Circular Queue

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Deleting from Array Circular Queue

 
0
  #1
Aug 28th, 2008
How do you delete the front cell of an array in an array based circular queue?

Here's the Queue cpp functions

  1. #include <iostream>
  2. #include <string>
  3. #include "Contributor.h"
  4. #include "myQueue.h"
  5. using namespace std;
  6.  
  7. myQueue::myQueue()
  8. {
  9. front=0;
  10. rear =0;
  11. Index=0;
  12. size= 0;
  13. ArrayQ[10];
  14. }
  15.  
  16.  
  17. myQueue::myQueue(const myQueue &CCmyQueue)
  18. {
  19. for(int i=0;i < CCmyQueue.Index;i++)
  20. {
  21. ArrayQ[i]=CCmyQueue.ArrayQ[i];
  22. front= CCmyQueue.front;
  23. rear =CCmyQueue.rear;
  24. Index=CCmyQueue.Index;
  25. size =CCmyQueue.size;
  26. }
  27. }
  28.  
  29.  
  30. myQueue &myQueue:: operator =(const myQueue & RHS)
  31. {
  32. if(this!= &RHS)
  33. {
  34. for (int i=0; i<RHS.Index;i++)
  35. {
  36. ArrayQ[i]= RHS.ArrayQ[i];
  37. Index = RHS.Index;
  38. rear= RHS.rear;
  39. front= RHS.front;
  40. size= RHS.size;
  41. }
  42. }
  43. return *this;
  44. }
  45.  
  46.  
  47. myQueue::~myQueue()
  48. {
  49. cout<< "default D-Tor"<<endl;
  50. }
  51.  
  52.  
  53. bool myQueue::enQueue(Contributor InData)
  54. {
  55. if(IsFull())
  56. {
  57. cout<< " Queue is full"<<endl;
  58. return false;
  59. }
  60. else
  61. {
  62. ArrayQ[rear]=InData;
  63. step(rear);
  64. Index++;
  65. }
  66. cout<<"Index ="<<Index<<endl;
  67. return true;
  68. }
  69.  
  70.  
  71. bool myQueue::deQueue()
  72. {
  73. if(IsEmpty())
  74. {
  75. cout<<"the IsEmpty()"<< endl;
  76. }
  77. else
  78. {
  79. //code for deleting the front data?
  80. step(front);
  81. --Index;
  82. }
  83. return true;
  84. }
  85.  
  86.  
  87. bool myQueue::IsFull()
  88. {
  89. if(Index==MAXSIZE)
  90. {
  91. return true;
  92. }
  93. return false;
  94. }
  95.  
  96.  
  97. bool myQueue::IsEmpty()
  98. {
  99. if(Index==0)
  100. {
  101. return true;
  102. }
  103. return false;
  104. }
  105.  
  106. void myQueue::step(int &num)
  107. {
  108. if(num==(size-1))
  109. {
  110. num=0;
  111. }
  112. else
  113. {
  114. num++;
  115. }
  116. }
  117. void myQueue::ShowFront()
  118. {
  119. cout<<"this is the Front Object"<<endl;
  120. cout<<ArrayQ[front]<<endl;
  121. }
  122.  
  123. void myQueue::ShowRear()
  124. {
  125. cout<<"this is the Rear Object "<<endl;
  126. cout<<ArrayQ[rear-1]<<endl;
  127. }
  128.  
  129. void myQueue::PrintQ()
  130. {
  131. for (int i=0;i<Index;++i)
  132. {
  133. cout<<ArrayQ[i];
  134. }
  135. }
Last edited by henpecked1; Aug 28th, 2008 at 1:19 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Deleting from Array Circular Queue

 
0
  #2
Aug 28th, 2008
The problem may be in my print function as well. I have it printing using the index, so it doesn't print out empty elements. Now I'm trying to delete the front element in the code above, and it appears to be working until I print the queue.

When I print the queue, the supposedly deleted element prints out, but the last element does not. I've attached all the functions if it will help.
Attached Files
File Type: cpp Contributor class.cpp (4.9 KB, 7 views)
File Type: h Contributor Class.h (2.4 KB, 5 views)
File Type: h Queue Class.h (547 Bytes, 9 views)
File Type: cpp Queue Class.cpp (2.7 KB, 8 views)
File Type: cpp Main.cpp (730 Bytes, 10 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: Deleting from Array Circular Queue

 
0
  #3
Aug 28th, 2008
I think you maybe misunderstand the meaning of index and size. There are some errors in your code, please see my comments below. I attached my code, which is base on yours. Please refer to it.

Originally Posted by henpecked1 View Post
How do you delete the front cell of an array in an array based circular queue?

Here's the Queue cpp functions

#include <iostream>
#include <string>
#include "Contributor.h"
#include "myQueue.h"
using namespace std;

myQueue::myQueue()
	{
	         front=0;
		rear =0;
		Index=0;
		size= 0;
		ArrayQ[10];  //what does this mean? I think it's not necessary
	}


myQueue::myQueue(const myQueue &CCmyQueue)
{	
	for(int i=0;i < CCmyQueue.Index;i++)
		{			
			ArrayQ[i]=CCmyQueue.ArrayQ[i]; 
// you should get the following four lines out of this loop
			front= CCmyQueue.front;
			rear =CCmyQueue.rear;
			Index=CCmyQueue.Index;
			size =CCmyQueue.size;
		}
}


myQueue &myQueue:: operator =(const myQueue & RHS) 
{
if(this!= &RHS)
	{
	  for (int i=0; i<RHS.Index;i++)
	  {
		ArrayQ[i]= RHS.ArrayQ[i];
// the same as in the copy constructor 
		Index = RHS.Index;
		rear= RHS.rear;
		front= RHS.front;
		size= RHS.size;
	  }
	}
return *this;
}


myQueue::~myQueue()
{
cout<< "default D-Tor"<<endl;
}


bool myQueue::enQueue(Contributor InData)
{
	if(IsFull())
		{
			cout<< " Queue is full"<<endl;
			return false;
	         }
           else
                 {
			ArrayQ[rear]=InData;
			step(rear);
			Index++;
	        }
	cout<<"Index ="<<Index<<endl;
	return true;
}


bool myQueue::deQueue()
{
	if(IsEmpty())
	    {
			cout<<"the IsEmpty()"<< endl;
	    }
         else
          {
			//code for deleting the front data?
			step(front);
			--Index;
	}
return true;
}


bool myQueue::IsFull()
{
	if(Index==MAXSIZE)
		{
		return true;
		}
		return false;
}


bool myQueue::IsEmpty()
{
	if(Index==0)
	{
	     return true;
	}
	     return false;
}

void myQueue::step(int &num)
{
	if(num==(size-1))
	  {
	         num=0;
	  }
           else
            {
	        num++;
            }
}
void myQueue::ShowFront()
{
	cout<<"this is the Front Object"<<endl;
	cout<<ArrayQ[front]<<endl;
}

void myQueue::ShowRear()
{
	cout<<"this is the Rear Object "<<endl;
	cout<<ArrayQ[rear-1]<<endl;
}

void myQueue::PrintQ()
{
	for (int i=0;i<Index;++i)
	{
	        cout<<ArrayQ[i];
	}
}
Attached Files
File Type: h Queue.H (689 Bytes, 10 views)
File Type: cpp Queue.cpp (2.9 KB, 12 views)
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Deleting from Array Circular Queue

 
0
  #4
Aug 28th, 2008
okay, I understand most of what you've done, what does including the windows.h file give me? I've never used it before
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,821
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Deleting from Array Circular Queue

 
0
  #5
Aug 28th, 2008
Originally Posted by henpecked1 View Post
okay, I understand most of what you've done, what does including the windows.h file give me? I've never used it before

I just glanced at littlestone's code, so I'm not sure whether you need windows.h to use it. I didn't see anywhere at first glance where you needed it, which doesn't mean that i couldn't have missed something. Try compiling with and without that #include line. Delete it if it makes no difference. windows.h is Windows-specific. Unless there's something in this assignment where you NEED to only have it work on Windows, and it doesn't appear to me that there is, don't use windows.h.

http://en.wikipedia.org/wiki/Windows.h
Last edited by VernonDozier; Aug 28th, 2008 at 2:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Deleting from Array Circular Queue

 
0
  #6
Aug 28th, 2008
Thanks for the heads up Vernon. BTW I figured out the linked list thing thanks to your tips on the node class and how data was being passed in.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

Re: Deleting from Array Circular Queue

 
0
  #7
Aug 28th, 2008
Sorry for troubling you with the windows.h. I included it just for using Sleep function to debug your code. After debugging I deleted the Sleep function but forgot to delete the windows.h.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC