LinkedList

Discussion in 'C' started by cyrow, Feb 17, 2009.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct node{
    	int num;
    	struct node *next;
    }Node, *NodePtr;
    
    
    main()
    {
    
    	NodePtr isDeleted(NodePtr, int);
    	NodePtr setDifference(NodePtr, NodePtr);
    	NodePtr makeNode(int);
    	int isMember(NodePtr, int);
    	void printDif(NodePtr);
    
    
    //generation of list 1
    	int n;
    	NodePtr top, np, last;
    
    	 top = NULL;
    	if(scanf("%d", &n)!=1)n=0;
    	while(n!=0)
    	{
    		np = makeNode(n);
    		if(top ==NULL)top = np;
    		else last->next = np;
    		last = np;
    		if(scanf("%d", &n)!=1)n=0;
    	}//end while
    
    
    //generation of list 2
    	int m;
    	NodePtr to, newp,las;
    	to = NULL;
    
    	if(scanf("%d", &m)!=1)m=0;
    	while(m!=0)
    	{
    		newp = makeNode(m);
    		if(to ==NULL)to = newp;
    		else las->next = newp;
    		las = newp;
    		if(scanf("%d", &m)!=1)m=0;
    	}//end while
    
    
    	NodePtr lover = setDifference(top, to);
    	printDif(lover);
    
    
    }//end main
    
    
    
    NodePtr makeNode(int n)
    {
    	NodePtr np = (NodePtr)malloc(sizeof(Node));
    	np->num = n;
    	np->next = NULL;
    	return np;
    }//end makeNode function
    
    
    
    
    int isMember(NodePtr np, int z)
    {
    	while(np!=NULL && z!= np->num)
    	{
    		np = np->next;
    
    	}
    
    	if(np==NULL){
    		return 0;
    	}else{
    		return np->num;
    	}
    }
    
    
    
    NodePtr isDeleted(NodePtr np, int d)
    {
    	NodePtr prev,  curr, temp;
    	prev = NULL;
    	curr = np;
    
    	while(curr!= NULL && d !=curr->num){
    		prev = curr;
    		curr = curr->next;
    	}
    
    	if(curr->num==d)
    	{
    		temp = curr;
    		prev->next = curr->next;
    		printf("\nNode deleted:");
    		free(temp);
    	}
    return np;
    
    }//end isDelete
    
    
    
    NodePtr setDifference(NodePtr top, NodePtr to)
    {
    	int test;
    	while(to!=NULL){
    		int result = isMember(top, to->num);
    		if(result!=100000){
    			top = isDeleted(top,to->num);
    
      }
    to = to->next;
    }//end setDifference
    	return top;
    }
    
    
    
    void printDif(NodePtr np){
    	while(np!=NULL)
    	{
    		printf("\nThis is the print for set difference    %d\n",np->num);
    		np = np->next;
    	}//end while
    }
    
    I am havingsome problems. What I am trying to do is find the differece between 2 sets. The problem I beleive is with the delete function. The delete funtions works fine when 1 set of data is given, but when I enter both sets of data it hands up. I cannot figure why. Help.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > if(scanf("%d", &n)!=1)n=0;
    > }//end while

    So you'll only be able to make nodes containing the value 1. Is that intentional?

    What input do you give and what output do you get?

    Code:
    to = to->next;
    }//end setDifference
    	return top;
    }
    
    Hmm. Bit of a formatting problem there.

    If you suspect isDeleted(), try stepping through it on paper with a sample list and a few examples of nodes to delete. Make sure you check the behaviour of deletion from the top and end as well as in the middle.

    Also try using sensible names. It makes debugging a lot easier. Not only is the code then fairly self-documenting, it also means that newcomers to your code won't struggle trying to figure out wtf is going on.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice