在C链接列表实现,而无需使用双指针 [英] Linked list implementation in c without using double-pointer

查看:114
本文介绍了在C链接列表实现,而无需使用双指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现在C语言中一个简单的链表,但它可以被不使用双指针(**)实现的。我想通过只使用单一指针来实现相同的程序。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构节点
{
  int数据;
  结构节点*接下来的;
};
空推(结构节点** head_ref,INT NEW_DATA)
{
    结构节点* new_node =(结构节点*)malloc的(的sizeof(结构节点));
    new_node->数据= NEW_DATA;
    new_node->接下来=(* head_ref);
    (* head_ref)= new_node;
}无效追加(结构节点** head_ref,INT NEW_DATA)
{
    结构节点* new_node =(结构节点*)malloc的(的sizeof(结构节点));
    结构节点*最后= * head_ref;在步骤5中使用/ * * /
    new_node->数据= NEW_DATA;
    new_node->接着= NULL;
    如果(* head_ref == NULL)
    {
       * head_ref = new_node;
       返回;
    }
    而(与最后所GT;!下次= NULL)
        最后的最后所=>接下来,
    最后所>接下来= new_node;
    返回;
}无效的printList(结构节点*节点)
{
  而(节点!= NULL)
  {
     的printf(%D,与于节点GT;数据);
     节点=于节点>接下来,
  }
}
诠释的main()
{
  结构节点*头= NULL;
  追加(安培;头,6);
  推(安培;头,7);
  推(安培;头,1);
  追加(安培;头,4);
  的printf(\\ n创建的链接列表是:);
  的printList(头);
  的getchar();
  返回0;
}

是否有可能取代结构节点** head_ref与结构节点* head_ref?


改code后建议(仍然没有得到输出)

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;结构节点
{
  int数据;
  结构节点*接下来的;
};
结构节点*推(结构节点*头,诠释NEW_DATA)
{
    结构节点* new_node =(结构节点*)malloc的(的sizeof(结构节点));
    new_node->数据= NEW_DATA;
    new_node->接下来=头;
    头= new_node;
    返回头;
}结构节点*追加(结构节点*头,诠释NEW_DATA)
{
    结构节点* new_node =(结构节点*)malloc的(的sizeof(结构节点));
    结构节点*最后=头;在步骤5中使用/ * * /
    new_node->数据= NEW_DATA;
    new_node->接着= NULL;
    如果(头== NULL)
    {
       头= new_node;
       返回头;
    }
    而(与最后所GT;!下次= NULL)
        最后的最后所=>接下来,
    最后所>接下来= new_node;
    返回头;
}
无效的printList(结构节点*节点)
{
  而(节点!= NULL)
  {
     的printf(%D,与于节点GT;数据);
     节点=于节点>接下来,
  }
}诠释的main()
{
  结构节点*头= NULL;
  头=追加(安培;头,6);
  头=推(安培;头,7);
  头=推(安培;头,1);
  头=追加(安培;头,4);
  的printf(\\ n创建的链接列表是:);
  的printList(头);
  的getchar();
  返回0;
}


解决方案

是的,你可以只使用单一的指针改写这个code,但你必须改变语义的API,并在它的模式用了。

从本质上讲,你替换

间接的第二级

 无效推(结构节点** head_ref,INT NEW_DATA)

与客户端分配的,即

 结构节点*推(结构节点*头,诠释NEW_DATA)

这意味着,而不是

 推(安培;头,NUM);

来电将有写

  =头推(头,NUM);

也是一样的实施追加

I have implemented a simple linked list in C language, but can it be implemented without using double-pointer(**).I want to implement same program by using only single pointers.

#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;
}

Is it possible to replace "struct node** head_ref" with "struct node* head_ref"?


Changed code after suggestions(still not getting output)

#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;
}

解决方案

Yes, you can rewrite this code using only single pointers, but you would have to change the semantic of your API and the pattern in which it is used.

Essentially, you replace the second level of indirection in

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

with a client-side assignment, i.e.

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

This means that instead of

push(&head, num);

the caller will have to write

head = push(head, num);

Same goes for the implementation of append.

这篇关于在C链接列表实现,而无需使用双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆