标题:关于链表逆置问题
取消只看楼主
其实、不想说
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



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




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

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