reverse linked list using a stack

Hello!

I have to reverse a linked list using a stack and then print it, but having problems with it, it does not print in reserve order.

It would be great if I can get any hint from anybody.

Thank you for you time.


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
void reverseUsingStack (nodeType *first)

{
 
    int n=0;
    nodeType *temp;
    temp = first;
    StackADT <int> stack;
    while (temp !=NULL)
    {   
          stack.Push (temp->info);
          temp= temp ->link;
   
    }
 }

void printList (nodeType *first)
{

    if (first  != NULL)
        {
            cout << first ->info << " " << endl;
            printList (first->link);
           
        }
    
you are never re-establishing the new list order...

you must set the pointers in reverse order...

while popping them re-assign their pointers to the next popped item

someptr = stack.top

stack.pop

someotherptr = stack.top

someptr->next = someotherptr
Last edited on
Not sure where do I put that... After a while loop? Thank you!
Topic archived. No new replies allowed.