简体   繁体   English

如何添加到链表C结尾

[英]How to add to end in linked list C

I wrote a simple program that should add some numbers inside a linked-list, then remove a specific one and print the elements at the end but it doesn't add all the elements, it simply adds the first one and the last one.我写了一个简单的程序,它应该在链表中添加一些数字,然后删除一个特定的数字并在最后打印元素,但它不会添加所有元素,它只是添加第一个和最后一个。 And also the delete function gives me problems because it doesn't delete anything but it keep's saying that the list's empty.而且删除功能给我带来了问题,因为它不会删除任何内容,但它一直说列表为空。 Thanks for the help everybody谢谢大家的帮助

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
  int info;
  struct node *next;
}node;
typedef node *list;
list L;

list del(list L, int elem)
{
  node *current, *temp;
  current=L;

  if(L==NULL)
  {
    printf("there's nothign to delete\n");
  }
  if((L->next!=NULL)&&(L->info==elem))
  {
    current=L->next;
    free(L);
    L=current;
    return(L);
  }
  else
  {
    temp=del(L->next, elem);
    return(L);
  }
}

list addEnd(list L, float elem)
{
  node *punt, *curr, *new_node;
  if(L==NULL)
  {
    punt=malloc(sizeof(node));
    punt->info=elem;
    punt->next=NULL;
    L=punt;
    return(L);
  }
  else
  {
    punt=L;
    curr=L;

    while(punt->next!=NULL)
    {
      curr=punt;
      punt=punt->next;
    }
    new_node=malloc(sizeof(node));
    new_node->info=elem;
    new_node->next=NULL;
    curr->next=new_node;
    return(L);
  }
}

void print(list L)
{
  node *current = L;
  if(current==NULL)
  {
        printf("list's empty");
  }
  while (current != NULL)
  {
   printf("%d\n", current->info);
   current = current->next;
  }
}

int main()
{
  int n1,n2,n3,n4;
  n1=1;
  n2=10;
  n3=23;
  n4=45;

  L=addEnd(L, n1);
  L=addEnd(L, n2);
  L=addEnd(L, n3);
  L=addEnd(L, n4);
  L=del(L,23);

  print(L);
  return(0);
}

Try this correction, you have two mistake one in delete and another one in add:试试这个更正,你有两个错误,一个是删除,另一个是添加:

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
  int info;
  struct node *next;
}node;
typedef node *list;
list L;

list del(list L, int elem)
{
  // no need to use recursive function
  node *current, *temp, *prev;
  temp = L;
  prev = L;

  if(temp==NULL)
  {
    printf("there's nothign to delete\n");
  }
  while((temp!=NULL))
  {
   if(temp->info==elem)
   {
      current=temp->next;
      if(L == temp)
          L = current;
      else
          prev->next=current;
      free(temp);
      return(L);
    }
    // you should keep the previous node
    prev = temp;
    temp = temp->next;
  }
  return(L);
}

list addEnd(list L, int elem)
{
  node *punt/*, *curr*/, *new_node;
  if(L==NULL)
  {
    punt=malloc(sizeof(node));
    punt->info=elem;
    punt->next=NULL;
    L=punt;
    return(L);
  }
  else
  {
    punt=L;
    // curr=L;

    while(punt->next!=NULL)
    {
      //curr=punt; 
      //no needed
      punt=punt->next;
    }
    new_node=malloc(sizeof(node));
    new_node->info=elem;
    new_node->next=NULL;
    // this instruction is wrong curr->next=new_node; it erase the last value
    punt->next=new_node;
    return(L);
  }
}

void print(list L)
{
  node *current = L;
  if(current==NULL)
  {
        printf("list's empty");
  }
  while (current != NULL)
  {
   printf("%d\n", current->info);
   current = current->next;
  }
}

int main()
{
  int n1,n2,n3,n4;
  n1=1;
  n2=10;
  n3=23;
  n4=45;

  L=addEnd(L, n1);
  L=addEnd(L, n2);
  L=addEnd(L, n3);
  L=addEnd(L, n4);
  L=del(L,23);

  print(L);
  return(0);
}

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

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