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

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?

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

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

This article has been dead for over six months. Start a new discussion instead.