| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 395 人关注过本帖
标题:关于链表逆置问题
只看楼主 加入收藏
其实、不想说
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:156
注 册:2011-3-3
结帖率:92.31%
收藏
已结贴  问题点数:20 回复次数:4 
关于链表逆置问题
#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
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:5 
如果是逆序建立的  把链表的结点再插入一遍就行了

                                         
===========深入<----------------->浅出============
2011-06-16 22:20
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:5 
程序代码:
    
// 经修改,测试通过

#include <stdio.h>
#include <stdlib.h>  
#include "malloc.h" // 如果你的编译器需要这个,那就加上吧

struct number {
    int num;
    struct number *next;
};

int main(void)
{
    int n = 0;
    
    struct number *head, *p, *r, *newnode;
    
    p = r = head = NULL;

    /* 建立链表 */
    head = NULL;
    printf("Enter 10 number: ");
    for (n = 0; n < 10; ++n) {
        
        newnode = (struct number *)malloc(sizeof(struct number));
        if (newnode == NULL) {
            printf("Error: Memory Full!\n");
            system("pause");
            exit(1);
        }
        newnode->next = NULL;
        
        scanf("%d", &newnode->num);
        if (head == NULL)
            head = r = newnode;
        else
            r->next = newnode;
        
        r = newnode;
    }
    /* newnode->next = NULL; */
    
    /* 输出链表 */
    for (p = head; p != NULL; p = p->next)
        printf("%4d", p->num);
    printf("\n\n");
    
    /* 逆置链表 */
    p = head;
    head = r = NULL;
    while (p != NULL) {
        if (head == NULL) {
            head = p;
            p = p->next;
            head->next = NULL;
        }
        else {
            r = p->next;
            p->next = head;
            head = p;
            p = r;
        }
    }
    
    /* 输出链表 */
    for (p = head; p != NULL; p = p->next)
        printf("%4d", p->num);
    printf("\n\n");
    
    system("pause");
    return 0;
}


[ 本帖最后由 cosdos 于 2011-6-16 23:03 编辑 ]

—>〉Sun〈<—
2011-06-16 22:58
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:5 
程序代码:
#include <stdio.h>
#include <malloc.h>

typedef struct number {
    int num;
    struct number * next;
} node;

typedef struct {
    node * head;
    node * first;
    node * last;
} list;

int init_list(list * l) {
    if (!l) {
        return 1;
    }
    l->head = (node *) malloc(sizeof(node));
    if (!l->head) {
        return 1;
    }
    l->head->next = l->first = l->last = NULL;
    return 0;
}

void insert_first(list * l, node * n) {
    n->next = l->first;
    l->first = l->head->next = n;
    if (!l->last) {
        l->last = l->first;
    }
}

void append(list * l, node * n) {
    n->next = NULL;
    if (l->last) {
        l->last->next = n;
        l->last = n;
    } else {
        l->head->next = l->first = l->last = n;
    }
}

int main() {
    int n;
    node * p = NULL, * q = NULL;
    list l, l_reverse;
    
    printf("How many numbers do you want to put into the list? ");
    scanf("%d", &n);
    
    if (init_list(&l)) {
        printf("Failed to allocate memory.");
        return 1;
    }
    while (n--) {
        p = (node *) malloc(sizeof(node));
        if (!p) {
            printf("Failed to allocate memory.");
            return 1;
        }
        scanf("%d", &p->num);
        append(&l, p);
    }
    
    p = l.first;
    while (p) {
        printf("%d ", p->num);
        fflush(stdout);
        p = p->next;
    }
    printf("\n");
    
    if (init_list(&l_reverse)) {
        printf("Failed to allocate memory.");
        return 1;
    }
    p = l.first;
    while (p) {
        q = p->next;
        insert_first(&l_reverse, p);
        p = q;
    }
    
    p = l_reverse.first;
    while (p) {
        printf("%d ", p->num);
        p = p->next;
    }
    
    return 0;
}


ps:这样进行逆序后,链表 l 的结构就被破坏掉了

[ 本帖最后由 voidx 于 2011-6-16 23:12 编辑 ]
2011-06-16 23:04
我不过去
Rank: 2
等 级:论坛游民
帖 子:18
专家分:42
注 册:2011-6-11
收藏
得分:5 
exit(1)是神马意思?
2011-06-17 10:54
快速回复:关于链表逆置问题
数据加载中...
 
   



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

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