| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 505 人关注过本帖
标题:关于单向循环链表
只看楼主 加入收藏
gold615
Rank: 2
等 级:论坛游民
帖 子:54
专家分:75
注 册:2014-6-7
结帖率:55.56%
收藏
已结贴  问题点数:20 回复次数:3 
关于单向循环链表
刚开始看数据结构,自己写了几行,目的是输入两个未知长度大小的循环链表,然后把这两个 链表合并成一个新的循环链表。
但是执行的时候发现,第一个链表 可以完整的输入进去,每次一开始输入第二个链表就死机,找了半天也没有发现问题在哪里。求各位大神帮忙看看那个输入链表的子函数是不是有问题。
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
    int a;
    node * next;
};
node *input_linked_list(node *head,int *count)//输入链表
{
    node *p,*last=NULL;
    char c='y';int i=1;
    while(1)
    {
        printf("please input the %d number:\t",i);
        p=(node *)malloc(sizeof(node));
        scanf("%d",&(p->a));getchar();
        if(head==NULL)
        {
            head=p;
        }
        else if(i==1)
        {
            head->next=p;
        }
        if(last==NULL)last=p;
        else
        {
            last->next=p;
            last=p;
        }
        (*count)++;i++;
        printf("continue? y&n\t");
        scanf("%c",&c);
        if(c=='n')
        {
            break;
        }
    }
    last->next=head;
    return last;
}
void print(node * link_head,node *link_last)
{
    node *p=link_head;
    while(p->next!=link_head)
    {
        printf("%d\t",p->next->a);
        p=p->next;
    }
    printf("\n");
}
void combine(node *last1,node * last2)
{
    node *temp;
    temp=last1->next;
    last1->next=last2->next;
    last2->next=temp;
}
main()
{
    node *head1,*head2,*last1,*last2;
    int count1=0,count2=0;
    printf("please input the first link.\n");
    last1=input_linked_list(head1,&count1);
    printf("the length of the first link is %d\n",count1);
    printf("the element is :\n");
    print(head1,last1);
    printf("please input the second link.\n");
    last2=input_linked_list(head2,&count2);
    printf("the length of the second link is %d\n",count2);
    combine(last1,last2);
    print(last1->next,last2->next);
    printf("\n");
}
搜索更多相关主题的帖子: include count 
2015-01-07 23:37
gold615
Rank: 2
等 级:论坛游民
帖 子:54
专家分:75
注 册:2014-6-7
收藏
得分:0 
没人回么?
2015-01-08 11:02
bin17
Rank: 2
等 级:论坛游民
帖 子:1
专家分:20
注 册:2015-1-8
收藏
得分:20 
#include<stdio.h>
#include<malloc.h>

typedef struct node
{
    int a;
    struct node* next;
}node;
node *input_linked_list(int *count)//输入链表
{
    node *head,*p,*q;
    head = (node *)malloc(sizeof(node));
    head->next = NULL;
    p = head;
    char c='y';int i = 1;
    while(1)
    {
        printf("please input the %d number:\t",i++);
        q=(node *)malloc(sizeof(node));
        scanf("%d",&(q->a));getchar();
        q->next = NULL;
        p->next = q;
        p = p->next;
        (*count)++;
        printf("continue? y&n\t");
        scanf("%c",&c);
        if(c=='n')
        {
            break;
        }
    }
    q->next=head;
    return head;
}
void print(node * link_head)
{
    node *p=link_head;
    while(p->next!=link_head)
    {
        printf("%d\t",p->next->a);
        p=p->next;
    }
    printf("\n");
}
node* combine(node *head1,node * head2)
{
    node *p,*q;
    p =head1;
    q = head2;

    while(p->next!=head1)
    {
        p=p->next;
    }

    p->next = head2->next;

    while(p->next!=head2)
    {
        p=p->next;
    }
    p->next = head1;

    return head1;
}
main()
{
    node *head1,*head2,*last1,*last2;
    int count1=0,count2=0;
    printf("please input the first link.\n");
    head1 = input_linked_list(&count1);
    printf("the length of the first link is %d\n",count1);
    printf("the element is :\n");
    print(head1);
    printf("please input the second link.\n");
    head2=input_linked_list(&count2);
    printf("the length of the second link is %d\n",count2);
    print(head2);

    head1 = combine(head1,head2);
    print(head1);
 //   combine(last1,last2);
    printf("\n");
}
2015-01-08 21:30
ffbh
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:10
注 册:2015-1-15
收藏
得分:0 
第三行的typedef后面没写新的定义名
2015-01-15 21:21
快速回复:关于单向循环链表
数据加载中...
 
   



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

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