a problem with linked list

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 2
Reputation: bita is an unknown quantity at this point 
Solved Threads: 0
bita bita is offline Offline
Newbie Poster

a problem with linked list

 
0
  #1
Jan 13th, 2009
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)

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. void Delete(struct node*,int);
  6.  
  7. struct node{
  8.  
  9. char item[10];
  10. int NUM;
  11. struct node*link;
  12.  
  13. };
  14.  
  15. int n=0;
  16.  
  17. void main(){
  18.  
  19. int i=0,num=1;
  20.  
  21. struct node*head=NULL;
  22.  
  23. struct node*p=NULL;
  24.  
  25. printf("\nHow many item do you want to enter?\n");
  26.  
  27. scanf("%d",&n);
  28.  
  29. for(i=1;i<=n;i++)
  30. {
  31. if(head==NULL)
  32. head=p=(struct node*)malloc(sizeof(struct node));
  33.  
  34. else
  35. {
  36. p->link=(struct node*)malloc(sizeof(struct node));
  37.  
  38. p=p->link;
  39.  
  40. }
  41.  
  42. p->link=NULL;
  43.  
  44. printf("\nEnter item:\n");
  45.  
  46. scanf("%s",p->item);
  47.  
  48. p->NUM=num++;
  49.  
  50. }
  51.  
  52. num--;
  53.  
  54. p->link=head;
  55.  
  56. for(p=head;;p=p->link){
  57.  
  58. printf("NUM = %d , item = %s\n",p->NUM,p->item);
  59.  
  60. num=num-1;
  61.  
  62. if(num==0)
  63. break;
  64. }
  65.  
  66. Delete(head,num);
  67.  
  68.  
  69. }
  70.  
  71. void Delete(struct node*p,int num)
  72. {
  73.  
  74. int count=0;
  75.  
  76. struct node*head,*q;
  77.  
  78. head=p;
  79.  
  80. num=n;
  81.  
  82. count=n;
  83.  
  84. while(num!=1)
  85. {
  86.  
  87. int a=1 + int (10 * rand() / ( RAND_MAX + 1 ) );
  88.  
  89. printf("a = %d\n",a);
  90.  
  91. for(p=head;;p=p->link)
  92. {
  93. if(p->link->NUM==a)//here myproblem
  94. it search for 'a'
  95. through the list and
  96. if find it,then delete it
  97. {
  98.  
  99. q=p->link->link;
  100.  
  101. p->link=q->link;
  102.  
  103. q->link=NULL;
  104.  
  105. free(q);
  106.  
  107. num--;
  108.  
  109. break;
  110.  
  111. }
  112.  
  113. count=count-1;
  114.  
  115. if(count==-1)
  116. {
  117. count=n;
  118.  
  119. break;
  120.  
  121. }
  122.  
  123. }
  124.  
  125. }
  126. for(p=head;;p=p->link){
  127.  
  128. printf("NUM = %d , item = %s\n",p->NUM,p->item);
  129.  
  130. num=num-1;
  131.  
  132. if(num==0)
  133. break;
  134. }
  135. }

Thanx

Bita
Last edited by Ancient Dragon; Jan 13th, 2009 at 2:53 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 18
Reputation: da penguin is an unknown quantity at this point 
Solved Threads: 5
da penguin's Avatar
da penguin da penguin is offline Offline
Newbie Poster

Re: a problem with linked list

 
0
  #2
Jan 13th, 2009
read the code-posting-rules first
No, ma'am, we are musicians.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: a problem with linked list

 
0
  #3
Jan 14th, 2009
i actually fail to understand your logic for deletion

  1.  
  2. q=p->link->link;
  3. p->link=q->link;
  4. q->link=NULL;
  5.  
  6. 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?
Last edited by Agni; Jan 14th, 2009 at 4:27 am.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: bita is an unknown quantity at this point 
Solved Threads: 0
bita bita is offline Offline
Newbie Poster

Re: a problem with linked list

 
0
  #4
Jan 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 147
Reputation: Laiq Ahmed will become famous soon enough Laiq Ahmed will become famous soon enough 
Solved Threads: 20
Laiq Ahmed Laiq Ahmed is offline Offline
Junior Poster

Re: a problem with linked list

 
0
  #5
Jan 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: a problem with linked list

 
0
  #6
Jan 14th, 2009
Since he doesn't seed rand at any point he is always going to get the same number back from it
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC