pta单链表的合并,创建时的错误

这个错误怎么办,pta报错

img

```c
#include
#include
#include
typedef struct cents{
int data;
int len ;
struct cents* next;
}cent,*CENT;
////////////////////////////
CENT creat_list();
void show(CENT p);
CENT add(CENT p,CENT p1);
CENT sort_list(CENT p );
////////////////////////////
int main(void)
{
CENT p1;
CENT p2;
CENT p3;
p1 = creat_list();
p2 = creat_list();
//show(p1);
//show(p2);
p3 = add(p1,p2);
p3 = sort_list(p3);
show(p3);

return 0;
}
///////////////////
CENT creat_list(){
CENT pHead =(CENT)malloc(sizeof(cent));
if(pHead == NULL){
printf("fasle1");
exit(-1);
}
pHead->len = 0;
CENT pTail =(CENT)malloc(sizeof(cent));
pHead = pTail;
while(pTail->data != -1){
CENT pNew = (CENT)malloc(sizeof(cent));
if(pNew==NULL){
printf("´false2");
exit(-1);
}
int s;
scanf("%d",&s);
pNew->data = s;
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
pHead->len++;
}
return pHead;
}

void show(CENT p){
CENT pArry = (CENT)malloc(sizeof(cent));
if(pArry == NULL){
printf("false3");
exit(-1);
}
pArry = p->next;
while(pArry->data!=-1){
printf("%d ",pArry->data);
pArry = pArry->next;
}
printf("\n");
}
//////////////
CENT add(CENT p,CENT p1){
CENT pHead = (CENT)malloc(sizeof(cent));
pHead = p;
while(p->next->data!=-1){
p = p->next;
}
p->next = p1->next;
return pHead;
}
CENT sort_list(CENT p ){
CENT pfirst=NULL;
CENT pend=NULL;
pfirst=p->next;
while(pfirst != pend){
while(pfirst->next != pend){
if(pfirst->data >pfirst->next->data){
int temp=pfirst->data;
pfirst->data=pfirst->next->data;
pfirst->next->data=temp;
}
pfirst=pfirst->next;
}
pend=pfirst;
pfirst=p->next;
}
return p->next;

}

```的结果

修改处见注释,供参考:

#include <stdio.h>
#include <stdlib.h>
typedef struct cents {
    int data;
    int len;
    struct cents* next;
}cent, * CENT;
////////////////////////////
CENT creat_list();
void show(CENT p);
CENT add(CENT p, CENT p1);
CENT sort_list(CENT p);
////////////////////////////
int main(void)
{
    CENT p1;
    CENT p2;
    CENT p3;
    p1 = creat_list();
    p2 = creat_list();
    //show(p1);
    //show(p2);
    p3 = add(p1, p2);
    show(p3);
    p3 = sort_list(p3);
    show(p3);

    return 0;
}
///////////////////
CENT creat_list() 
{
    CENT pHead = (CENT)malloc(sizeof(cent));
    if (pHead == NULL) {
        printf("fasle1");
        exit(-1);
    }
    pHead->next = NULL;
    pHead->len = 0;
    CENT pTail;        //= (CENT)malloc(sizeof(cent));//修改
    pTail = pHead;     //修改 pHead = pTail;
    while (1) { //while (pTail->data != -1)
        int s, ret;
        ret = scanf("%d", &s);   //修改
        if (ret != 1 || s == -1)   break;//修改
        CENT pNew = (CENT)malloc(sizeof(cent));
        if (pNew == NULL) {
            printf("´false2");
            exit(-1);
        }
        pNew->next = NULL;
        pNew->data = s;
        pTail->next = pNew;
        pTail = pNew;
        pHead->len++;
    }
    return pHead;
}

void show(CENT p) {
    CENT pArry; //= (CENT)malloc(sizeof(cent));修改
                //if (pArry == NULL) {
                //    printf("false3");
                //    exit(-1);
                //}
    pArry = p->next;
    while (pArry) { //while (pArry->data != -1) 修改
        printf("%d ", pArry->data);
        pArry = pArry->next;
    }
    printf("\n");
}
//////////////
CENT add(CENT p, CENT p1) {
    CENT pHead;  //= (CENT)malloc(sizeof(cent)); 修改
    pHead = p;
    while (p->next) {  //while(p->next->data != -1) 修改
        p = p->next;
    }
    p->next = p1->next;
    pHead->len += p1->len;  //修改
    free(p1);              //修改
    return pHead;
}
CENT sort_list(CENT p) {
    CENT pfirst = NULL;
    CENT pend = NULL;
    while (p->next != pend) {
        pfirst = p;
        while (pfirst->next->next != pend) { //修改
            if (pfirst->next->data > pfirst->next->next->data) { //修改
                CENT temp = pfirst->next; //int temp = pfirst->data; //修改
                pfirst->next = pfirst->next->next;                   //修改
                temp->next = pfirst->next->next;//pfirst->data = pfirst->next->data;//修改
                pfirst->next->next = temp;                           //修改
            }
            pfirst = pfirst->next;
        }
        pend = pfirst->next; //修改
    }
    return p;
}

scanf改成 scanf_s


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct cents{
int data;
int len ;
struct cents* next;
}cent,*CENT;
////////////////////////////
CENT creat_list();
void show(CENT p);
CENT add(CENT p,CENT p1);
CENT sort_list(CENT p );
////////////////////////////
int main(void)
{
CENT p1;
CENT p2;
CENT p3;
p1 = creat_list();
p2 = creat_list();
//show(p1);
//show(p2);
p3 = add(p1,p2);
p3 = sort_list(p3);
show(p3);

return 0;
}
///////////////////
CENT creat_list(){
CENT pHead =(CENT)malloc(sizeof(cent));
if(pHead == NULL){
printf("fasle1");
exit(-1);
}
pHead->len = 0;
CENT pTail =(CENT)malloc(sizeof(cent));
pHead = pTail;
while(pTail->data != -1){
CENT pNew = (CENT)malloc(sizeof(cent));
if(pNew==NULL){
printf("´false2");
exit(-1);
}
int s;
scanf("%d",&s);
pNew->data = s;
pTail->next = pNew;
pNew->next = NULL;
pTail = pNew;
pHead->len++;
}
return pHead;
}

void show(CENT p){
CENT pArry = (CENT)malloc(sizeof(cent));
if(pArry == NULL){
printf("false3");
exit(-1);
}
pArry = p->next;
while(pArry->data!=-1){
printf("%d ",pArry->data);
pArry = pArry->next;
}
printf("\n");
}
//////////////
CENT add(CENT p,CENT p1){
CENT pHead = (CENT)malloc(sizeof(cent));
pHead = p;
while(p->next->data!=-1){
p = p->next;
}
p->next = p1->next;
return pHead;
}
CENT sort_list(CENT p ){
CENT pfirst=NULL;
CENT pend=NULL;
pfirst=p->next;
while(pfirst != pend){
while(pfirst->next != pend){
if(pfirst->data >pfirst->next->data){
int temp=pfirst->data;
pfirst->data=pfirst->next->data;
pfirst->next->data=temp;
}
pfirst=pfirst->next;
}
pend=pfirst;
pfirst=p->next;
}
return p->next;

}