cxk521aoligei
2021-10-03 17:45
采纳率: 95%
浏览 55

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条回答 默认 最新

相关推荐 更多相似问题