关于链表的题

雪墓 2018-03-26 06:30:31

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int n,m,x,y,i,j;
scanf("%d %d",&m,&n);
struct node *head1,*head2,*q,*p,*t,*q1,*q2,*k;
head1=(struct node*)malloc(sizeof(struct node));
head1->next=NULL;
head2=(struct node*)malloc(sizeof(struct node));
head2->next=NULL;
q1=head1;
q2=head2;
for(i=1; i<=m; i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next=NULL;
q1->next=p;
q1=p;
}
for(i=1; i<=n; i++)
{
p=(struct node*)malloc(sizeof(struct node));;
scanf("%d",&p->data);
p->next=NULL;
q2->next=p;
q2=p;
}
p=head2->next;
while(p)
{
k=p;
t=head1->next;
q=head1;
while(t)
{
if(k->data<t->data)
{
k->next=t;
q->next=k;
break;
}
t=t->next;
q=q->next;
}
if(t==NULL)
{
k->next=NULL;
q->next=k;
}
p=p->next;
}
p=head1->next;
while(p)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
return 0;
}


刚开始学
求大佬看看
我的循环是错的 没有输出
不知道哪里错了

...全文
347 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-03-27
  • 打赏
  • 举报
回复
对比一下找不同的地方,分析一下;
赵4老师 2018-03-27
  • 打赏
  • 举报
回复
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
雪墓 2018-03-26
  • 打赏
  • 举报
回复
引用 1楼自信男孩 的回复:
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};

int main()
{
    int n,m,x,y,i,j;
    scanf("%d %d",&m,&n);
    struct node *head1,*head2,*q,*p,*t,*q1,*q2,*k;
    head1=(struct node*)malloc(sizeof(struct node));
    head1->next=NULL;
    head2=(struct node*)malloc(sizeof(struct node));
    head2->next=NULL;
    q1 = head1;
    q2 = head2;
    for(i=1; i<=m; i++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next = NULL;
        q1->next = p;
        q1 = p;
    }
    for(i = 1; i<=n; i++)
    {
        p=(struct node*)malloc(sizeof(struct node));;
        scanf("%d",&p->data);
        p->next=NULL;
        q2->next = p;
        q2 = p;
    }
    q1 = head1->next;
    q2 = head2->next;
    t = head1;

    while (q1 && q2) {
        if (q1->data < q2->data) {
            t->next = q1;
            t = q1;
            q1 = q1->next;
        } else {
            t->next = q2;
            t = q2;
            q2 = q2->next;
        }
    }
    t->next = q1 ? q1 : q2;

    p = head1->next;
    while(p)
    {
        printf("%d ",p->data);
        p = p->next;
    }
    /*
    p = head2->next;
    while(p)
    {
        k=p;
        t=head1->next;
        q=head1;
        while(t)
        {
            if(k->data<t->data)
            {
                k->next=t;
                q->next=k;
                break;
            }
            t=t->next;
            q=q->next;
        }
        if(t==NULL)
        {
            k->next=NULL;
            q->next=k;
        }
        p=p->next;
    }
    p=head1->next;
    while(p)
    {
        if(p->next==NULL)
            printf("%d\n",p->data);
        else
            printf("%d ",p->data);
        p=p->next;
    }
    */
    return 0;
}
参考一下吧 合并和输出可以对比参考一下;
我的那为啥不对
自信男孩 2018-03-26
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
};

int main()
{
    int n,m,x,y,i,j;
    scanf("%d %d",&m,&n);
    struct node *head1,*head2,*q,*p,*t,*q1,*q2,*k;
    head1=(struct node*)malloc(sizeof(struct node));
    head1->next=NULL;
    head2=(struct node*)malloc(sizeof(struct node));
    head2->next=NULL;
    q1 = head1;
    q2 = head2;
    for(i=1; i<=m; i++)
    {
        p = (struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next = NULL;
        q1->next = p;
        q1 = p;
    }
    for(i = 1; i<=n; i++)
    {
        p=(struct node*)malloc(sizeof(struct node));;
        scanf("%d",&p->data);
        p->next=NULL;
        q2->next = p;
        q2 = p;
    }
    q1 = head1->next;
    q2 = head2->next;
    t = head1;

    while (q1 && q2) {
        if (q1->data < q2->data) {
            t->next = q1;
            t = q1;
            q1 = q1->next;
        } else {
            t->next = q2;
            t = q2;
            q2 = q2->next;
        }
    }
    t->next = q1 ? q1 : q2;

    p = head1->next;
    while(p)
    {
        printf("%d ",p->data);
        p = p->next;
    }
    /*
    p = head2->next;
    while(p)
    {
        k=p;
        t=head1->next;
        q=head1;
        while(t)
        {
            if(k->data<t->data)
            {
                k->next=t;
                q->next=k;
                break;
            }
            t=t->next;
            q=q->next;
        }
        if(t==NULL)
        {
            k->next=NULL;
            q->next=k;
        }
        p=p->next;
    }
    p=head1->next;
    while(p)
    {
        if(p->next==NULL)
            printf("%d\n",p->data);
        else
            printf("%d ",p->data);
        p=p->next;
    }
    */
    return 0;
}
参考一下吧 合并和输出可以对比参考一下;

70,037

社区成员

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

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