标题:这个逆序链表怎么只输出第一个结点呢??费解,求助
只看楼主
林浩
Rank: 2
等 级:论坛游民
帖 子:47
专家分:23
注 册:2011-1-12
结帖率:90.91%
已结贴  问题点数:10 回复次数:2 
这个逆序链表怎么只输出第一个结点呢??费解,求助
感觉是不是nixu()函数出嘞错哦,但看了很久都没看出来。看你们了哦。
#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, j;
    struct student *p1, *p2, *new_head, *p3, *p4;    //p3,p4为指向新链表的结构体指针;
    p1=p2=head;  
    new_head=p3=p4=(struct student *)malloc(sizeof(struct student));     //为新链表申请表头;

    for(i=0;i<5;i++)    //是将原链表分别指向第5,4...1结点,分别接到新链表new_head;
    {
        p1=p2=head;
        for(j=1;j<5-i;j++)
        {
            p2=p1;
            p1=p1->next;
        }
        p2->next=NULL;         //删除p1所指结点;
        if(i==0)
        {
            new_head=p1;    //建立新链表表头,即原链表第5个结点;
        }
        p3=p1;      
        p4=p3;        //是p3,p4指向一个新的结点p1;
        if(i==4)    //当原链表已被删到只剩一个结点了;
        {
            p4->next=NULL;
            break;
        }
        p3=(struct student *)malloc(sizeof(struct student));    //为新链表申请一个新结点内存
        p4->next=p3;      //连接新链表结点;
    }
    return(new_head);
}
void main()
{
    struct student *head, *p=NULL;

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

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

    while(1)   //输出经逆序的新链表;
    {
        printf("%d, %s\n", p->num, p->name);
        if(p->next=NULL)
        {
            break;
        }
        p=p->next;
    }
}
搜索更多相关主题的帖子: input 
2011-03-21 22:35
njucs2008
Rank: 2
来 自:安徽省
等 级:论坛游民
帖 子:11
专家分:26
注 册:2011-3-21
得分:10 
程序代码:
#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, j;
    struct student *p1, *p2, *new_head, *p3, *p4;    //p3,p4为指向新链表的结构体指针;
    p1=p2=head;  
    new_head=p3=p4=(struct student *)malloc(sizeof(struct student));     //为新链表申请表头;

    for(i=0;i<5;i++)    //是将原链表分别指向第5,4...1结点,分别接到新链表new_head;
    {
        p1=p2=head;
        for(j=1;j<5-i;j++)
        {
            p2=p1;
            p1=p1->next;
        }
        p2->next=NULL;         //删除p1所指结点;
        if(i==0)
        {
            new_head=p1;    //建立新链表表头,即原链表第5个结点;
        }
        p3=p1;       
        p4=p3;        //是p3,p4指向一个新的结点p1;
        if(i==4)    //当原链表已被删到只剩一个结点了;
        {
            p4->next=NULL;
            break;
        }
        p3=(struct student *)malloc(sizeof(struct student));    //为新链表申请一个新结点内存
        p4->next=p3;      //连接新链表结点;
    }
    return(new_head);
}
void main()
{
    struct student *head, *p=NULL;

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

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

    while(1)   //输出经逆序的新链表;
    {
        printf("%d, %s\n", p->num, p->name);
        if(p->next==NULL)
        {
            break;
        }
        p=p->next;
    }
}

判等是==,不是=
粗心可不好哦
2011-03-21 23:35
njkido
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:224
专家分:1184
注 册:2011-3-8
得分:0 
F9断点 F10/F11单步 最好自己调试
2011-03-22 01:40



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




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

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