cxk521aoligei 2021-10-03 17:45 采纳率: 90.9%
浏览 73
已结题

c语言数据结构单链表问题

数据结构与算法分析书上的例题,参考答案后,写的代码还是问题
代码如下

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Student)
struct Student
{
    int m;
    struct Student* next;
    
};
struct Student *creatlist();
struct Student *creatlist()
{
    
    int    n,a;
    a=n=0;
    printf("请输入学生人数:");
    scanf("%d",&a);
    struct Student *head,*p1,*p2;
    head=(struct Student*)malloc(LEN);
    printf("请输入数据:"); 
    while(n<a)
    {
        p1=(struct Student*)malloc(LEN);
        scanf("%d",&p1->m);
        n++;
        if(head->next==NULL)
        {
            head->next=p1;
            p2=p1;
        }
        else
        {
            p2->next=p1;
            p2=p1;
            
            
            
            
        }
        p2->next=NULL;
        
        
        
    }
    
    
    
    
    return head;
}
void print(struct Student* L,struct Student* P);
void print(struct Student* L,struct Student* P)
{
 struct Student* t1,*t2;
    t1 = L->next;
    t2 = P->next;
    int poi;
    poi = 1;
    while(t1 != NULL && t2 != NULL)
    {
        if(t2->m == poi++)
        {
            printf("%d\n",t1->m);
            t2 = t2->next;
        }
        t1 = t1->next;
    }
    
    
}

int main()
{
    struct Student *L,*P;
    L=creatlist();
    P=creatlist();
    print(L,P);
    
    
    
    
    return 0;
}


程序运行结果如图

img

希望指出错误

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-10-03 18:06
    关注

    没什么错误,增加了第18行的语句,没看明白 void print(struct Student* L,struct Student* P) 这个函数的作用是输出两个链表的公共部分?,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    #define LEN sizeof(struct Student)
    struct Student
    {
        int    m;
        struct Student* next;
    };
    struct Student *creatlist();
    struct Student *creatlist()
    {
        int    n,a;
        a=n=0;
        printf("请输入学生人数:");
        scanf("%d",&a);
        struct Student *head,*p1,*p2;
        head=(struct Student*)malloc(LEN);
        head->next = NULL;            //修改
        printf("请输入数据:");
        while(n<a)
        {
            p1=(struct Student*)malloc(LEN);
            scanf("%d",&p1->m);
            n++;
            if(head->next == NULL)
            {
                head->next=p1;
                p2=p1;
            }
            else
            {
                p2->next=p1;
                p2=p1;
            }
            p2->next=NULL;
        }
        return head;
    }
    void print(struct Student* L,struct Student* P);
    void print(struct Student* L,struct Student* P)
    {
     struct Student* t1,*t2;
        t1 = L->next;
        t2 = P->next;
        int poi;
        poi = 1;
        while(t1 != NULL && t2 != NULL)
        {
            if(t2->m == poi++)
            {
                printf("%d\n",t1->m);
                t2 = t2->next;
            }
            t1 = t1->next;
        }
        
    }
    int main()
    {
        struct Student *L,*P;
        L=creatlist();
        P=creatlist();
        print(L,P);
        
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月13日
  • 已采纳回答 10月5日
  • 创建了问题 10月3日

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题