circular array + queue

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 14
Reputation: gehad3003 is an unknown quantity at this point 
Solved Threads: 0
gehad3003 gehad3003 is offline Offline
Newbie Poster

circular array + queue

 
0
  #1
Apr 24th, 2008
Hi

I have a homework assignment that ask you to complete an empty project that contains a queue and stack. the data should be stored in a circular array.
Functions of the queue are:
pushBack : to insert in the back
pushFront : to insert in the front
popBack : to remove from the back
popFront : to remove from the front
rotate(n) : to rotate the array. if numbers are 3, 2, 1, 0, 7, 8 and then you rotate(2) it will be 1, 0, 7, 8, 3, 2
reverse: to reverse the array. if numbers are 8, 3, 2, 1, 0, 7 and you reverse it will be 7, 0, 1, 2, 3, 8

I just couldn't come with an appropriate method to do it.

Should the implementation be based on front and rear and just change the value of front and rear. if yes, what is the right way to code it? the problem is that the program will have an empty cell between rear and front.

any help will be appreciated
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,728
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 737
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: circular array + queue

 
1
  #2
Apr 24th, 2008
>Should the implementation be based on front and
>rear and just change the value of front and rear.
Yes. When you add an item (stack as the example), add to the rear and increment it. If incrementing it would put it beyond the last element, reset it to 0. When you remove an item, decrement the rear. If decrementing it puts you into the negatives, reset it to the last element. Then return the rear value.

>the problem is that the program will have an empty cell between rear and front.
That's not a problem if you use an item count to determine if the array is full or empty:
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. template <typename T, int N>
  5. class stack_type {
  6. T _base[N];
  7. int _front;
  8. int _back;
  9. int _size;
  10. public:
  11. stack_type()
  12. : _front ( 0 ), _back ( 0 ), _size ( 0 )
  13. {}
  14.  
  15. bool empty() const { return _size == 0; }
  16. bool full() const { return _size == N; }
  17.  
  18. void push ( T data )
  19. {
  20. if ( full() )
  21. throw std::runtime_error ( "Stack is full" );
  22.  
  23. _base[_back] = data;
  24.  
  25. if ( ++_back == N )
  26. _back = 0;
  27.  
  28. ++_size;
  29. }
  30.  
  31. T pop()
  32. {
  33. if ( empty() )
  34. throw std::runtime_error ( "Stack is empty" );
  35.  
  36. if ( --_back < 0 )
  37. _back = N - 1;
  38.  
  39. --_size;
  40.  
  41. return _base[_back];
  42. }
  43. };
  44.  
  45. int main()
  46. {
  47. stack_type<int, 3> stack;
  48.  
  49. for ( int i = 5; !stack.full(); i-- )
  50. stack.push ( i );
  51.  
  52. std::cout<< stack.pop() <<'\n';
  53. std::cout<< stack.pop() <<'\n';
  54.  
  55. stack.push ( 2 );
  56. stack.push ( 1 );
  57.  
  58. while ( !stack.empty() )
  59. std::cout<< stack.pop() <<'\n';
  60. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 14
Reputation: gehad3003 is an unknown quantity at this point 
Solved Threads: 0
gehad3003 gehad3003 is offline Offline
Newbie Poster

Re: circular array + queue

 
0
  #3
Apr 24th, 2008
Hi

thanks for this nice explanation with example.
>>That's not a problem if you use an item count to determine if the array is full or empty:
how can i display my queue if the array is like this
1 2 3 _ 8 7 0
where front is 3 and back is 8
also when i rotate front (-1) front will be in the empty area

to find more about my assignment Click here
it's the same project

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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