【问题标题】:Save distinct words into linked list将不同的单词保存到链表中
【发布时间】:2012-06-26 12:18:10
【问题描述】:

基本上我在这里有 2 个链表:list 和 distinct。有几组词早先已保存到“列表”结构中。打算编写一个程序,该程序将找到不同/唯一的单词并将其保存到“不同”结构中。这是我到目前为止基于我的指针概念得到的。但是,当我尝试打印“distinct”时,程序崩溃:(如果我错了,请纠正我。

struct list {
char string[50];
struct list *next;
};

struct distinct {
char string[50];
struct distinct *next; 
};

void checkdistinct() { 

 list *ori = NULL;
 distinct *copy = NULL;
 distinct *check = NULL;

if(ori == NULL && copy == NULL) { //first time.
    ori = ori->next;
    copy = copy->next;
    copy = (distinct*)malloc(sizeof(distinct));
    strcpy(copy->string, ori->string);
    ori = ori->next;
    copy = copy->next;
}
else {}

while(ori!=NULL) {
    check = check->next;

   while(check != NULL) {
    if(strcmp(ori->string, check->string)!=0) {
        check = check->next;
    }
    else {
        ori = ori->next;
        check = NULL;
    }

 }

    //only compare same casing words, for now.
    copy = (distinct*)malloc(sizeof(distinct));
    strcpy(copy->string, ori->string);
    ori = ori->next;
    copy = copy->next;      
 }
}

当我尝试在 main 中打印时,它会崩溃:(如果您需要额外的 cmets 代码,请回复。谢谢!

【问题讨论】:

  • 当您遇到崩溃时,您应该始终做的第一件事就是在调试器中运行您的程序。它不仅可以帮助您确定崩溃的位置,还可以让您检查变量以帮助您找出崩溃的原因。
  • 我想我做到了。不久前刚开始使用 c++ Visual Studio 还在尝试熟悉它。我尝试在整个代码中放置断点,结果发现上面的函数是导致问题的函数。
  • 问题标记为 C。不要使用 C++ 编译器编译 C 代码。
  • 您不需要断点,如果发生崩溃,那么调试器将在发生崩溃的实际行上停止。然后只需将鼠标光标移到不同的变量上即可查看它们的值,或者查看“局部变量”窗口。
  • @wildplasser,Visual C++ 包含 C 编译器。

标签: c copy linked-list distinct


【解决方案1】:

这三行可能是罪魁祸首之一:

if(ori == NULL && copy == NULL) { //first time.
    ori = ori->next;
    copy = copy->next;

在这里您检查oricopy 是否为NULL,然后您立即取消引用那些NULL 指针!

【讨论】:

    【解决方案2】:

    在您的代码中, distinct 不是变量的名称,而是结构的名称。您似乎误解了指针,希望这会有所帮助,尽管它与您的问题没有直接关系。

    我省略了很多错误检查以使代码看起来更简单。

    typedef struct listnode {
        char string[50];
        struct listnode *next;
    } list_node;
    
    typedef struct listbase {
        list_node *head;
        int numberOfElements;
    } list;
    
    /* Add a new string at the start of the list
    */
    void ListPrepend(list *myList, char *myString) {
        list_node *newNode = malloc(sizeof *newNode); /* create node to store string */
    
        strcpy(newNode->string, myString); /* copy string into node */
        newNode->next = myList->head;      /* New node now followed by whole list */
        myList->head = newNode;            /* List now starts with new node */
        myList->numberOfElements++;
    }
    
    /* Add a new string at the end of the list
    */
    void ListAppend(list *myList, char *myString) {
        list_node *newNode = malloc(sizeof *newNode), /* create node to store string */
                  *currentNode = myList->head; /* pointer to node so we can find the end */
    
        strcpy(newNode->string, myString); /* copy string into new node */
        newNode->next = NULL;              /* Nothing following this node */
    
        if ( myList->head == NULL ) {
            myList->head = newNode; /* we didn't have a start node so assign it */
        } else {
            /* if there is a next node, move to it */
            while ( currentNode->next != NULL ) {
                currentNode = currentNode->next;
            }
            /* there is no next node so add new node on the end */
            currentNode->next = newNode;
        }
        myList->numberOfElements++;
    }
    
    /* Show the list in order head to tail
    */
    void ListDisplay(list *myList) {
        list_node *currentNode = myList->head;
    
        while ( currentNode != NULL ) {
            printf("%s\n", currentNode->string);
            currentNode = currentNode->next;
        }
    }
    
    int main() {
        list distinct = {0}; /* Now there is a variable called distinct */
        char name[][20] = {"Lim Zheng Yue", "Monkey", "Dave"};
    
        ListAppend(&distinct, name[0]);
        ListAppend(&distinct, name[1]);
        ListPrepend(&distinct, name[2]);
        ListDisplay(&distinct);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-05
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      相关资源
      最近更新 更多