removing duplicates

This is a discussion on removing duplicates within the C Programming forums, part of the General Programming Boards category; Hello ! I am trying to remove the duplicates strings from a file... i have read the strings from the ...

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    removing duplicates

    Hello !
    I am trying to remove the duplicates strings from a file...
    i have read the strings from the file , it reads successfully and prints it but when I try to delete the duplicates, it doesnt work n give no errors,
    I have no clue what is going wrong....
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #define LINE_LENGTH 10
    struct lnode {
      char *str;              
      struct lnode *next;         
    };
    
    void removeDuplicates(struct lnode* list)
    {
      
      struct lnode* p = list;
      struct lnode* q;
      if(p == NULL)
        return;
      while(p->next != NULL)
        {
          if(p->str == p->next->str)
    	{
    	  q = p->next->next;
    	  free(p->next);
    	  p->next = q;
    	}
        else 
          {
    	p = p->next;
          }
        }
    }
    void  *insert(char *data, struct lnode *list) {
      struct lnode *p;
      p = (struct lnode *)malloc(sizeof(struct lnode));
      p->str = strdup(data);
      p->next = list;
      list = p;
      return list;
    }
    
    void print_list(struct lnode *list) 
    {
      while(list!=NULL)
        {
          printf("%s ", list->str);
          list = list->next;
        }
    } 
    int main ()
    {
      struct lnode *list = NULL;
      FILE *fp;
      int count = 0;
      char names[10];
      
      fp = fopen ("Workbook2.txt","r");
      if(fp ==NULL)
        {
          printf("Error");
        }
      else{
        
        while(fgets(names,10,fp) != NULL)
          {
    	count++;
    	list = insert(names,list);
          }
        removeDuplicates(list);
        print_list(list);
        printf("%d\n", count); 
      }
      
      fclose(fp);  
      return 0;
      
    }
    thanks.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Not gone all the way through but

    >>if(p->str == p->next->str)

    If that is how you are comparing strings lookup strcmp

    http://www.cplusplus.com/reference/c...string/strcmp/

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    thanksssssss a lot!
    I was comparing the integers not strings ....
    U solved my problem....

    thanks!
    Now it runs perfectly.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I was comparing the integers not strings ....
    Not integer. Address.

  5. #5
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    yes, rightly said.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 01:02 PM
  2. Removing Duplicates from an Array
    By SlayerBlade in forum C Programming
    Replies: 4
    Last Post: 10-02-2005, 05:46 PM
  3. Array with at most n duplicates
    By kratz in forum C++ Programming
    Replies: 19
    Last Post: 07-16-2005, 11:46 PM
  4. removing duplicates from a linked list
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2003, 09:27 PM
  5. Eliminating duplicates
    By C-Struggler in forum C Programming
    Replies: 3
    Last Post: 03-23-2003, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21