943,903 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2403
  • C++ RSS
Aug 28th, 2008
0

Deleting from Array Circular Queue

Expand Post »
How do you delete the front cell of an array in an array based circular queue?

Here's the Queue cpp functions

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 28th, 2008
0

Re: Deleting from Array Circular Queue

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, 22 views)
File Type: h Contributor Class.h (2.4 KB, 22 views)
File Type: h Queue Class.h (547 Bytes, 26 views)
File Type: cpp Queue Class.cpp (2.7 KB, 24 views)
File Type: cpp Main.cpp (730 Bytes, 29 views)
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 28th, 2008
0

Re: Deleting from Array Circular Queue

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.

Click to Expand / Collapse  Quote originally posted by henpecked1 ...
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, 37 views)
File Type: cpp Queue.cpp (2.9 KB, 36 views)
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Aug 28th, 2008
0

Re: Deleting from Array Circular Queue

okay, I understand most of what you've done, what does including the windows.h file give me? I've never used it before
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 28th, 2008
0

Re: Deleting from Array Circular Queue

Click to Expand / Collapse  Quote originally posted by henpecked1 ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 28th, 2008
0

Re: Deleting from Array Circular Queue

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.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 28th, 2008
0

Re: Deleting from Array Circular Queue

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.
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error LNK2001
Next Thread in C++ Forum Timeline: Data Structures





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC