| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 465 人关注过本帖
标题:如何逆序链表?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
已结贴  问题点数:10 回复次数:2 
如何逆序链表?
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
    long num;
    float score;
    char name[20];
    struct student *next;
};
int n;

struct student * create(void)
{
    struct student *head;
    struct student *p1,*p2;
    n=0;
    p1=(struct student *)malloc(LEN);
    scanf("%ld,%f,%s",&p1->num,&p1->score,p1->name);
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        scanf("%ld,%f,%s",&p1->num,&p1->score,&p1->name);
    }
    p2->next=NULL;
    return (head);
}

struct print(struct student *head)
{
    struct student *p;
    printf("\nNow,These %d records are:\n",n);
    p=head;
    if(head!=NULL)
    {
        do
        {
            printf("%ld%5.1f%4s\n",p->num,p->score,p->name);
            p=p->next;
        }while(p!=NULL);
    }
}

struct nixu(struct student *head)//这段函数该如何些逆序链表的代码?
{
    
    
}
int main(int argc, char* argv[])
{
    struct student *head,stu;
    long del_num;
    printf("input records:\n");
    head=create();
    print(head);
    return 0;
}
搜索更多相关主题的帖子: 逆序 链表 
2009-08-05 15:38
CrystalFan
Rank: 8Rank: 8
来 自:江苏南京
等 级:蝙蝠侠
帖 子:187
专家分:802
注 册:2009-7-30
收藏
得分:5 
程序代码:
struct student* nixu(struct student *head)//这段函数该如何些逆序链表的代码?
{
    //每一次让在后面的q的next指向直接前驱p
    //由于上一步破坏了q->next指针,所以要用q_next提前保存q的直接后继指针
    //在开始时,让原来的head的后继指针为空,让它作为链表表尾
    struct student *p=head,*q,*q_next;    
    q=p->next;
    q_next=q->next;
    head->next=NULL;
    while(q_next)
    {
        q->next=p;          //q的next指针指向q的直接前驱p
        //使p,q,q_next各自向后移动一个位置
        p=q;                
        q=q_next;
        q_next=q->next;
    }
    q->next=p;                //最后一次,不要忘记
    return q;               //原来的尾指针成为新的头指针
}

在main()函数中:
    head=nixu(head); 
    print(head);


[[it] 本帖最后由 CrystalFan 于 2009-8-5 16:59 编辑 [/it]]
2009-08-05 16:51
birdvvv
Rank: 1
等 级:新手上路
帖 子:2
专家分:5
注 册:2008-11-26
收藏
得分:5 
楼上的不错,但对于只有一个节点的情况会出错,建议改为
程序代码:
struct student* nixu(struct student *head)//这段函数该如何些逆序链表的代码?
{
    //每一次让在后面的q的next指向直接前驱p
    //由于上一步破坏了q->next指针,所以要用q_next提前保存q的直接后继指针
    struct student *p,*q,*q_next;
    p=NULL;                 //在开始时,设原来的head的后继指针为空,让它作为链表表尾
    q=head;
    q_next=q->next;
    while(q_next)
    {
        q->next=p;          //q的next指针指向q的直接前驱p
        //使p,q,q_next各自向后移动一个位置
        p=q;           
        q=q_next;
        q_next=q->next;
    }
    q->next=p;                //最后一次,不要忘记
    return q;               //原来的尾指针成为新的头指针
}

我也来一个头插法的
程序代码:
struct student *nixu(struct student *head)       //头插法反转链表
{
    struct student *p,*pafter;
    p=head;
    head=NULL;               //重新生成链表
    while(p)
    {
        pafter=p->next;         //保存当前结点的下一结点
        p->next=head;           //将当前节点插入到新链表头部
        head=p;
        p=pafter;              //当前结点的下一结点,作为下次循环的当前结点
    }
    return head;
}

2009-08-06 01:44
快速回复:如何逆序链表?
数据加载中...
 
   



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

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