c-这个代码有什么有趣的错误



很抱歉有这么长的代码,但我的错误对我来说很有趣。我想根据许多学生的卷数创建一个排序的链表---

#include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
typedef struct stuDetails
{
    char name[15];
    int age;
    int roll;
    struct stuDetails *next;
}det;
det *head;
void sortInsert(det *q, det *p);
void printSortedStructure(head);
void insSorted(det *p);
int j = 1, i;
int main(void)
{

    head = (det *) malloc(sizeof(det));
    head ->next = NULL;
    insSorted(head);
    puts("nnSorted Structure - n");
    printSortedStructure(head);

}

void insSorted(det *p)
{
    printf("Type the name of student . Type nil to end.n");
    gets(p ->name);
    if(j > 1)
    {
        gets(p ->name);
    }
    j++;
    if(strcmp(p ->name, "nil") != 0)
    {
    printf("Type the age of student n");
    scanf("%d", &(p ->age));
    printf("Type the roll no of studentn");
    scanf("%d", &(p ->roll));
    p ->next = NULL;
    sortInsert(head, p);
    p ->next = (det *) malloc(sizeof(det));
    insSorted(p ->next);
    }
    else{
        return;
    }
}
void sortInsert(det *q, det *p)
{
    if((p ->roll > q ->roll && p ->roll < q ->next ->roll) || q ->next == NULL || p ->roll == q ->roll || p ->roll == q ->next ->roll)
    {
        q ->next = p ->next;
        p ->next = q;
        return;
    }
    sortInsert(q ->next, p);
}
void printSortedStructure(det *head)
{
    while(head ->next != NULL)
    {
        printf("Name : ", head ->name);
        puts(head ->name);
        printf("Age :%dn", head ->age);
        printf("Roll No :%dnn", head ->roll);
        printSortedStructure(head ->next);
    }
}

问题是,当我运行这个程序时,它占用了atmost 3个节点,然后停止工作。若我只键入一个节点,然后键入"nil"作为下一个节点名称,它将打印结果中的第一个节点,程序将再次停止工作。到底是什么问题?

如果你想在排序的线性点赞列表中插入一个节点,你必须搜索前置节点,并插入新节点作为找到的prdecessor节点的后续节点

void sortInsert(det *head, det *p)
{
    // search predecessor
    det *pred = head;
    while ( pred->next != NULL && p->roll < pred->next->roll )
        pred = pred->next;
    // insert new node next to pred
    det *predNext = pred->next;
    pred->next = p; // successor of predecessor is new node
    p->next = predNext; // successor of new node ist old successor of predecessor node
}

在读取任何数据之前分配新节点:

void insSorted(det *head)
{
    det *newNode = malloc( sizeof(det) );
    newNode->next = NULL;
    printf("Type the name of student . Type nil to end.n");
    gets( newNode->name );
    if ( strcmp( newNode->name, "nil" ) != 0 )
    {
        printf( "Type the age of student n" );
        scanf( "%d", &( newNode->age ) );
        printf( "Type the roll no of studentn" );
        scanf( "%d", &( newNode->roll ) );
        sortInsert( head, newNode );
    }
    else
        free( newNode );
    return;
}

您可以在一个简单的循环中打印节点。

void printSortedStructure(det *head)
{
    det *act = head->next;
    while( act != NULL )
    {
        printf("Name : %sn", act->name );
        printf("Age : %dn", act ->age);
        printf("Roll No : %dnn", act ->roll);
        act = act->next;
    }
}

相关内容

  • 没有找到相关文章

最新更新