用一个程序创建多个链表

Creating multiple linked lists with one program

本文关键字:创建 链表 程序 一个      更新时间:2023-10-16

我是链表的新手,我正试图编写一个程序,在这个程序中,我们可以简单地将新的头传递给add()函数,并创建我们想要的尽可能多的列表。但不知何故,代码根本不起作用。从输出来看,似乎每次调用add()函数时,即使使用相同的头地址,也会创建一个新的头。

谁能告诉我该怎么做?

这是我写的:

#include<stdio.h>
#include<iostream>
using namespace std;
struct node
{
  struct node *next;
  int val;
};

void add(int i,node** h,node** e)
{
  node* head = *h;
  node* endnode = *e;
  printf("addingn");
  if(head!=NULL)
  {
      node *n = (struct node*)malloc(sizeof(node));
      n->next  = NULL;
      n->val = i;
      endnode->next = n;
      endnode = n;
  }
  else
  {
      printf("headingn");
      head  = (struct node*)malloc(sizeof(node));
      head->next = NULL;
      head->val = i;
      endnode = head;
  }
  }
  void delete_node(int i,node** h,node** e)
  {
    node* head = *h;
    node* endnode = *e;
    node *temp;
    node *n = head;
    while(n!=NULL)
    {
     if(n->val == i)
     {
        if(n==head)
        {
            head = head->next;
        }
        else if(n==endnode)
        {
            temp->next = NULL;
            endnode = temp;
        }
        else
        {
            temp->next = n->next;       
        }
        free(n);
        break;
    }
    else
    {
        temp = n;
        n = n->next;
    }
  }
}
 void display(node** h)
 {
   node* head = *h;
   node *n = head;
   while(n!=NULL)
   {
    printf("%dn",n->val);
    n = n->next;
   }
 }

int main()
{
  node *head  = NULL;
  node *endnode  = NULL;    
  add(5,&head,&endnode);
  add(8,&head,&endnode);
  add(1,&head,&endnode);
  add(78,&head,&endnode);
  add(0,&head,&endnode);
  display(&head);
  printf("nn");
  system("pause");
  return 0;
}

撇开设计,直接问你的问题,问题是:

  1. 在main()

    中创建一个指针
    node *head  = NULL;
    
  2. 将其地址传递给函数,从而拥有指向指针的指针

    void add(int i,node** h,node** e)
    
  3. 你取消了它的引用,这样就有了确切的指针在

    之外
    node* head = *h;
    
  4. 赋值给指针的本地副本

    head  = (struct node*)malloc(sizeof(node));
    
  5. 您继续愉快地认为您已经更新了我在1中列出的指针。

作为个人意见,我同意这些评论:你可以用一个更简洁的设计来代替那些双指针。


编辑:为了帮助你理解,这里有一个正确的版本,你的确切设计

void add(int i,node** h,node** e)
{
    printf("addingn");
    if(*h!=NULL)
    {
        node *n = (struct node*)malloc(sizeof(node));
        n->next  = NULL;
        n->val = i;
        (*e)->next = n;
        *e = n;
    }
    else
    {
        printf("headingn");
        *h  = (struct node*)malloc(sizeof(node));
        (*h)->next = NULL;
        (*h)->val = i;
        *e = *h;
    }
}

在你的第一个版本中,你是这样做的(伪代码):

void functon(int** ptrToPtrToInt) {
  int* ptrToInt = *ptrToPtrToInt; // suppose ptrToInt now contains 0x20 (the address of the integer)
  ptrToInt = malloc(sizeof(int)); // ptrToInt loses the 0x20 and gets a new address
} // ptrToInt gets destroyed, but nobody updated the pointer where ptrToPtrToInt came from, thus is unchanged

也许不只是使用node,而是为列表创建一个struct。

struct list{
    node *head;
    int size;
}
void add(list *l, node *n){
    // check nulls
    // check size = 0
    // add and increment size
}

然后让你所有的函数都在上面工作,而不是你的节点,这样你就可以创建多个节点。

传递列表指针,这样您就可以访问列表的头部。