【问题标题】:Linked list implementation in c without using double-pointerc中的链表实现不使用双指针
【发布时间】:2015-09-09 17:29:33
【问题描述】:

我已经用C语言实现了一个简单的链表,但是不使用双指针(**)可以实现吗?我想只使用单指针来实现相同的程序。

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

struct node
{
  int data;
  struct node *next;
};


void push(struct node** head_ref, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}



void append(struct node** head_ref, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    struct node *last = *head_ref;  /* used in step 5*/
    new_node->data  = new_data;
    new_node->next = NULL;
    if (*head_ref == NULL)
    {
       *head_ref = new_node;
       return;
    }
    while (last->next != NULL)
        last = last->next;
    last->next = new_node;
    return;
}

void printList(struct node *node)
{
  while (node != NULL)
  {
     printf(" %d ", node->data);
     node = node->next;
  }
}
int main()
{
  struct node* head = NULL;
  append(&head, 6);
  push(&head, 7);
  push(&head, 1);
  append(&head, 4);
  printf("\n Created Linked list is: ");
  printList(head);
  getchar();
  return 0;
}

是否可以将“struct node** head_ref”替换为“struct node* head_ref”?


建议后更改代码(仍然没有得到输出)

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

struct node
{
  int data;
  struct node *next;
};


struct node* push(struct node* head, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = head;
    head   = new_node;
    return head;
}

struct node* append(struct node* head, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    struct node *last = head;  /* used in step 5*/
    new_node->data  = new_data;
    new_node->next = NULL;
    if (head == NULL)
    {
       head = new_node;
       return head;
    }
    while (last->next != NULL)
        last = last->next;
    last->next = new_node;
    return head;
}


void printList(struct node *node)
{
  while (node != NULL)
  {
     printf(" %d ", node->data);
     node = node->next;
  }
}

int main()
{
  struct node* head = NULL;
  head= append(&head, 6);
  head=push(&head, 7);
  head=push(&head, 1);
  head=append(&head, 4);
  printf("\n Created Linked list is: ");
  printList(head);
  getchar();
  return 0;
}

【问题讨论】:

  • 如果你将你的函数结果用于void之外的东西,当然。
  • 当然 - 将头部作为函数结果返回,而不是所有那些 voids
  • @WhozCraig 我们中只有一个人应该在这个标签上:)
  • 使用哨兵节点会简化这一点,请参阅here

标签: c pointers data-structures linked-list


【解决方案1】:

是的,您可以仅使用单个指针来重写此代码,但您必须更改 API 的语义和使用它的模式。

本质上,您替换了第二级间接

void push(struct node** head_ref, int new_data)

带有客户端分配,即

struct node* push(struct node* head, int new_data)

这意味着,而不是

push(&head, num);

调用者必须写

head = push(head, num);

append 的实现也是如此。

【讨论】:

  • 我要成为一个全球性的人吗
  • printList 函数呢?
  • @dexter printList 有一个星号,因为它读取列表,从不写入。您不必将head 设为全局 - 您可以以相同方式将其传递给函数,而无需使用&amp; 运算符,并将结果分配回head。这两种 API 都可以让您创建任意数量的列表。
  • 我已经按照你说的做了所有的改变,我的代码编译得很好,但我没有得到任何输出
  • @dexter - 好的,是时候拿出你的调试器了。
【解决方案2】:

将 (&head,6) 替换为 (head,6)。由于您没有传递头的地址,因此在接收端您有 push(struct node* head, int new_data)。其余的都已在上面阐明给出答案

【讨论】:

    【解决方案3】:

    另一种解决方案是创建一个名为head 的空节点,然后创建一个指向该节点的指针list。然后您可以将list 传递给所有函数,就像这样

    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    void push(struct node *list, int new_data)
    {
        struct node* new_node = malloc(sizeof(struct node));
        new_node->data = new_data;
        new_node->next = list->next;
        list->next = new_node;
    }
    
    void append(struct node *list, int new_data)
    {
        while ( list->next != NULL ) 
            list = list->next;
        push( list, new_data );
    }
    
    void printList(struct node *node)
    {
        for ( node=node->next; node != NULL; node=node->next )
            printf(" %d ", node->data);
        printf( "\n" );
    }
    
    int main( void )
    {
        struct node head = { 0, NULL };
        struct node *list = &head;
    
        append(list, 6);
        push(list, 7);
        push(list, 1);
        append(list, 4);
        printf("\n Created Linked list is: ");
        printList(list);
        getchar();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-29
      • 2012-04-20
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2019-04-20
      相关资源
      最近更新 更多