| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 395 人关注过本帖
标题:关于链表逆置问题
取消只看楼主 加入收藏
其实、不想说
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:156
注 册:2011-3-3
结帖率:92.31%
收藏
已结贴  问题点数:20 回复次数:0 
关于链表逆置问题
#include "stdio.h"
#include "malloc.h"

struct number
{
    int num;
    struct number *next;
};
struct number *head, *p, *r, *newhead;

main()
{
    int n=0;

    newhead=p=r=head=(struct number *)malloc(sizeof(struct number));
    newhead=NULL;
    while(n<10)
    {
        p=(struct number *)malloc(sizeof(struct number));/*建立链表*/
        printf("Enter the number:");
        scanf("%d", &p->num);
        if (head == NULL)
            head=p;
        else
            r->next=p;
        r=p;
        n++;
    }
    r->next=NULL;
    p=head->next;
    do
    {
        printf("%4d", p->num);/*输出链表*/
        p=p->next;
    }while(p->next != NULL);
    printf("%4d", p->num);

    p=r=head;
    for(n=0; n<10; n++)/*逆置链表*/
    {
        p=p->next;
        r=p;
        if (newhead == NULL)
            {
                newhead->next=r;
                r->next=head;
            }
        else
            {
                r->next=newhead->next;
                newhead->next=r;
            }
    }
    for(p=newhead->next,n=0; n<10; p=p->next,n++)
    {
        printf("%4d", p->num);
    }
    getch();
}
收到的鲜花
  • Devil_W2011-06-17 13:11 送鲜花  -3朵   附言:最经典的,显然是用递归写。
搜索更多相关主题的帖子: include number Enter while 
2011-06-16 22:17
快速回复:关于链表逆置问题
数据加载中...
 
   



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

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