How to move items off a list and onto a queue or stack

I have written a code to push things off a list and onto a queue or stack til the list is empty. The programs compiles and runs but it is not executing as it is suppose to. Can anyone please tell me what is happening and suggestions on what needs to be done to fix it, that is to move items successfully off the list and onto either a queue or a stack. Thank you

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
#include <iostream>

using namespace std;

#include <list>
#include <queue>
#include <stack>
template <typename T>                       // prototype for client function
void print_list(list<T> &);

int main()
{
    list< string > lst;
    queue< string > package;
    stack< string > box;
    string choice, str;
    string::size_type pos,  start_position=0;
    bool choice_flag = true;

    do{
        cin >> choice;
        if(choice == "QUIT"){
            choice_flag = false;
            break;
        }
        else{
            lst.push_back(choice);
        }
    }while(choice_flag);
    
    print_list(lst);

    while(!lst.empty()){
        str = lst.front();
        if(str.find('P',0))
            package.push(str);
        else
            box.push(str);
    }
    //TO TEST WHAT GETS STORED INTO THE PACKAGE
    while (!package.empty()) {
        cout << package.front() << endl;
        package.pop();
                     
    }

     //TO TEST WHAT GETS STORED INTO THE BOX
    while (!box.empty()) {
        cout << box.top() << endl;
        box.pop();
                     
    }

    //print_queue(package);
    //print_stack(box);

    return 0;
}

//There are other functions:
void print_list(list<T> &lst)
.....
//void print_queue(queue<T> &package)
.....
//void print_stack(stack<T> &box) 

.....
Last edited on
1
2
3
4
5
6
7
    while(!lst.empty()){
        str = lst.front();
        if(str.find('P',0))
            package.push(str);
        else
            box.push(str);
    }


Your iteration over the list lst has some problem. You uses front() repeatedly in the while loop? In general we use iterator class to iterate over list,queue,stack etc.
1
2
3
4
5
6
7
while(!lst.empty()){
    str = lst.front();
    if(str.find('P',0))
        package.push(str);
    else
        box.push(str);
}

Here, you're never removing str from the list, you're simply accessing it. You should call lst.pop_front(); after you've taken the front element.
It is a homework assignment and we were specifically asked not to use iterators. So I attempted to take the front off first store it in a string and then examine it ... then push it onto either a stack or a queue but maybe it is that this syntax doesn't work? It compiles but there is no output after the printed list
Oh, I definitely did not catch i missed that step. Thank you for helping me debug it, that worked.
Topic archived. No new replies allowed.