dimanche 3 février 2019

creating new stack objects in a loop

i finished creating a stack but i cannot create the interface my professor is asking for. in the interface, which i am implementing with switch statement i need to keep asking the user to input data to be pushed back on the stack...

since the nodes are pointing to each other, when i am just creating nodes and pushing them on the stack i end up with an endless loop of a node pointing to itself...therefor my print function would get stuck.... i rummaged the internet, and found a solution with vectors, and indeed the & is diffrent. but even here: when i reach the push section of the second iteration i am hitting a wall and my console stops from printing anything...i cant figuer it out for the life of me...

LLStack S;
int nodeCount=0;
while(iod=='i'||iod=='d'){
    cout<<"which operation would you like the stack to perform?\n"
        <<"i: for insertion\n"
        <<"d: for deletion\n";
    cin>>iod;
    switch(iod) {
        case 'i':{
            cout<<"enter the data int for the node to hold\n";
            int input;
            cin>>input;
            //cout<<"one";
            ListNode ln(input);
            lnv.push_back(ln);
            //cout<<"two";
            //cout<<&(lnv[nodeCount]);
            S.push(S,lnv[nodeCount]);
            //cout<<"three";
            S.printS(S);
            //cout<<"shit";
            nodeCount++;
            break;
        }
    }
}


results:

you chose: s w

which operation would you like the stack to perform? i: for insertion d: for deletion i i enter the data int for the node to hold 10 10 0 10which operation would you like the stack to perform? i: for insertion d: for deletion i i enter the data int for the node to hold 15 15

Aucun commentaire:

Enregistrer un commentaire