使用变量'head'而不初始化链表c

时间:2014-05-27 14:01:36

标签: c sorting linked-list initialization runtime-error

对C来说真的很新,刚刚开始,我得到一个运行时错误,说“变量'head'正在被使用而没有被初始化。”

带有指向

的黄色箭头
*head = list_p->head_p;

位于:

int listInsert(struct list *list_p, char *string_p)

我不知道为什么。肯定是在初始化它?任何人都可以帮我解决这个问题吗?

以下是整个代码:

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

#define SUCCESS 0
#define FAIL    1

char *phonetic[] = { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot",
                     "golf", "hotel", "india", "juliet", "kilo", "lima", "mike",
                     "november", "oscar", "papa", "quebec", "romeo", "sierra",
                     "tango", "uniform", "victor", "whisky", "xray", "yankee", 
                     "zulu" };

unsigned char indexes[] = { 1, 14, 17, 3, 22, 0, 5, 18, 24, 11, 4, 6, 13, 21,
                            2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };                       

// represents an entry in the linked-list
struct listEntry
{
  char *data_p;               // pointer to the entry's string
  struct listEntry *prev_p;   // pointer to previous entry in the linked-list  
  struct listEntry *next_p;   // pointer to next entry in the linked-list
};

// represents the linked-list
struct list
{
  int entryCount;             // number of entries present in the linked-list
  struct listEntry *head_p;   // pointer to the first entry in the list  
  struct listEntry *tail_p;   // pointer to the last entry in the list
};

// Dynamically allocate & initialise an empty linked list
int listCreate(struct list** list_p2)
{
  // allocate struct list from heap 
  *list_p2 = (struct list*) malloc(sizeof(**list_p2));

  if (*list_p2 != NULL)
  {
    // zero-initialize the list structure 
    memset(*list_p2, 0, sizeof(**list_p2));
    return SUCCESS;    
  }

  return FAIL;
}

// Free all entries in the linked-list and the list structure
int listDestroy(struct list *list_p)
{
  if (list_p != NULL)
  {
    struct listEntry *entry_p = list_p->head_p;

    while (entry_p != NULL)
    {
      struct listEntry *next_p = entry_p->next_p;
      // free the current entry
      free(entry_p);
      // move to the next entry
      entry_p = next_p;
    }

    // free list structure
    free(list_p);
  }

  return FAIL;
}

// Traverse the linked-list from head to tail printing out
// the string data from each list entry
int listPrintForward(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->head_p;
    int count = 0;

    printf("FORWARD: %d entries\n", list_p->entryCount);
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->tail_p)
        printf("\n");

      entry_p = entry_p->next_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Traverse the linked-list from tail to head printing out
// the string data from each list entry
int listPrintReverse(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->tail_p;
    int count = 0;

    printf("REVERSE: %d entries\n", list_p->entryCount);   
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->head_p)
        printf("\n");

      entry_p = entry_p->prev_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
int listInsert(struct list *list_p, char *string_p)
{ 
      // Please write the listInsert function
    if(list_p)
    {
        struct listEntry** head;
        *head = list_p->head_p;

        //Special case for head end.
        if(*head == NULL)
        {
            //list is empty, inserting at first element.
            *head = (struct listEntry*)malloc(sizeof(struct listEntry));
            (*head)->data_p = string_p;
            (*head)->next_p = NULL;
        }
        else
        {
            struct listEntry* temp = *head;
            while(temp->next_p!=NULL)
            {
                if(string_p > temp->next_p->data_p){
                    //string is greater than data, move next
                    temp=temp->next_p;
                }
                else{
                    //at the right place
                    struct listEntry* current = (struct listEntry*)malloc(sizeof(struct listEntry));

                    current->data_p = string_p;
                    current->next_p = temp->next_p;
                    temp->next_p = current;

                    }
            }
        }
        return SUCCESS;

    }


  return FAIL;  
}

int main(int argc, char **argv)
{
  struct list *list_p = NULL;
  (void) argc;
  (void) argv;

  if (listCreate(&list_p) == SUCCESS)
  {
    unsigned int count;

    // insert every word in the phonetic alphabet into the
    // linked-list.
    printf("INSERT:\n");
    for (count = 0; count < sizeof(indexes); count++)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", phonetic[indexes[count]]);
      }
      else
      {
        printf("%s ", phonetic[indexes[count]]);
      }
      listInsert(list_p, phonetic[indexes[count]]);
    }
    printf("\n");

    // print out the list in alphabetical order
    listPrintForward(list_p);
    // print out the list in reverse alphabetical order
    listPrintReverse(list_p); 

    // Destroy the linked list and free all associated memory
    listDestroy(list_p);               
  }

  return SUCCESS;
} 

我正在尝试按字母顺序在C中对链接列表进行排序。除了这个错误,有人知道我是否在正确的行? 我正在学习插入功能的本教程 https://www.youtube.com/watch?v=-Wt88_rkWaE

任何帮助都会受到赞赏,谢谢你!

2 个答案:

答案 0 :(得分:1)

struct listEntry** head;

上面的行将head定义为指向struct listEntry的指针,但不指定任何值。

以下一行

    *head = list_p->head_p;

使用head运算符对uninitialise *进行解除引用,即查找指向的位置,然后写入其中的内容,如左侧=运营商的一面。

它引发了对未经过无限制的变量进行操作的未定义行为。


<强>更新

要解决此问题,您可能希望

struct listEntry * head;
head = list_p->head_p;

答案 1 :(得分:1)

在第

*head = list_p->head_p;

您没有初始化head,而是head指向的内容。要初始化head,您需要

head = ...