class function for removing and adding linklist dose not work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
int deleteNode(struct node *h, string name){
struct node *p, *q;
p=h->next;
q=h;
while(p!=NULL){
if(strcmp(p->name, name.c_str())==0){
q->next=p->next;
free(p);
return 1;
}
else {
q=p;
p=p->next;
}
}
return 0;
}
void printLL(struct node *h){
struct node *p;
p=h;
while(p!=NULL){
cout << p->name << ", " << p->age << endl;
p=p->next;
}
}
int addNode(struct node *h, string name, int age){
struct node *p, *q, *r;
p=h->next;
q=h;
r=(struct node *)malloc(sizeof(struct node));
strcpy(r->name, name.c_str());
r->age=age;
while(p!=NULL){
if(strcmp(name.c_str(), p->name)<0){
// add the node
q->next=r;
r->next=p;
return 1;
}
q=p;
p=p->next;
}
q->next=r;
r->next=p;
return 1;
}
|
Topic archived. No new replies allowed.