首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未初始化链接列表c使用的变量“head”

未初始化链接列表c使用的变量“head”
EN

Stack Overflow用户
提问于 2014-05-27 14:01:35
回答 2查看 232关注 0票数 0

对C非常陌生,刚开始时,我就收到一个运行时错误,它说“变量'head‘被使用而没有被初始化。”

一支黄色的箭头指向

代码语言:javascript
复制
*head = list_p->head_p;

该文件载于:

代码语言:javascript
复制
int listInsert(struct list *list_p, char *string_p)

我不知道为什么。那肯定是在初始化它?有人能帮我解决这个问题吗?

以下是整个代码:

代码语言:javascript
复制
#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中的链接列表,除了这个错误之外,有没有人知道im在正确的行上?我按照本教程学习了插入函数rkWaE

任何帮助都将不胜感激,谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-27 14:07:08

代码语言:javascript
复制
struct listEntry** head;

上面的行将head定义为指向指向struct listEntry的指针的指针,但没有为其赋值。

下一行

代码语言:javascript
复制
    *head = list_p->head_p;

在未初始化的head上操作,使用*操作符解除它的引用,也就是说,它查找它所指向的位置,然后将一些东西写到那里,作为=操作符的左边。

它引发在未初始化变量上操作的未定义行为。

更新

要解决这个问题,您可能希望这样做。

代码语言:javascript
复制
struct listEntry * head;
head = list_p->head_p;
票数 1
EN

Stack Overflow用户

发布于 2014-05-27 14:07:50

排在队伍里

代码语言:javascript
复制
*head = list_p->head_p;

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

代码语言:javascript
复制
head = ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23891565

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档