954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

a problem with linked list

Hi

I have a problem with linked list,i want to delete a node.q is a node that i want to delete and p is the node before q,i signed my problem in the code below could you plz tell me whats my problem,because it doesnt work?(it's a Circularly-linked list)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void Delete(struct node*,int);

struct node{

    char item[10];
    int NUM;
    struct node*link;

};

int n=0;

void main(){

    int i=0,num=1;

    struct node*head=NULL;

    struct node*p=NULL;

    printf("\nHow many item do you want to enter?\n");

    scanf("%d",&n);

    for(i=1;i<=n;i++)
    {
    if(head==NULL)
    head=p=(struct node*)malloc(sizeof(struct node));

    else
    {
    p->link=(struct node*)malloc(sizeof(struct node));

            p=p->link;

        }

        p->link=NULL;

        printf("\nEnter item:\n");

        scanf("%s",p->item);

        p->NUM=num++;

        }

            num--;

        p->link=head;

    for(p=head;;p=p->link){

    printf("NUM = %d , item = %s\n",p->NUM,p->item);

                    num=num-1;

                    if(num==0)
                        break;
            }

        Delete(head,num);

        
}

void Delete(struct node*p,int num)
{

    int count=0;

    struct node*head,*q;

    head=p;

    num=n;

    count=n;
    
    while(num!=1)
    {

    int a=1 + int (10 * rand() / ( RAND_MAX + 1 ) );

    printf("a = %d\n",a);

            for(p=head;;p=p->link)
            {
            if(p->link->NUM==a)//here myproblem
                                    it search for 'a'
                                     through the list and 
                                 if find it,then delete it
            {

                q=p->link->link;

                p->link=q->link;

                q->link=NULL;
                        
                free(q);

                num--;

                    break;

                    }

                count=count-1;

                if(count==-1)
                {
                    count=n;

                    break;

                }
        
            }

            }
      for(p=head;;p=p->link){

     printf("NUM = %d , item = %s\n",p->NUM,p->item);

                    num=num-1;

                    if(num==0)
                        break;
            }
}


Thanx

Bita

bita
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

read the code-posting-rules first

da penguin
Newbie Poster
18 posts since Dec 2008
Reputation Points: 36
Solved Threads: 5
 

i actually fail to understand your logic for deletion

q=p->link->link;
p->link=q->link;
q->link=NULL;

free(q);


1> why are you checking for p->link->NUM and not p->NUM directly?

2> when you find it, you assign q to p->link->link and then free(q), shouldn't you be freeing p->link instead?

Agni
Practically a Master Poster
655 posts since Dec 2007
Reputation Points: 431
Solved Threads: 116
 

1>answer:becuase in this way 'p' is to the node before the node we want to delete.

2>yes,thanks i think you are right q should assign to p->link

bita
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

Why you search for the Random Value? is it your requirement?

int a=1 + int (10 * rand() / ( RAND_MAX + 1 ) );

this would result in a random number, theoratically every time you are creating a new integer to search (i.e. for each loop iteration theoretically a would be different.

why are these assignments? num=n;
this would override the value of num passed?
hmmm, few problems? tell me what actually you want to achieve with this link list? we'll provide you the solution

Laiq Ahmed
Junior Poster
148 posts since Jun 2006
Reputation Points: 113
Solved Threads: 20
 

Since he doesn't seed rand at any point he is always going to get the same number back from it

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You