Alessandro
Alessandro

Reputation: 1

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

Upvotes: 0

Views: 150

Answers (1)

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

Upvotes: 1

Related Questions