Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   LinkedList (http://www.go4expert.com/forums/linkedlist-t16229/)

cyrow 17Feb2009 11:34

LinkedList
 
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.

xpi0t0s 17Feb2009 16:23

Re: LinkedList
 
> 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.


All times are GMT +5.5. The time now is 19:54.