标题:C链表的逆序的问题??
只看楼主
林浩
Rank: 2
等 级:论坛游民
帖 子:47
专家分:23
注 册:2011-1-12
结帖率:90.91%
已结贴  问题点数:20 回复次数:2 
C链表的逆序的问题??
#include<stdio.h>
#include<malloc.h>
#define NULL 0

struct student
{
    int num;
    char name[20];
    struct student *next;
}stu;

struct student *creat(struct student *head)
{
    struct student *p1, *p2;
    int n=0;
    p1=(struct student *)malloc(sizeof(struct student));
    printf("please input the data:\n");
    while((++n)<=5)                                            //依次输入学生数据;
    {
            scanf("%d,%s",&p1->num, p1->name);
            if(n==1)
            {
                head=p1;
            }
            p2=p1;
            p1=(struct student *)malloc(sizeof(struct student));  //开辟新结点;
            p2->next=p1;
    }
    p2->next=NULL;  //结束链表;
    return(head);
}
 
struct student *nixu(struct student *head)    //逆序链表函数; 我的想法是将原来链表的从尾到头赋给以个新的链表,但不知哪里错了。
{
    int i;
    struct student *p1, *p2, *new_head, *p, *pnew;
    p1=p2=head;
    p=pnew=new_head=(struct student *)malloc(sizeof(struct student));

    for(i=0;i<5;i++)
    {
        p1=head;
        while(1)
        {
            p2=p1;
            p1=p1->next;
            if(p1->next==NULL)
            {
                break;
            }
        }
        p2->next=NULL;
        if(i==0)
        {
            new_head=p1;   
        }
        pnew=p=p1;
        p=(struct student *)malloc(sizeof(struct student));
        pnew->next=p;   
    }
    pnew->next=NULL;

    return(new_head);
}
void main()
{
    struct student *head, *p=NULL;

    head=&stu;
    head=creat(head);
    p=nixu(head);

    printf("please output the lianbiao:\n");

    while(1)
    {
        printf("%d, %s\n", p->num, p->name);
        if(p->next=NULL)
        {
            break;
        }
        p=p->next;
    }
}
搜索更多相关主题的帖子: input 
2011-03-21 13:45
常建华
Rank: 2
等 级:论坛游民
帖 子:16
专家分:28
注 册:2011-3-20
得分:10 
最后的那个if语句有问题
2011-03-21 17:10
变幻小子
Rank: 6Rank: 6
来 自:广东陆丰
等 级:侠之大者
帖 子:188
专家分:473
注 册:2011-3-4
得分:10 
我看不出来

明天的梦
2011-03-21 22:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-334303-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.173119 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved