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