printf分割故障和错误的排序链接列表

发布于 2025-01-30 11:14:17 字数 2427 浏览 2 评论 0原文

这是我将节点添加到链接列表中的算法,该列表以某种方式的姓氏进行分类。

这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 

struct Node {
    char Name[1024];
    char Surname[1024];
    char Telno[1024];
    struct Node* next;
};
 



void Add(struct Node** firstnode, struct Node* NewNode)
{
    struct Node* tempo;
    //printf("%d",strcmp((*firstnode)->Surname,NewNode->Surname));
    if (*firstnode == NULL || (strcmp((*firstnode)->Surname,NewNode->Surname) > 0)) {
        
        NewNode->next = *firstnode;
        *firstnode = NewNode;
    }
    else {
        
        tempo = *firstnode;
        while (tempo->next != NULL && strcmp(tempo->Surname,NewNode->Surname) < 0) {
            tempo = tempo->next;
        }
        NewNode->next = tempo->next;
        tempo->next = NewNode;
    }
}
 

 

struct Node* CreateNode(char name[1024], char surname[1024], char telno[1024])
{
    
    struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
 
    
    strcpy(NewNode->Name,name);
    strcpy(NewNode->Surname,surname);
    strcpy(NewNode->Telno,telno);
    NewNode->next = NULL;
 
    return NewNode;
}
 

void Printlinkedlist(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%s %s %s\n", temp->Name,temp->Surname,temp->Telno);
        temp = temp->next;
    }
}
 
struct Node* head = NULL;
struct Node* temp;

int main()
{
    
    int personcount;
    char name[1024],surname[1024],telno[1024];
    printf("Please give the count of person:");
    scanf("%d", &personcount);
    
    
    for (int i = 0; i < personcount; i++) {
        
        printf("Please give the name of %d. person:", i + 1);
        scanf(" %s", &name);
        
        printf("Please give the surname of %d. person:", i + 1);
        scanf(" %s", &surname);
        
        printf("Please give the phone number of %d. person:", i + 1);
        scanf(" %s", &telno);
        
        temp = CreateNode(name,surname,telno);
        
        Add(&head, temp);
    }
    
    
    printf("\n -------------- Linkedlist --------------\n");
    Printlinkedlist(head);


    return 0;
    
}

第一个问题是:例如,如果我以G,A,L,E,K的名字输入人的姓氏(因此,第一人称姓氏将是“ G”,那么第二人称的姓氏将为“ A”等等),它给出了错误的订购输出。

第二个是:如果我在添加功能中删除printf后面的评论行字符,我会得到一个分段故障,我不明白为什么要

感谢您的答案。

This is my algorithm for adding nodes to a linked list which is in a sorted way for surnames of persons.

Here is the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

 

struct Node {
    char Name[1024];
    char Surname[1024];
    char Telno[1024];
    struct Node* next;
};
 



void Add(struct Node** firstnode, struct Node* NewNode)
{
    struct Node* tempo;
    //printf("%d",strcmp((*firstnode)->Surname,NewNode->Surname));
    if (*firstnode == NULL || (strcmp((*firstnode)->Surname,NewNode->Surname) > 0)) {
        
        NewNode->next = *firstnode;
        *firstnode = NewNode;
    }
    else {
        
        tempo = *firstnode;
        while (tempo->next != NULL && strcmp(tempo->Surname,NewNode->Surname) < 0) {
            tempo = tempo->next;
        }
        NewNode->next = tempo->next;
        tempo->next = NewNode;
    }
}
 

 

struct Node* CreateNode(char name[1024], char surname[1024], char telno[1024])
{
    
    struct Node* NewNode = (struct Node*)malloc(sizeof(struct Node));
 
    
    strcpy(NewNode->Name,name);
    strcpy(NewNode->Surname,surname);
    strcpy(NewNode->Telno,telno);
    NewNode->next = NULL;
 
    return NewNode;
}
 

void Printlinkedlist(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%s %s %s\n", temp->Name,temp->Surname,temp->Telno);
        temp = temp->next;
    }
}
 
struct Node* head = NULL;
struct Node* temp;

int main()
{
    
    int personcount;
    char name[1024],surname[1024],telno[1024];
    printf("Please give the count of person:");
    scanf("%d", &personcount);
    
    
    for (int i = 0; i < personcount; i++) {
        
        printf("Please give the name of %d. person:", i + 1);
        scanf(" %s", &name);
        
        printf("Please give the surname of %d. person:", i + 1);
        scanf(" %s", &surname);
        
        printf("Please give the phone number of %d. person:", i + 1);
        scanf(" %s", &telno);
        
        temp = CreateNode(name,surname,telno);
        
        Add(&head, temp);
    }
    
    
    printf("\n -------------- Linkedlist --------------\n");
    Printlinkedlist(head);


    return 0;
    
}

The first problem is this: For example, if I enter people's surnames as G, A, L, E, K (So first person's last name will be "G", second person's last name will be "A" etc..), it gives an incorrectly ordered output.

And the second one is: If I delete the comment line characters behind the printf inside the add function, I get a segmentation fault that I don't understand why

Thanks for the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛泛之交 2025-02-06 11:14:17

首先应该说,您可以并且应该通过任何一个来弄清楚它:


但是,更重要的是,让我们看一下您的代码:

if (*firstnode == NULL || /* another condition */) {
    // do stuff
}
else {
    // so *firstnode may be NULL here 
    tempo = *firstnode;
    while (tempo->next != /* some value */ && /* another condition*/ ) {
        // do stuff
    }
    // do stuff
}

查看问题吗? tempo可以分配一个零点,然后删除转到下一个字段。那可能会导致分割故障。

It should first be said that you could, and should, have figured it out yourself by either:


But, more to the point, let's have a look at (some of) your code:

if (*firstnode == NULL || /* another condition */) {
    // do stuff
}
else {
    // so *firstnode may be NULL here 
    tempo = *firstnode;
    while (tempo->next != /* some value */ && /* another condition*/ ) {
        // do stuff
    }
    // do stuff
}

See the problem? tempo could get assigned a NULL point, and then de-referenced to get to the next field. That would likely cause a segmentation fault.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文