首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于链表的一个程序,该如何解决

2012-06-14 
关于链表的一个程序题目要求:将一个顺序输入的链表逆序输出,例如:输入1 2 3 4 5,则输出结果为 5 4 3 2 1以

关于链表的一个程序
题目要求:将一个顺序输入的链表逆序输出,例如:输入1 2 3 4 5,则输出结果为 5 4 3 2 1
以下是我的程序,已经考虑到输入长度为0,1,2,以及大于或等于3的情况,可是唯独输入长度为1的无法正确执行,各位大侠帮忙看一下。

C/C++ code
#include <iostream.h>struct node{    int data;    node *next;};void display(node *head,int n);node *creat_initial(node *head,int n);node *rev(node *head,int n);void main(){    int n;    cout<<"ÊäÈëÁ´±í³¤¶È"<<endl;    cin>>n;    node *head=NULL;    head=creat_initial(head,n);    head=rev(head,n);    display(head,n);}void display(node *head,int n){    node *curnode=head;    for(int i=0;i<n;i++)    {        cout<<curnode->data;        if(curnode->next)            curnode=curnode->next;    }}node *creat_initial(node *head,int n){    node *newnode=NULL,*tail=NULL;    //creat the node    if(n>0)    {        int num;        cout<<"ÊäÈëÊý¾Ý"<<endl;        cin>>num;        head=new node;        if(head==NULL)        {cout<<"memeory is not available"<<endl;return NULL;}        else        {            head->data=num;            tail=head;            //creat and initial the list            if(n-1>0)            {                for(int i=0;i<n-1;i++)                {                    cout<<"ÇëÊäÈëÊý¾Ý"<<endl;                    newnode=new node;                    cin>>num;                    newnode->data=num;                    newnode->next=NULL;                    tail->next=newnode;                    tail=newnode;                }                return head;            }            else                 return head;        }    }    else         return NULL;}node *rev(node *head,int n){    node *pre=NULL,*sub=NULL;    pre=head;    if(head)//head²»Îª¿Õ    {        if(head->next)//Ò»¸ö½áµã        {            head=head->next;            if(head->next)//Á½¸ö½áµã            {                sub=head->next;                pre->next=NULL;                head->next=pre;                while(sub)//¹°Öíǰ½ø                {                    pre=head;                    head=sub;                    sub=sub->next;                    head->next=pre;                }                head->next=pre;                return head;            }            else            {                head->next=pre;                pre->next=NULL;                return head;            }        }        else            return head;    }    else     {        cout<<"the list is empty"<<endl;        return NULL;    }}


[解决办法]
忘记将头节点的next指针设置为NULL了吧??
C/C++ code
node *creat_initial(node *head,int n){    node *newnode=NULL,*tail=NULL;    //creat the node    if(n>0)    {        int num;        cout<<"what???"<<endl;        cin>>num;        head=new node;        head->next = NULL;        if(head==NULL)        {cout<<"memeory is not available"<<endl;return NULL;}        else        {            head->data=num;            tail=head;            //creat and initial the list            if(n-1>0)            {                for(int i=0;i<n-1;i++)                {                    cout<<"ÇëÊäÈëÊý¾Ý"<<endl;                    newnode=new node;                    cin>>num;                    newnode->data=num;                    newnode->next=NULL;                    tail->next=newnode;                    tail=newnode;                }                return head;            }            else                 return head;        }    }    else         return NULL;} 

热点排行