【问题标题】:Alphabetically sorting linked list按字母排序的链表
【发布时间】:2016-08-31 10:52:00
【问题描述】:

我正在尝试制作一个按字母顺序排序的链表。对于代码,如果任何两个名称相同,则比较它们的年龄。假设他们的年龄不会相同。

我创建了一个函数来比较两个学生的姓名和年龄,如 comeBefore 所示。这很好用。

我遇到的问题是将四个名称链接在一起时出现分段错误。如果有人能给我一些关于我哪里出错或在 c 中调试的指示,那就太好了。

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

typedef struct name_s Name;

//Struct name_s contains a character pointer to a name, an age, and a
// pointer to the next name in a linked list

struct name_s{
    char* name;
    int age;
    Name* next;
};

//comesBefore takes a pointer to student1 and student2.
//The two students are compared to see which one comes first in
//alphabetical order.
//If both names are the same then their ages are compared.
//The function returns true if student1 should come first else false

bool comesBefore(const Name* student1, const Name* student2)
{
    int i  = 0;
    if (student2 == NULL){
        return true;
    }
    while (student1->name[i]!= '\0' && student2->name[i] != '\0'){
        if (student1->name[i] < student2->name[i]){
            return true;
        }
        i++;
    }

    if (student1->name == student2->name){
        if (student1->age < student2->age){
            return true;
        }
    }

    return false;
}

//creates a linked list in alphabetical order of the names
//if any two names are the same the one with the lowest age comes first

Name* linkedList(Name names[], int n)
{
    Name* head = NULL;
    Name* previous = NULL;
    Name* last = NULL;
    Name* current = &names[0];
    int i = 0;
    while (i < n) {
        if (head == NULL){
            head = current;
        } else {
            if (comesBefore(current, head)){
                current->next = head;
                head = current;
            } else {
                previous = head;
                while(comesBefore(current, previous->next) == false 
                    && previous->next != NULL){
                    previous = previous->next;
                }

                current->next = previous->next;
                previous->next = current;
            }

        }
        last = head;
        int k = 0;
        while (k <= i){
            last = last->next;
            k++;
        }

        last->next = NULL;
        i++;
        current = &names[i];
    }


    return head;
}


int main(void)
{

    Name a;
    Name b;
    Name c;
    Name d;
    Name* s1 = &a;
    Name* s2 = &b;
    Name* s3 = &c;
    Name* s4 = &d;

    s1->name = "Zaphod Beeblebrox";
    s1->age = 250;
    s2->name = "Albert Einstein";
    s2->age = 133;
    s3->name = "Albert Einstein";
    s3->age = 7;
    s4->name = "Brook Fleming";
    s4->age = 20;

    Name names[4];
    names[0] = a;
    names[1] = b;
    names[2] = c;
    names[3] = d;

    Name* list = linkedList(names, 4);
    while (list!=NULL){
        printf("Name: %s\nAge: %d\n--------\n",
        list->name,
        list->age
        );
        list = list->next;
    }



    return 0;
}

【问题讨论】:

  • 你的比较功能有问题。假设代码将 Zaphod 与 Albert 进行比较;第一次比较发现 Z 不小于 A,因此继续比较 al — 结果错误。您需要测试if (student1-&gt;name[i] &gt; student2-&gt;name[i]) return false;,并且仅在到目前为止名称相等时才继续循环。随后的if (student1-&gt;name == student2-&gt;name) 测试也是假的;比较指针是否相等不是你想要的——你必须再次测试小于和大于,然后决定如果年龄相等则返回什么。

标签: c data-structures struct linked-list alphabetical


【解决方案1】:

问题是“下一个”指针没有初始化。您需要取消它们。

Name a = {0};
Name b = {0};
Name c = {0};
Name d = {0};

另一件事是,您的排序函数不能很好地处理字符串比较。 您应该将其更改为:

bool comesBefore(const Name* student1, const Name* student2)
{
    int i  = 0;
    if (student2 == NULL)
    {
        return true;
    }
    while (student1->name[i]!= '\0' && student2->name[i] != '\0')
    {
        if (student1->name[i] < student2->name[i]){
            return true;
        }
        else if (student1->name[i] > student2->name[i]){
            return false;
        }

    i++;
}
...

或者更好的是,使用strcmp()

另请注意,您的代码中的整个“k”和“last”部分根本不需要

【讨论】:

  • 空括号在 C 中无效;你至少需要一个0… = { 0 };
  • 感谢您的帮助!
  • @Brook Renzi:您可以使用调试器快速找到此类问题。
  • 关于调试器的任何建议以及如何访问它?
【解决方案2】:

改变

 while (list->next!=NULL){

 while ( list != NULL ){

可能还有其他问题,但这是我注意到的第一件事。

【讨论】:

  • 我最初确实有这个,实际上感谢您在这方面的帮助。
猜你喜欢
  • 2021-08-10
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
相关资源
最近更新 更多