| | |
a problem with linked list
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
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)
Thanx
Bita
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)
c Syntax (Toggle Plain Text)
#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
Last edited by Ancient Dragon; Jan 13th, 2009 at 2:53 pm. Reason: add code tags
i actually fail to understand your logic for deletion
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?
C++ Syntax (Toggle Plain Text)
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?
Last edited by Agni; Jan 14th, 2009 at 4:27 am.
thanks
-chandra
-chandra
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
Why you search for the Random Value? is it your requirement?
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?
hmmm, few problems? tell me what actually you want to achieve with this link list? we'll provide you the solution
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
![]() |
Similar Threads
- Problem with linked list in multi-threaded C program (C)
- problem about linked list. (C++)
- comparing and doing something between two Linked List (C)
- linked list (C)
- *strcpy and linked list (C#)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
Other Threads in the C++ Forum
- Previous Thread: HW help. Intro to Visual C++.
- Next Thread: Writting a "command promt" or "Shell".. dealing with directories !
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





