943,547 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 489
  • C++ RSS
Jan 13th, 2009
0

a problem with linked list

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bita is offline Offline
2 posts
since Jan 2009
Jan 13th, 2009
0

Re: a problem with linked list

read the code-posting-rules first
Reputation Points: 36
Solved Threads: 5
Newbie Poster
da penguin is offline Offline
18 posts
since Dec 2008
Jan 14th, 2009
0

Re: a problem with linked list

i actually fail to understand your logic for deletion

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jan 14th, 2009
0

Re: a problem with linked list

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bita is offline Offline
2 posts
since Jan 2009
Jan 14th, 2009
0

Re: a problem with linked list

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
Reputation Points: 113
Solved Threads: 20
Junior Poster
Laiq Ahmed is offline Offline
147 posts
since Jun 2006
Jan 14th, 2009
0

Re: a problem with linked list

Since he doesn't seed rand at any point he is always going to get the same number back from it
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: HW help. Intro to Visual C++.
Next Thread in C++ Forum Timeline: Writting a "command promt" or "Shell".. dealing with directories !





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC