-1

I'm making a little program that should allow me to insert n numbers into an array. Subsequently these numbers must be inserted in a list, avoiding to insert duplicates. I think I'm close to the solution but I don't understand where I'm wrong. My idea is to insert the first element directly in the list without doing any checks, from the second element onwards, I scroll through the list of elements inserted and check if the number I am about to insert is already present, if yes I discard it otherwise I add a node to the list. Problem is, I've made some mistakes that I can't find. Can you help me?

    //This is the node
    struct node
    {
      int dato;         
      struct node *next; 
    };
    
int main()
{
 int i,j=0,n, control=0;
 int V[dmax];
  struct node *p, *prec, *head, *f;
  do
  {
  printf("type dim of array\n");
  scanf("%d",&n); 
  }
  while(n<1 || n>dmax);
  loadArray(V,n); 

 //______________________
  for(i=0; i<n; i++){
    control = 0, j=0;
    if(i==0){
        p=malloc(sizeof(struct node));
            head = p;
    p->dato=V[i];
    }else{
      while(j<i){
        f=head;
      if(V[i]==f->dato){
        control = 1;
        j++;
       //maybe break;?
      }else{
          f = f->next;
          j++;
        }
      }//while                         
    }//else
    if ( control == 1)
      printf("%d discarded\n", i);
    else{
      p=malloc(sizeof(struct node));
      p->dato=V[i];
      prec->next = p;
      prec = p;
    }
    p->next=NULL;

  }//for
  p=head;
    printf("print list\n");
    while(p != NULL){

    printf("%d\n",p->dato);
    p = p->next;
    }

}


   
Zeld
  • 33
  • 5
  • 3
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. It should also specify the exact input necessary to reproduce the issue, the exact output and the desired output. – Andreas Wenzel May 07 '22 at 19:06
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 07 '22 at 19:07
  • 1
    One observation: you haven't written enough functions in your code. You should not put everything in `main()`. You should use functions to do specific tasks. You appear to have a function `loadArray()` which isn't shown; the value of `dmax` is not shown either. Please read about how to create an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)) — the same idea by a different name. – Jonathan Leffler May 07 '22 at 19:08
  • You should have functions to print the array, to print the list, to insert a (non-duplicate) value in the list. And you should use them in `main()`. I regard writing functions to print the key data structures in a program as an invaluable debugging tool — and it often helps identify how the code should be accessed and may even alter the design. – Jonathan Leffler May 07 '22 at 19:10

0 Answers0