问一个数据结构题五个空

#include<stdlib.h>

#include<stdio.h>

#define N 8

typedef struct list

{ int data;

struct list *next;

} SLIST;

void fun(SLIST *h, int x)

{SLIST *p, *q, *s;

s=(SLIST *)malloc(sizeof(SLIST));

s->data=填空1;

q=h;

p=h->next;

while (p!=NULL && x>p->data)

{

q=填空2;

p=p->next;

}

s->next=p;q->next=填空3;

}

SLIST *creatlist(int *a)

{ SLIST *h, *p, *q;int i;

h=p=(SLIST ) malloc (填空4));

for(i=0; i<N; i++)

{q=(SLIST *)malloc(sizeof(SLIST));

q->data=a[i]; p->next=q; p=q;

}

p->next=0;return h;

}

void outlist(SLIST *h)

{SLIST *p;

p=h->next;

if(p==NULL)

printf("\nThe list is NULL!\n");

else

{printf("nHead");

do { printf("->%d",p->data); p=填空5; }

while (p!=NULL);

printf("->End\n");

}

}

void main ()

{

SLIST *head; int x;

int a[N]={11, 12, 15, 18, 19,22, 25, 29};

head=creatlist(a);

printf("\nThe list before inserting:\n");

outlist (head);printf("\nEnter a number: ");

scanf("%d", &x);

fun(head, x);

printf("\nThe list after inserting:\n");

outlist (head);

}

s->data = x; //填空1;
q = p; //填空2;
q->next = s; //填空3;
h = p = (SLIST*)malloc(sizeof(SLIST)); //填空4
do { printf("->%d", p->data); p = p->next; }//填空5
供参考:

#include<stdlib.h>
#include<stdio.h>
#define N 8
typedef struct list
{
    int data;
    struct list* next;
} SLIST;
void fun(SLIST* h, int x)

{
    SLIST* p, * q, * s;
    s = (SLIST*)malloc(sizeof(SLIST));
    s->data = x; //填空1;
    q = h;
    p = h->next;
    while (p != NULL && x > p->data)
    {
        q = p;  //填空2;
        p = p->next;
    }
    s->next = p; q->next = s; //填空3;
}

SLIST* creatlist(int* a)
{
    SLIST* h, * p, * q; int i;
    h = p = (SLIST*)malloc(sizeof(SLIST)); //填空4
    for (i = 0; i < N; i++)
    {
        q = (SLIST*)malloc(sizeof(SLIST));
        q->data = a[i]; p->next = q; p = q;
    }
    p->next = 0; 
    return h;
}

void outlist(SLIST* h)

{
    SLIST* p;
    p = h->next;
    if (p == NULL)
        printf("\nThe list is NULL!\n");
    else
    {
        printf("nHead");
        do { printf("->%d", p->data); p = p->next; }//填空5
        while (p != NULL);
        printf("->End\n");
    }
}

void main()

{
    SLIST* head; int x;
    int a[N] = { 11, 12, 15, 18, 19,22, 25, 29 };
    head = creatlist(a);
    printf("\nThe list before inserting:\n");
    outlist(head); printf("\nEnter a number: ");
    scanf("%d", &x);
    fun(head, x);
    printf("\nThe list after inserting:\n");
    outlist(head);
}

修改如下:


#include<stdlib.h>

#include<stdio.h>

#define N 8

typedef struct list

{ 
    int data;
    
    struct list *next;

} SLIST;

void fun(SLIST *h, int x)
{
    SLIST *p, *q, *s;
    
    s=(SLIST *)malloc(sizeof(SLIST));
    
    s->data=x;  //创建新链表存储传入的要插入的值 x
    
    q=h;
    
    p=h->next;
    
    while (p!=NULL && x>p->data)
    
    {
        q=p;  //保留p的值,以便在遍历到x合适的位置时在此位置插入s节点 
        
        p=p->next;  //指向下一个链表下一个节点,用于遍历到x合适的位置后连接s节点和s节点下一个节点 
    
    }
    
    s->next=p;  //s节点连接下一个节点 
    
    q->next=s;  //在合适的位置插入s节点 

}

SLIST *creatlist(int *a)    
{ 
    SLIST *h, *p, *q;int i;
    
    h=p=(SLIST *) malloc (sizeof(SLIST));  //创建节点 
    
    for(i=0; i<N; i++)
    
    {q=(SLIST *)malloc(sizeof(SLIST));
    
    q->data=a[i]; p->next=q; p=q;
    
    }
    
    p->next=0;return h;

}

void outlist(SLIST *h)

    {SLIST *p;
    
    p=h->next;
    
    if(p==NULL)
    
        printf("\nThe list is NULL!\n");
    
    else
    
    {
        printf("nHead");
        
        do { 
            printf("->%d",p->data);
            p=p->next; 
        }while (p!=NULL);
        
        printf("->End\n");
    
    }

}

int  main ()

{
    SLIST *head; int x;
    
    int a[N]={11, 12, 15, 18, 19,22, 25, 29};
    
    head=creatlist(a);
    
    printf("\nThe list before inserting:\n");
    
    outlist (head);printf("\nEnter a number: ");
    
    scanf("%d", &x);
    
    fun(head, x);
    
    printf("\nThe list after inserting:\n");
    
    outlist (head);

}