简体   繁体   English

在链表中的第n个位置插入元素 - C.

[英]insert element at nth position in the linked list - C

my program shows list is empty. 我的程序显示列表为空。 I think I'm making mistake on re-linking the nodes to the head. 我认为我在将节点重新连接到头部时犯了错误。 Help me figure it out. 帮帮我搞清楚。

void insert(struct node** headRef, int index, int Data)
{
  int i, distanceFromHead = 1;
  struct node* head = *headRef;
  struct node* temp1 = (struct node*)malloc(sizeof(struct node)); //node to be inserted.
  temp1->data = Data;
  if(index == 0)
  {
    temp1->next = head;
    head = temp1;
    return;
  }
  while(head != NULL)
  {
    if(distanceFromHead == index)
    {
        temp1->next = head->next;
        head->next = temp1;
        *headRef = head;
        return;
    }
    head = head->next;
    distanceFromHead++;
  }

}

You're using head to iterate through the linked list and if index matched with distance then updating headref . 您正在使用head来遍历链表,如果索引与distance匹配,则更新headref

Problem is *headRef = head in while..if 问题是*headRef = head in while..if

And in if(index == 0) assign temp1 to *headref ie *headref=temp1 并且在if(index == 0) *headref temp1分配给*headref*headref=temp1

Your have two conditions: 你有两个条件:

  • finding the postion for insertion in the Linked list 找到要在链接列表中插入的位置
  • not falling off the end of the Linked List 不会脱离链接列表的末尾

And, of course, you have to assign to *headRef , not to some local pointer variable. 当然,你必须分配给*headRef ,而不是某些本地指针变量。 And you should not call malloc before you are absolutely sure you really need the memory. 在你完全确定你真的需要内存之前,你不应该调用malloc。

You can combine the two conditions in a single loop: 您可以在一个循环中组合这两个条件:


void insert1(struct node **headRef, int index, int Data)
{
  struct node *temp1;
  int distanceFromHead = 0;

  for( ; *head; head = &(*head)->next) {
    if(distanceFromHead == index) break;
    distanceFromHead++;
  }

  if (distanceFromHead != index) return; // index not found: list too short

  temp1 = malloc(sizeof *temp1); //node to be inserted.
  temp1->data = Data;
  temp1->next = *head;
  *head = temp1;
}

You dont need the distanceFromHeadvarable; 你不需要distanceFromHeadvarable; you could just as well decrement the index: 你也可以减少索引:

void insert2(struct node **headRef, int index, int Data)
{
  struct node *temp1;

  for( ; *head; head = &(*head)->next) {
    if(!index) break;
    index--;
  }
  if (index) return; // index not found: list too short

  temp1 = malloc(sizeof *temp1); //node to be inserted.
  temp1->data = Data;
  temp1->next = *head;
  *head = temp1;
}

Now, the test for index!=0 is repeated after the loop. 现在,循环后重复index!=0的测试。 This can be avoided by moving the insertion inside the loop, and jumping out afterwards: 这可以通过在循环内移动插入并在之后跳出来避免:

void insert3(struct node **headRef, int index, int Data)
{

  for( ; *head; head = &(*head)->next) {
    struct node *temp1;
    if(index--) continue;

    temp1 = malloc(sizeof *temp1); //node to be inserted.
    temp1->data = Data;
    temp1->next = *head;
    *head = temp1;
    break; // or : return;
  }

  return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM