943,031 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7272
  • C++ RSS
Apr 24th, 2008
0

circular array + queue

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gehad3003 is offline Offline
14 posts
since Nov 2007
Apr 24th, 2008
1

Re: circular array + queue

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1391
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 24th, 2008
0

Re: circular array + queue

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gehad3003 is offline Offline
14 posts
since Nov 2007

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: random
Next Thread in C++ Forum Timeline: Adding nodes to a tree





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


Follow us on Twitter


© 2011 DaniWeb® LLC