2

我从这个网站阅读了代码:http: //www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-CC,但它给了我分段错误,我不太明白.

*我将其修改为我的结构

struct Node
{
    int type;
    char cmd[256];
    struct Node *next;
};

struct Node *head = NULL;

void insert(int val, char arr[])
{
    struct Node *temp1 = (struct Node*)malloc(sizeof(struct Node));
    struct Node *temp2 = (struct Node*)malloc(sizeof(struct Node));
    temp1 = head;
    while(temp1->next != NULL)
        temp1 = temp1->next;

    temp2->type = val;
    strcpy(temp2->cmd, arr);

    temp2->next = NULL;
    temp1->next = temp2;
}

这段代码有什么问题?

好的,这个问题解决了。谢谢Guyz'^'!您是否知道如何将字符 " (ASCII 34) 放入 printf 字符串中?(例如,如果我执行 printf("Print this "sentence""); 它会在句子中给我错误,cut I cast another set of “”里面有一个“”。谢谢一堆。

4

4 回答 4

3

首先,您未能在初始插入时设置头指针。这可以通过简单的头部检查来完成,但如果插入循环设置正确,则不需要这样做。其次,您正在泄漏内存。这不是 Java。覆盖一个保存动态分配地址的指针就像把内存扔出窗口一样好。

这是一种无需if (head == NULL)在插入代码中隐藏特殊情况的方法。与流行的观点相反,如果您这样做,则不需要这种特殊情况:

void insert(int val, char arr[])
{
    struct Node **pp = &head;
    while (*pp)
        pp = &(*pp)->next;

    *pp = malloc(sizeof(**pp));
    (*pp)->next = NULL;
    (*pp)->type = val;
    strcpy((*pp)->cmd, arr);
}

只需确保head在进行任何插入之前已初始化为 NULL,通过查看更新后的帖子,看起来您所做的一切都是正确的。

最后,不要malloc()在 C 程序中转换结果。

于 2013-09-29T09:35:50.990 回答
1

您需要head在运行第一次插入之前进行初始化:

/* This should go in main or some init function, before the first insert */
head = (struct Node *)malloc(sizeof(struct Node));
head->next = NULL;
于 2013-09-29T09:28:05.963 回答
1

试试这个,它将纠正内存泄漏并检查 head 是否有效。如果您仍然有分段错误,您应该运行一个调试器来确切地知道发生了什么。

void insert(int val, char arr[])
{
    struct Node *temp2 = malloc(sizeof(struct Node));
    temp2->type = val;
    strcpy(temp2->cmd, arr);
    temp2->next = NULL;

    if (head == NULL) {
      //list is empty, head must points on the created node
      head = temp2;
    }
    else {
      struct Node *temp1 = head;

      while(temp1->next != NULL)
        temp1 = temp1->next;

      temp1->next = temp2;
   }
}

编辑:现在,这个函数应该处理任何情况,即使head是空的。(当列表为空时)

于 2013-09-29T09:31:19.057 回答
0

看到你提到的链接,还有一个测试文件,http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=24684,它会告诉你为什么会出现这个错误,当从后面插入时,它将首先检查头部是否为空并为第一个元素分配空间。

块引用

1  #include<iostream>
  2  
  3  using namespace std;
  4  
  5  typedef struct node
  6  {
  7      int data;   // will store information
  8      node *next; // the reference to the next node
  9  };
 10  
 11  
 12  int main()
 13  {
 14      node *head = NULL;  //empty linked list
 15      int info = 0, node_number = 0,  counter = 0;
 16      char ch;
 17  
 18      do{
 19          cout<<"\n\n";
 20          cout<<"0.Quit\n";
 21          cout<<"1.Insert at first\n";
 22          cout<<"2.Traverse\n";
 23          cout<<"3.Insert at last\n";
 24          cout<<"4.Insert after specified number of node\n";
 25          cout<<"5.Delete at first node\n";
 26          cout<<"6.Delete at last node\n";
 27          cout<<"7.Delete specified number of node\n";
 28          cout<<"8.Sort nodes\n";
 29  
 30          cout<<"Enter your choice: ";
 31          cin>>ch;
 32  
 33      switch(ch)
 34      {
 35  
 36      case '0': break;
 37  
 38      case '1': ....

  .....  case '3':{
           **// check linked list is empty**
           if(head==NULL)
           {
               cout<<"ENTER ANY NUMBER:";
               cin>>info;                        // take input data
               cout<<"Input data: "<<info;

               node *temp;                     // create a temporary node
               temp = (node*)malloc(sizeof(node)); // allocate space for node
               temp->data = info;               // store data(first field)
               temp->next = NULL;               // second field will be null
               head = temp;                    // transfer the address of 'temp' to 'head'
               counter++;
           }

           else
           {
               cout<<"ENTER ANY NUMBER:";
               cin>>info;                        // take input data
               cout<<"Input data: "<<info;
               node *temp1;                        // create a temporary node
               temp1=(node*)malloc(sizeof(node));  // allocate space for node
               temp1 = head;                   // transfer the address of 'head' to 'temp'
               while(temp1->next!=NULL)         // go to the last node
                   temp1 = temp1->next;         //tranfer the address of 'temp->next' to 'temp'

               node *temp;                 // create a temporary node
               temp = (node*)malloc(sizeof(node));// allocate space for node
               temp->data = info;               // store data(first field)
               temp->next = NULL;               // second field will be null(last node)
               temp1->next = temp;              // 'temp' node will be the last node
               break;
            }
   }
于 2013-09-29T09:48:42.137 回答