不使用双指针的c中的链表实现

Linked list implementation in c without using double-pointer

我用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;
}

是的,您可以仅使用单个指针重写此代码,但您必须更改 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 的实施也是如此。

将 (&head,6) 替换为 (head,6)。因为你没有传递头部的地址,所以在接收端你有 push(struct node* head, int new_data).Rest以上给出的答案都已澄清

另一个解决方案是创建一个名为 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;
}