链表问题?

xiansizhe 2008-09-27 07:55:24
/* 大家好,怎样把一个链表分割成两个链表,再把它合成一个循环链表,谢谢大家 */
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *create() /* 建立链表 */
{
int num;
struct node *head, *tail, *p;
head = tail = NULL;
puts("Please enter a num: ");
scanf("%d",&num);
while(num != 0)
{
p = (struct node *)malloc(sizeof(struct node));
p -> data = num;
p -> next = NULL;
if(head == NULL)
head = tail = p;
else
{
tail -> next = p;
tail = p;
}
scanf("%d",&num);
}
return (head);
}

int main(void)
{
struct node *point,*temp;
point = create();

puts("The list ");
while(point != NULL) /* 读取链表 */
{
temp = point;
printf("%d\t",point -> data);
point = point -> next;
free(temp);
}
return 0;
}


...全文
114 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kkndciapp 2008-09-27
  • 打赏
  • 举报
回复
星羽都已经写好了。。
  • 打赏
  • 举报
回复
up 星羽GG
luoyong276831622 2008-09-27
  • 打赏
  • 举报
回复
同意一楼
liaoweixiaoyu 2008-09-27
  • 打赏
  • 举报
回复
4楼的合并后的链表是一个循环链表吗?
liaoweixiaoyu 2008-09-27
  • 打赏
  • 举报
回复
lz ,你用的是尾插法建表,最好最后有一个判断
struct node *create() /* 建立链表 */
{
int num;
struct node *head, *tail, *p;
head = tail = NULL;
puts("Please enter a num: ");
scanf("%d",&num);
while(num != 0)
{
p = (struct node *)malloc(sizeof(struct node));
p -> data = num;
p -> next = NULL;
if(head == NULL)
head = tail = p;
else
{
tail -> next = p;
tail = p;
}
scanf("%d",&num);
}
if(tail != NULL)
tali->next=NULL;
//对于非空链表,表尾置空
return (head);
}
星羽 2008-09-27
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

struct node *create() /* 建立链表 */
{
int num;
struct node *head, *tail, *p;
head = tail = NULL;
puts("Please enter a num: ");
scanf("%d",&num);
while(num != 0)
{
p = (struct node *)malloc(sizeof(struct node));
p -> data = num;
p -> next = NULL;
if(head == NULL)
head = tail = p;
else
{
tail -> next = p;
tail = p;
}
scanf("%d",&num);
}
return (head);
}

void print_link(struct node* head)
{
struct node* point = head;
puts("The list ");
while(point != NULL)
{
printf("%d\t",point -> data);
point = point -> next;
}
printf("\n");
}

void free_link(struct node* head)
{
struct node* point = head;
while(point != NULL)
{
struct node* t = point;
point = point -> next;
free(t);
}
}

struct node* split_link(int split_pos, struct node* head)
{
struct node* point = head;
struct node* prevp = head;
struct node* newp = NULL;
int pos = 0;

if (split_pos <= 0)
return NULL;

while(point != NULL)
{
if (pos == split_pos)
break;
prevp = point;
point = point -> next;
++pos;
}

if (point != NULL)
{
newp = point;
prevp->next = NULL;
}

return newp;
}

void merge_link( struct node* p1, struct node* p2)
{
struct node* point = p1;

if (!p1 || !p2 || !point)
return;

while(point->next != NULL)
{
point = point->next;
}

point->next = p2;
}

int main(void)
{
struct node *point,*point2;
point = create();

print_link(point);

point2 = split_link(3, point);
print_link(point);
print_link(point2);

merge_link(point, point2);
print_link(point);



free_link(point);
return 0;
}


---------

Please enter a num:
1 2 3 4 5 6 7 8 9 0
The list
1 2 3 4 5 6 7 8 9
The list
1 2 3
The list
4 5 6 7 8 9
The list
1 2 3 4 5 6 7 8 9
请按任意键继续. . .






bitxinhai 2008-09-27
  • 打赏
  • 举报
回复
分割成两个链表和合并都要有一定的规则吧,
不然的话,是很难确定怎么分割的
迷夏牛 2008-09-27
  • 打赏
  • 举报
回复
同意楼上的
cattycat 2008-09-27
  • 打赏
  • 举报
回复
比如从链表的p节点分割,先遍历到p节点,那p的前面的节点的next=NULL,然后返回原来的head和p,就是分割后两个链表的头指针。
合并成循环表,遍历第一个链表,尾节点的next指向head2,然后遍历第二个链表,末尾节点的next=head1.
就行了。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧