在C中使用链接列表

时间:2015-08-13 17:17:33

标签: c pointers linked-list

我用C语言学习编程,我遇到了一些麻烦,特别是在使用指针时。这对我来说有点困难,因为我们不会在Java或C#中使用指针。

我尝试做的是创建链接列表(在互联网上找到的代码)并推送其中的元素。如下面的代码所示。

当我取消注释第二行代码时,代码可以工作,但我收到以下列表作为答案{0,1,2},即使我没有在列表中推送数字0 。我希望得到这个答案:{1,2}。

int main (){
    node_t * test_list = NULL;

    //test_list = malloc(sizeof(node_t));

    push(test_list, 1);
    push(test_list, 2);

    print_list(test_list);

    return 0;
}

这就是代码的样子:

typedef struct node
{
  int val;
  struct node * next;
}              node_t;

void print_list(node_t * head)
{
  node_t * current = head;

  while (current != NULL)
    {
      printf("%d\n", current->val);
      current = current->next;
    }
}

node_t* new_node(node_t * head)
{
  node_t * head2 = malloc(sizeof(node_t));

  return  head2;
}

void push(node_t * head, int val)
{
  if (head == NULL)
    {
      head = new_node(head);
      head->val = val;
      head->next = NULL;
    }
  else
    {
      node_t * current = head;
      while (current->next != NULL)
          current = current->next;

      /* now we can add a new variable */
      current->next = malloc(sizeof(node_t));
      current->next->val = val;
      current->next->next = NULL;
    }
}

在push函数中,我决定检查head是否等于NULL。如果是这种情况,我只想创建一个新节点并将其分配给它。我不知道这是不是一个好方法。然而,它没有用。

如果有人能引导我走上正确的道路,我将感激不尽!

谢谢!

(代码来源:http://www.learn-c.org/en/Linked_lists

5 个答案:

答案 0 :(得分:1)

我会尝试解释额外的元素0.

def check():
    if len(nameOne.get()) == 0 or len(nameTwo.get()) == 0 or len(comment.get(INSERT)) == 0:
        showwarning()
    else:
        print 'True'

check()

你应该在malloc:

之后用一些值初始化列表的头部
test_list = malloc(sizeof(node_t)); //this changes the pointer test_list                
                                    //which is initially NULL to a non-NULL pointer, hence the mysterious extra element "0"

 push(test_list, 1);

void push(node_t * head, int val) {
if (head == NULL) //head is not NULL at this point because you called malloc earlier on it, so 1 will be inserted in the next position
{...}

现在,第一个元素不是0,它将是5。

答案 1 :(得分:1)

如果您在代码中取消注释以下行

//test_list = malloc(sizeof(node_t));

这里是在调用push函数之前分配头指针。 因此,下面的代码行永远不会被执行

 if (head == NULL)
    {
        head = new_node(head);
        head->val = val;
        head->next = NULL;}

因为你已经为头指针进行了一次malloced,你还没有初始化它,然后是两个推送函数。所以你会在列表中看到0 / garbage,1,2而不是1,2。

当您为test_list注释malloc时,请在下面的代码中

if (head == NULL)
    {
        head = new_node(head);
        head->val = val;
        head->next = NULL;
    }else
    {

        node_t * current = head;
        while (current->next != NULL) {
            current = current->next;
        }

由于您没有发送test_list的地址(& test_list-您需要使用双指针),因此if case中所做的任何更改都不会反映在test_list中。

浏览链接以便清楚了解 - linked_list

答案 2 :(得分:0)

尝试以下功能。

node_t * new_node( int val )
{
    node_t * n = malloc( sizeof( node_t ) );

    if ( n )
    {
        n->val = val;
        n->next = NULL;
    }

    return  n;
}

void push( node_t **head, int val ) 
{
    node_t *n = new_node( val );

    if ( n )
    {
        while ( *head ) head = &( *head )->next;
        *head = n;
    }
}

必须像

一样调用函数push
push( &test_list, 1 );

至于您的功能push实施,它会处理test_list的副本。因此test_list的原始值不会更改。

答案 3 :(得分:0)

来自莫斯科的Vlad和Violeta Marin已经说明了它不起作用的原因。我做了一些修改在你的代码中使其工作,

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

  typedef struct node
  {
     int val;
     struct node * next;
  } node_t;

  void print_list(node_t * head) 
  {
      node_t * current = head;
      printf("called\n");
      while (current != NULL) {
         printf("%d\n", current->val);
         current = current->next;
      }
  }


  node_t* new_node()
  {
     //you have to return NULL , if malloc fails to allocate memory 
     node_t * head2 = malloc(sizeof(node_t));
     return  head2;
  }

  void push(node_t **head, int val)
  {
      if (*head == NULL)
      {
          printf("null\n");  
         //you have to check the return value of new_node
         (*head) = new_node();
         (*head)->val = val;
         (*head)->next = NULL;
      }
      else {
          printf("not null\n");
          node_t * current = *head;

          while (current->next != NULL) {
              current = current->next;
          }
          /* now we can add a new variable */
          //you have to check the return value of malloc
          current->next = malloc(sizeof(node_t));
          current->next->val = val;
          current->next->next = NULL;
       }
  }

  void my_free(node_t *head)
  {
    node_t *temp= NULL;
    while(head) 
    {
       temp=head->next;
       free(head);
       head=temp;
    }
}
int main ()
{
   node_t *test_list = NULL;
   push(&test_list, 1);
   push(&test_list, 2);
   print_list(test_list);
   my_free(test_list);
   return 0;
}

答案 4 :(得分:0)

以下代码:

{{1}}
相关问题