m0_74087874 2022-10-05 00:20 采纳率: 100%
浏览 25
已结题

求改错:单向链表的多次插入

pta题目段错误 太搞人心态了 看不出来哪里错了

1 #include<stdio.h>
2
struct node{
3
int data;
4
struct node *next;
5
};
6
struct node *create(int n){
7
struct node *head,*tail,p;
8
head=(struct node
)malloc(sizeof(struct node));
9
head->next=NULL;
10
tail=head;
11
for(int i=0;i<n;i++){
12
p=(struct node*)malloc(sizeof(struct node));
13
scanf("%d",p->data);
14
p->next=NULL;
15
tail->next=p;
16
tail=p;
17
}
18
return head;
19
}
20
struct node *insert(struct node *L,int x){
21
struct node *head,*p,temp;
22
head=L;
23
temp=(struct node
)malloc(sizeof(struct node));
24
temp->data=x;
25
temp->next=NULL;
26
while(L->next!=NULL&&L->next->data<=x){
27
L=L->next;
28
}
29
temp->next=L->next;
30
L->next=temp;
31
return head;
32
}
33
int main(){
34
int repeat;
35
scanf("%d",&repeat);
36
for(int i=0;i<repeat;i++){
37
struct node *head1,*head2,*p;
38
int n;
39
scanf("%d",&n);
40
head1=create(n);
41
int x;
42
scanf("%d",&x);
43
head2=insert(head1,x);
44
printf("size=%d:",n+1);
45
for(p=head2->next;p!=NULL;p=p->next){
46
printf("%d ",p->data);
47
}
48
printf("\n");
49
}
50
return 0;
51

img

img

!题目在此

img

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-10-05 10:15
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    struct node {
            int data;
            struct node* next;
    };
    struct node* create(int n) {
        struct node* head, * tail, * p;
        head = (struct node*)malloc(sizeof(struct node));
        head->next = NULL;
        tail = head;
        for (int i = 0; i < n; i++) {
            p = (struct node*)malloc(sizeof(struct node));
            scanf("%d", &p->data);  //scanf("%d", p->data);
            p->next = NULL;
            tail->next = p;
            tail = p;
        }
        return head;
    }
    //struct node* insert(struct node* L, int x)
    void insert(struct node* L, int x) {
        struct node * p, * temp;   
        p = L;
        while (p->next != NULL && p->next->data < x) {
            p = p->next;
        }
        temp = (struct node*)malloc(sizeof(struct node));
        temp->data = x;
        temp->next = NULL;
        temp->next = p->next;
        p->next = temp;
    }
    void print(struct node* L)
    {
        struct node* p = L;
        while (p->next) {
            printf(p == L ? "%d" : " %d", p->next->data);
            p = p->next;
        }
        printf("\n");
    }
    void destroy(struct node* L)
    {
        struct node* p = NULL;
        while (L) {
            p = L;
            L = L->next;
            free(p);
        }
        L = NULL;
    }
    int main() {
        int repeat, n, x;
        struct node* head1;
        scanf("%d", &repeat);
        for (int i = 0; i < repeat; i++) {
            scanf("%d", &n);
            head1 = create(n);
            scanf("%d", &x);
            insert(head1, x);
            printf("size=%d:", n + 1);
            print(head1);
            destroy(head1);
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。