| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 512 人关注过本帖
标题:两个链表连接的问题 有几句不太明白 来个大哥说说
只看楼主 加入收藏
lukunstriker
Rank: 2
等 级:论坛游民
帖 子:22
专家分:20
注 册:2010-3-29
结帖率:85.71%
收藏
已结贴  问题点数:20 回复次数:1 
两个链表连接的问题 有几句不太明白 来个大哥说说
标红线的部分没有用循环 为什么就能把所有的链表二赋给新链表呢?


#include <stdio.h>
#include <stdlib.h>
#define NULL 0
typedef struct link
    {int data;
     struct link *next;
    }node;

typedef node *style;/*style定义node指针类型*/

style create()
{style p, s, head;
int x,circle=1;
head=(style)malloc(sizeof(node));/*分配空间*/
p=head;
while(circle)
{
scanf("%d",&x);
    if(x!=0)
        {s=(style)malloc(sizeof(node));
         s->data=x;
         p->next=s;
         p=s;
   
        }
    else
        circle=0;
}
p->next=NULL;/*尾节点*/
head=head->next;/*删除空的头节点*/
return head;        /*返回指针链表头表头的指针*/

}


style connect(style chain1,style chain2)/*连接函数*/
{style new_link,head;
head=chain1;
while(chain1)
    {new_link=chain1;
    chain1=chain1->next;
    }



new_link->next=chain2;
new_link=chain2;

return head;
}


void print(style p)/*输出函数*/
{while(p!=NULL)
    {printf("%d ",p->data);
    p=p->next;

    }
return;
}

void main()
{style chain1,chain2,new_link;
 printf("请输入第一个链表:\n");
 chain1=create();
 printf("请输入第二个链表:\n");
 chain2=create();
 printf("将两个链表连接后为:\n");
 print(connect(chain1,chain2));
 printf("\n");


}
搜索更多相关主题的帖子: 链表 
2010-04-30 11:02
NOMIPS
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:86
专家分:537
注 册:2010-3-31
收藏
得分:20 
所谓连接就是将一个链表的尾节点与另一个链表的头节点连接起来。
理解这句话就知道你的连接函数为什么不用对第二个链表循环。
2010-04-30 11:16
快速回复:两个链表连接的问题 有几句不太明白 来个大哥说说
数据加载中...
 
   



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

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