| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 578 人关注过本帖
标题:如何交换值?
只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
已结贴  问题点数:10 回复次数:4 
如何交换值?
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct stu)
struct stu
{
    int num;
    struct stu *next;
}*p1,*p2;
struct stu *creat()
{
    int temp;
    struct stu *head=NULL;
    printf("\ninput number:");
    scanf("%d",&temp);
    while(temp!=0)
    {
        p1=(struct stu *)malloc(LEN);
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p1->num=temp;
        p2=p1;
        printf("\ninput number:");
        scanf("%d",&temp);
 }
    p2->next=NULL;
    return(head);
}


void output(struct stu *head)
{
    for(p1=head;p1!=NULL;printf("%4d",p1->num),p1=p1->next);
    {
        
        
    }
}
struct stu *turnback(struct stu *head)
{
    struct stu *a,*newhead,*temp;
    p2=head;
    p1=head->next;
    head->next=NULL;
    while(p1!=NULL)//这段while循环里面的代码是如何交换值的?
    {
        temp=p1->next;
                  p1->next=p2;
        p2=p1;
        p1=temp;
    }
    newhead=p2;
    return(newhead);
}



int main(int argc, char* argv[])
{
    struct stu *head;
    head=creat();

    printf("the original list:\n");
    output(head);
    head=turnback(head);
    printf("\nthe new list:\n");
    output(head);
    printf("\n");

    return 0;
}
搜索更多相关主题的帖子: 交换值 
2009-07-25 11:03
rgbitxpl
Rank: 3Rank: 3
来 自:赣州
等 级:论坛游侠
帖 子:148
专家分:127
注 册:2009-5-10
收藏
得分:5 
没有交换值, 只是交换了指针的指向

只要精通C,哪里都敢闯
为了成大虾,只有多编程
2009-07-25 11:14
ziyourufeng3
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2009-7-25
收藏
得分:5 
那段代码是实现节点的逆置:先把head节点分离出来
然后把第一个节点的next指向head节点,再把第二个节点的next指向第一个节点,再是第三个......,
最后head节点就成尾节点了
2009-07-25 14:16
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
回复 3楼 ziyourufeng3
还是不太懂?
2009-07-25 15:21
ziyourufeng3
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2009-7-25
收藏
得分:0 
这个要靠自己想明白才行的,我只能提示你,给你讲个大概。
2009-07-25 15:28
快速回复:如何交换值?
数据加载中...
 
   



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

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