链表第n次插入失败

问题描述 投票:0回答:1

我写了一段代码来在LinkedList中插入数据。但是输出数据有问题。这是代码。

#include <stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
struct node {
  char *name;
  int age;
  struct node *next;
};
struct node *linkeslist1head= NULL;
struct node *linkedlist1tail= NULL;
void llinsertend(const char *a,const int *b){
  struct node *current = malloc(sizeof(struct node));
  if(current == NULL){
    printf("Current creation failed.\n");
  }
  current->name = malloc(strlen(a)+1);
  if(current->name == NULL) {
    printf("String allocation failed\n");
  }
  strcpy(current->name,a);
  current->age = *b;
  if(linkeslist1head == NULL){
    linkeslist1head = current;
    linkedlist1tail = current;
  }else{
    //If the list is not empty, append the new node to the end
    linkedlist1tail->next = current;
    // Update tail to point to the new last node
    linkedlist1tail = current;
  }
}
void llinsertbegin(const char *a, const int *b) {
  struct node *newnode = malloc(sizeof(struct node));
  if (newnode == NULL) {
    printf("Memory allocation failed\n");
    return;
  }

  newnode->name = malloc(strlen(a) + 1);
  if (newnode->name == NULL) {
    printf("String allocation failed\n");
    free(newnode);
    return;
  }

  strcpy(newnode->name, a);
  newnode->age = *b;

  if (linkeslist1head == NULL) {
    // If the list is empty
    newnode->next = NULL;
    linkeslist1head = newnode;
    linkedlist1tail = newnode;
  } else {
    // If the list is not empty
    newnode->next = linkeslist1head;
    linkeslist1head = newnode;
  }
}
void llinsertaftern(const char *a, const int *b, int n) {
  struct node *current = linkeslist1head;
  int i;
  for (i = 1; current != NULL && i < n; i++){
    current = current->next; // Iterate until the (n-1)th node or until current 
                               becomes NULL
  }
  if (current == NULL){
    printf("LL short\n");
    return; // Exit the function if current is NULL
  }

  printf("Reached node %d\n", i);
  struct node *newnode = malloc(sizeof(struct node));
  if (newnode == NULL) {
    printf("Memory allocation failed\n");
    return;
  }

  newnode->name = malloc(strlen(a) + 1);
  if (newnode->name == NULL) {
    printf("String allocation failed\n");
    free(newnode);
    return;
  }

  strcpy(newnode->name, a);
  newnode->age = *b;

  if (current == NULL) {
    printf("LL is shorter than %d\n", n);
    free(newnode->name);
    free(newnode);
    return;
  }

  newnode->next = current->next;
  current->next = newnode;
}
void outputLinkedList(struct node *head){
  struct node *p = head;
  while(p != NULL){
    printf("Name:%s Age:%d\n",p->name,p->age);
    p = p->next;
  }
  printf("\n");
}
int main() {
   printf("How many persons' details you want to add\n");
   int t;
   scanf("%d",&t);
   getchar();
   for(int i=1;i<=t;i++){
     int x;
     char name[50];
     scanf("%s",name);
     getchar();
     scanf("%d",&x);

     llinsertend(name,&x);
   }
   int x=10,y=20,z=30;
  llinsertbegin("facebook",&x );//(const int *) 10
  llinsertbegin("instragram", &y);
  llinsertbegin("whatsapp", &z);
  outputLinkedList(linkeslist1head);
  int code=1200,pos = 3;
  llinsertaftern("Dhaka",&code,pos);
  outputLinkedList(linkeslist1head);
  }

在第二个输出中,LinkedList 在我的 CLion 和 Codeblocks 中都不起作用,但它在编译器资源管理器中起作用。我的意思是通过 llinsertend() 函数添加一些元素然后 llinsertbegin() 输出函数完美运行。但是当我使用 llinsertaftern 函数时,它在调试器中显示分段错误。假设如果我通过 insertend 函数添加 3 个元素,并通过 insertbegin 函数添加 3 个元素,那么 pos = 3 的 inseraftern 的问题是什么。它应该工作得很好。

c data-structures linked-list singly-linked-list
1个回答
0
投票

您的代码包含许多错误。例如,让我们考虑函数

llinsertend

void llinsertend(const char *a,const int *b){
  struct node *current = malloc(sizeof(struct node));
  if(current == NULL){
    printf("Current creation failed.\n");
  }
  current->name = malloc(strlen(a)+1);
  if(current->name == NULL) {
    printf("String allocation failed\n");
  }
  strcpy(current->name,a);
  current->age = *b;
  if(linkeslist1head == NULL){
    linkeslist1head = current;
    linkedlist1tail = current;
  }else{
    //If the list is not empty, append the new node to the end
    linkedlist1tail->next = current;
    // Update tail to point to the new last node
    linkedlist1tail = current;
  }
}

首先,如果未分配新节点,则继续处理空指针

  if(current == NULL){
    printf("Current creation failed.\n");
  }
  current->name = malloc(strlen(a)+1);
  //...

同样,如果未分配字符数组,您将再次继续使用空指针

  if(current->name == NULL) {
    printf("String allocation failed\n");
  }
  strcpy(current->name,a);
  //...

并且您忘记将新创建节点的数据成员

next
设置为
NULL

所以只有这一个函数包含三个错误。

注意函数

llinsertaftern
可以将新节点追加到列表的尾部。然而该函数不会改变指针
linkedlist1tail

© www.soinside.com 2019 - 2024. All rights reserved.