Thread: NODE *RemoveDuplicates(NODE *p, NODE *q);

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Question NODE *RemoveDuplicates(NODE *p, NODE *q);

    how to write a function that removes all duplicated notes from a Circular Doubly Linked List.
    <code>NODE *RemoveDuplicates(NODE *p, NODE *q);</code>

    where p points to the first node, and q to the last

    thanks

  2. #2
    Registered User
    Join Date
    Dec 2006
    Posts
    3
    I did it like tis:
    Code:
    NODE* Remove_Duplicates(NODE *p, NODE **q)
    {
    	NODE *tmp, *tmp1;
    NODE *n;
    
    /* checking for Empty list */
    
    	if (p==NULL)
    		return;
    
    	tmp1=p;
    	while (p!=tmp1->prev)
    		{
    		n=p->next;
    		while (n!=tmp1)
    		{
    			if (n->data==p->data)
    			{
    				n->prev->next=n->next;
    				n->next->prev=n->prev;
    				tmp=n;
    				n=n->next;
    				free(tmp);
    			}
    			else n=n->next;
    		}
    		p=p->next;
    		}
    	return tmp1;
    }
    Is there any better way?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't seem to use the q parameter.

    What do you mean 'better'?
    Better named variables might help understanding it perhaps.
    n, tmp, tmp1 hardly make for good reading.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well as salem said u are not using Q any where there. Instead u can use the pointer to pointer on P so that can avoid returning pointer P in you function

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM