re: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
    here's my code

    Please Register or Log in to view the hidden image!



    #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;



    }
     
Thread Status:
Not open for further replies.

Share This Page