Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem 8.10 resize problem #11

Closed
GoogleCodeExporter opened this issue Jul 5, 2015 · 1 comment
Closed

Problem 8.10 resize problem #11

GoogleCodeExporter opened this issue Jul 5, 2015 · 1 comment

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
Try following code with the Queue class:

int main(int argc, char *argv[]) {
    Queue<int> q;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.enqueue(4);
    q.enqueue(5);
    q.enqueue(6);
    q.enqueue(7);
    q.enqueue(8);
    // Now head = 0 and tail = 0

    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;
    // Now head = 3 and tail = 0

    q.enqueue(11);
    q.enqueue(12);
    q.enqueue(13);
    // Ok till here. Now head = 3 and tail = 3

    q.enqueue(14); // now the vector (data) is resized; but the head and tail (or elements) does not change accordingly. data[tail]=data[3]=14!
    q.enqueue(15);
    q.enqueue(16);
    q.enqueue(17);
    q.enqueue(18);    
    // The elements starting from head=3 are overwriten! 

    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;
    cout<<q.dequeue()<<endl;

    return 0;
}

What is the expected output? What do you see instead?

Expected output:
1
2
3
4
5
6
7
8
11
12

The actual output:
1
2
3
14
15
16
17
18
0
0


What version of the product are you using? On what operating system?
Book version 1.2.0
Test on Ubuntu 12.04 with gcc version 4.6.3

Please provide any additional information below.
The resize operation is not correct. The operation appends zeros to the data 
but the existing elements are not rearranged. Also see my comments in the 
testing code.

Original issue reported on code.google.com by xuxie...@gmail.com on 10 May 2013 at 7:00

@GoogleCodeExporter
Copy link
Author

Hey Xie Xu,

Thanks for your report! I have attached the updated version solution in here. 
Besides, I also add your test case in the file.

Original comment by TsungHsi...@gmail.com on 10 May 2013 at 8:09

  • Changed state: Fixed

Attachments:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant