| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 662 人关注过本帖
标题:链表反转不能正常显示
只看楼主 加入收藏
tredy6t
Rank: 2
等 级:论坛游民
帖 子:41
专家分:59
注 册:2015-8-6
结帖率:45.45%
收藏
已结贴  问题点数:10 回复次数:1 
链表反转不能正常显示
程序代码:
#include<stdio.h>
#include<stdlib.h>
struct Node{
    int num;
    char name[10];
    char sex;
    float score;
    Node * next;
};
int n=0;
struct Node * creat(void)
{
    struct Node *p1,*p2;
    struct Node *head=NULL;
    p2=p1=(struct Node*)malloc(sizeof(struct Node));
    scanf("%d %s %c %f",&p1->num,p1->name,&p1->sex,&p1->score);
    while(p1->num!=0)
    {
        ++n;
        if(n==1)
            head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct Node*)malloc(sizeof(struct Node));
        scanf("%d %s %c %f",&p1->num,p1->name,&p1->sex,&p1->score);
    }
    p2->next=NULL;
    return head;
}
void print(struct Node*head)
{
    struct Node *p;
    p=head;
    if(head!=NULL)
    {
        do{
            printf("%d\t%s\t%c\t%f\n",p->num,p->name,p->sex,p->score);
            p=p->next;
        }while(p!=NULL);
    }
}

 struct Node reserve(struct Node * head)
{
    struct Node *p,*q;
    p=NULL;
    while(head!=NULL)
    {
        q=p;
        p=head;
        head=head->next;
        p->next=q;
    }
    return *p;
}
void Free(struct Node *head)   
{
  struct Node *ptr;
  while(head!=NULL)
  {
    ptr=head;
    head=head->next ;
    free(ptr);
  }
}
int main()
{
    struct Node *head;
    head=creat();
    printf("The init:\n");
    print(head);
    printf("反转:\n");
    reserve(head);
    print(head);
    Free(head);
    return 0;
}

图片附件: 游客没有浏览图片的权限,请 登录注册
2015-12-11 16:11
学习之旅1
Rank: 1
等 级:新手上路
帖 子:1
专家分:7
注 册:2015-12-11
收藏
得分:7 
你反转后的,返回值没有接收。而且最好不要用head=head->next,可以定义一个结构体指针指向head。我用你的试过了定义一个指针接收后,打印它,结果是正确的,你试试看
2015-12-11 20:22
快速回复:链表反转不能正常显示
数据加载中...
 
   



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

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