Mar 5, 2012 at 3:01pm UTC
I'm trying to learn about the deque container. Here is the code that I have written so far. However, looks like the push_back() and the pop_front() functions are not working as expected. CAn some one take a look and comment please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include<iostream>
using namespace std;
class dequeue {
friend ostream & operator <<(ostream &os,dequeue &d);
public :
dequeue(int size); //constructor
~dequeue(){ delete [] storage; } //destructor
void push_front(int data);
void push_back(int data);
int pop_back();
int pop_front();
int & dequeue::operator [] (int index);
private :
int size;
int curr_front,curr_back;
int * storage;
};
dequeue:: dequeue(int size){
curr_back=curr_front=0;
storage = new int [size];
}
void dequeue:: push_front(int data){
storage[curr_front]=data;
curr_front++;
}
void dequeue:: push_back(int data){
storage[curr_back]=data;
curr_back--;
}
int dequeue:: pop_front(){
curr_front++;
return storage[curr_front];
}
int dequeue:: pop_back(){
return storage[curr_back];--curr_back;
}
int & dequeue::operator [] (int index)
{
if (index < 0 || index >size)
cout<<"Error \n" ;
else
return storage[index];
}
ostream & operator <<(ostream &os,dequeue &d)
{
int i;
os << "[ " ;
for (i=0; i<d.size; i++)
os << d[i] << " " ;
os << "]" << endl;
return os;
}
int main(){
cout<<"This is a Dequeue implementation \n" ;
dequeue q(10);
q.push_front(2);
q.push_front(1);
q.push_front(5);
q.push_back(7);
cout<<"Elements of the dequeue entered so far are \n" ;
for (int i=0;i<4;i++)
cout<<q[i]<<" " ;
cout<<q.pop_front()<<endl;
cout<<q.pop_back();
return 0;
}
Last edited on Mar 5, 2012 at 3:01pm UTC
Mar 5, 2012 at 3:15pm UTC
both pop_front() and pop_back() are wrong. You need to get the value first (to a temporary variable) and then increment/decrement. Plus you cannot do anything after
return
on line 45
EDIT: I suggest looking at this:
http://www.cplusplus.com/reference/stl/deque/
hm, yeah the push functions are also wrong. To accomplish it start in the middle:
1 2 3 4 5 6
dequeue:: dequeue(int size){
curr_back=curr_front=size/2;
curr_back++;
storage = new int [size];
}
push_back:
1 2
storage[curr_back]=data;
curr_back++;
push_front:
1 2
storage[curr_front]=data;
curr_front--;
Take care that the indexes don't go out ouf bounds
Last edited on Mar 5, 2012 at 3:25pm UTC
Mar 5, 2012 at 3:19pm UTC
I only glanced over your code, but I wonder about lines 29 and 35. If something is added to the front of the deque, shouldn't "front" be decremented, as the new first is one ahead of the old first? Similarly, if you add one to the end, the new last is one after the old last, thus it's location is incremented, no?
Mar 5, 2012 at 3:32pm UTC
coder 777,
Thank You.
Well, I'm pretty sure all my push and pop functions are a mess. I think I'm confused here.
Also, I understand your functions but not able to understand why to start from the middle?
Mar 5, 2012 at 3:38pm UTC
Gaminic,
Thanks.
I've corrected that now.
Mar 5, 2012 at 3:56pm UTC
Also, I understand your functions but not able to understand why to start from the middle?
Well, if you start both with 0 you cannot push anything to the front.
Another (maybe better) approach would be starting from the ends going to the middle:
1 2 3 4 5 6
dequeue:: dequeue(int size){
curr_back=size;
curr_front=0;
storage = new int [size];
}
Then you have to change just push_back:
1 2 3 4 5
void dequeue:: push_back(int data){
curr_back--;
storage[curr_back]=data;
}
Anyway you need to check in your push function if it's still possible to push
Last edited on Mar 5, 2012 at 3:57pm UTC
Mar 5, 2012 at 5:03pm UTC
Personally, I prefer having my "deque front" at the "array end", as if array[0] was the middle of the array.
If I know "front" is -10 (i.e. there are 10 items 'before the middle'), then I can find the first by doing "size+front", regardless of size. You can check for overflow if "end-front = size". Resizing requires you to copy the last "front" items of your array to the last "front" items of the new one. (Start from size-1 and go backward)
I have no idea if that's the best way to do it, but it seems a lot easier to me than the "start from the middle" trick, which still has troubles when you add more to one side than to the other, and is more difficult to resize.
Mar 6, 2012 at 1:02am UTC
Thanks guys.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include<iostream>
using namespace std;
class dequeue {
friend ostream & operator <<(ostream &os,dequeue &d);
public :
dequeue(int size); //constructor
~dequeue(){ delete [] storage; } //destructor
void push_front(int data);
void push_back(int data);
int pop_back();
int pop_front();
int & dequeue::operator [] (int index);
private :
int size;
int curr_front,curr_back;
int * storage;
};
dequeue:: dequeue(int size){
curr_back=curr_front=0;
// curr_back=curr_front=size/2; // some issue here...i don't get it!
//curr_back++;
storage = new int [size];
}
void dequeue:: push_front(int data){
storage[curr_front]=data;
curr_front++;
}
void dequeue:: push_back(int data){
storage[curr_back]=data;
curr_back--;
}
int dequeue:: pop_front(){
int x = storage[curr_front];
storage[curr_front]=x;
}
int dequeue:: pop_back(){
int y= storage[curr_back];
storage[curr_back]=y;
}
int & dequeue::operator [] (int index)
{
if (index < 0 || index >size)
cout<<"Error \n" ;
else
return storage[index];
}
ostream & operator <<(ostream &os,dequeue &d)
{
int i;
os << "[ " ;
for (i=0; i<d.size; i++)
os << d[i] << " " ;
os << "]" << endl;
return os;
}
int main(){
cout<<"This is a Dequeue implementation \n" ;
dequeue q_obj(4);
q_obj.push_front(1);
q_obj.push_front(2);
q_obj.push_front(3);
q_obj.push_back(7);
cout<<"Elements of the dequeue entered so far are \n" ;
for (int i=0;i<4;i++)
cout<<"[" <<q_obj[i]<<"] " ;
cin.get();
cout<<"\nPopping from the front of the deque.... " <<q_obj.pop_front()<<endl;
cin.get();
cout<<"\nPopping from the back of the deque.... " <<q_obj.pop_back();
cin.get();
return 0;
}
Last edited on Mar 6, 2012 at 1:37am UTC
Mar 6, 2012 at 1:38am UTC
I'm having issues with the initialization of the array for the deque.....can anyone help me please?
Mar 6, 2012 at 10:10am UTC
Can you show us the errors? There doesn't seem to be anything wrong with the line you marked.