c++ reverse function in linked list

Discussion in 'Computer Science & Culture' started by gram1234, Mar 3, 2003.

Thread Status:
Not open for further replies.
  1. gram1234 Registered Member

    Messages:
    18
    I need to write a reverse function with one parameter which is a head pointer to a linked list of integers.The return type should be void.i am completely stymied.

    Please Register or Log in to view the hidden image!

    please help.thanks
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    Post Other Code...

    post the code you already have...the other various linked list functions and structure. From there we can help you better.

    -AntonK
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. gram1234 Registered Member

    Messages:
    18
    Re: Post Other Code...

    #include<stddef.h>
    #include<iostream.h>

    struct Node//file.h
    {
    int data;
    Node* link;
    };

    void PrintLinked(Node* printPtr);
    void invert(Node* cur);



    #include"link.h"


    void main()//linktest.cpp

    int num;

    Node* head;
    head=NULL;
    Node* tail;
    tail=NULL;
    Node* ptr;

    cout<<"Please input positive integers or 0 to exit :";
    cout<<endl;
    cin>>num;

    while(num!=0)
    {

    ptr=new Node;
    ptr->data=num;
    ptr->link=NULL;

    if(head==NULL)
    {

    head=ptr;
    tail=ptr;
    }

    else
    {
    tail->link=ptr;
    tail=ptr;
    }


    cin>>num;

    }

    PrintLinked(head);
    invert(head);


    PrintLinked(head);
    cout<<endl;



    }

    void PrintLinked(Node* printPtr)//file.cpp
    {
    Node* temp;
    temp=printPtr;
    cout<<"The numbers you entered into the link : ";
    cout<<endl;
    while(temp!=NULL)

    {
    cout<<temp->data<<" ";
    temp=temp->link;
    }
    cout<<endl;
    }



    void invert(Node* cur)//function to reverse
    {
    cout<<"the link is"<<cur<<endl;
    Node* temp;
    temp=cur;
    Node* head;
    head=NULL;
    Node* tail;
    tail=NULL;

    while(temp!=NULL)

    {
    cout<<temp->data<<" ";
    temp=temp->link;

    }

    PrintLinked(head);
    if(cur->link)
    invert(cur->link);
    cur->data;
    return ;
    cout<<endl;



    }


    very confused ,print function seems to work fine,help greatly appreciated
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Ender Registered Senior Member

    Messages:
    294
    I just thought this up off the top of my head, I think it might work though!

    If you don't have to read the list anymore (ie: it ca be distroyed after outputting)
    1. Make a while loop while(Ptr->next != NULL && ptr != head)
    2. Make anoher pointer that will point to the node before the current node (ie: node * prev )
    3. Make Prev = Head;
    4. Make Ptr = Head;
    5. Write an if statements that will get the end of the list (the ptr->next == NULL;
    6. Cout the Ptr->data
    7. Set the prev->next = NULL;
    8. make ptr->data = 0; delete ptr;
      [/list=1]
     
Thread Status:
Not open for further replies.

Share This Page