代码之家  ›  专栏  ›  技术社区  ›  Matt

C链表搜索功能[关闭]

  •  -5
  • Matt  · 技术社区  · 16 年前
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    /*Define custom functions */
    void insertElement();
    bool elementExists();
    int getNumElements();
    
    
    /*Create linked list */
    struct node {
        int number;
        int occurence;
        struct node *next;
    };
    
    /*Call our linked list freqTable */
    struct node *freqTable = NULL;
    
    unsigned int numElements = 0;
    
    int main(){
        int readNumElements = 0;
        int i = 0;
        int newNum, status;
    
        status = scanf("%d", &readNumElements);
        if(status == -1){
            fprintf(stderr, "%d is not a number\n", readNumElements);
            exit(-1);
        }
    
    
        for (i = 0; i < readNumElements;i++) {
            status = scanf("%d", &newNum);
            if(status == -1){
                fprintf(stderr, "%d is not a number\n", newNum);
                exit(-1);
            }
            if(elementExists(newNum)){
                printf("%d exists\n", newNum);
            }else{
                insertElement(&freqTable, newNum);
            }
    
        }
    
    return 0;
    }
    
    void insertElement(struct node **list, int n){
        struct node *new_input; 
    
        new_input = malloc(sizeof(struct node));
    
        if(new_input == NULL){
            fprintf(stderr,"Error: Failed to create memory for new node\n");
            exit(EXIT_FAILURE);
        }
    
        new_input->number = n;
        new_input->occurence = 1;
        new_input->next = *list;
        numElements++;
    
        *list = new_input;
    }
    
    bool elementExists(int n){
        printf("%d\n", freqTable->number);
    return false;
    }
    
    int getNumElements(){
        return numElements;
    }
    

    好的,这是我得到的。这应该可以编译。

        if(elementExists(newNum)){
            printf("%d exists\n", newNum);
        }else{
            insertElement(&freqTable, newNum);
        }
    

    我得到分割错误,我不知道为什么。

    3 回复  |  直到 14 年前
        1
  •  2
  •   codaddict    16 年前

    在函数中 elementExists 您需要确保freqTable不是 NULL :

    bool elementExists(int n){
     if(freqTable) {  // add this check
       printf("%d\n", freqTable->number);
     }
    }
    

    也是你的 元素存在 不执行它应该执行的操作(检查是否存在具有值的节点) n ),您应该执行以下操作:

    bool elementExists(int n) {
    
     if(!freqTale) { // table does not exist..return false.
      return false;
     }
     // table exists..iterate node by node and check.
     struct node *tmp = freqTable;
     while(tmp) { // loop till tmp becomes NULL
      if(*tmp == n) { // it node contains n..return false.
        return true;
       }
      tmp = tmp->next; // move on
     }
     return false; // n does not exist in the list..return false.
    }
    
        2
  •  1
  •   Arkku    16 年前

    打给的电话有问题 elementExists

    if(elementExists(&freqTable, newNum)){
    

    你通过考试 而不是它的值。但在 元素存在

    printf("Compare with: %d\n", list->number);
    

    和

    else list = list->next;
    

    拆下 & 从呼叫到 元素存在 . 不要引用全局 freqTable 里面 元素存在 如果您将列表作为参数传递。

        3
  •  0
  •   Matthew Flaschen    16 年前

    freqTable elementExists 在分配任何节点之前。因此, 始终为NULL,因此调用未定义的行为。您只需在中添加一个NULL检查 元素存在 ,在这种情况下返回false。

    推荐文章