•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 374,023 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,766 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 774 | Replies: 1
![]() |
•
•
Join Date: Apr 2006
Location: City of God
Posts: 145
Reputation:
Rep Power: 3
Solved Threads: 8
Hello, I usually don't complain too much. But this time I am getting mad. I just wrote a linked list, where
1. If the value was not present before in the list, it will be added. In this case, there is a capacity. If the new value increases the list size more than capacity, then the eldest one will be deleted.
2. If the value was present in the list, it will be added on the top of the list, and the old value have to be removed.
I am done with all of the parts, but I can't remove the old value.... my code goes below:
so far i can see, the problem is in this code:
1. If the value was not present before in the list, it will be added. In this case, there is a capacity. If the new value increases the list size more than capacity, then the eldest one will be deleted.
2. If the value was present in the list, it will be added on the top of the list, and the old value have to be removed.
I am done with all of the parts, but I can't remove the old value.... my code goes below:
void RList::add(int newNum) {
lNode *h, *t, *p=NULL, *c;
bool run=false;;
if(!numberExists(newNum)) {
if(!(size<capacity)) {
throw invalid_argument ("RedialList::add() Not enough place");
}
size++;
}
else run=true;
if(head==NULL) {
h=new lNode(newNum, NULL);
head=h;
return;
}
h= new lNode(newNum, head);
head=h;
if(numberExists(newNum) && run) {
c=head;
for(p=head->next;;p=p->next;) {
if(p->phnNo==newNum){
c=p->next;
delete c;
return;
}
}
}
}so far i can see, the problem is in this code:
if(numberExists(newNum) && run) {
c=head;
for(p=head->next;;p=p->next;) {
if(p->phnNo==newNum){
c=p->next;
delete c;
return;
}
}
} Last edited by orko : Oct 31st, 2006 at 5:50 pm.
A Perfect World
•
•
Join Date: Jul 2005
Posts: 1,066
Reputation:
Rep Power: 8
Solved Threads: 138
1) What's the terminating condition of the for loop?
2) If I have APA and I want to remove the first A then I can store the head in a temp node, change head to to head->next, then delete the temp node.
However, if I have APCP and I want to remove the first P toget ACP I have to disconnect the first P from C and connect the A to the C without changing head and then I can delete P. In theory, if I use a searching node called current, then if current->next->value == P, then current->next can be assigned to a temp node, current->next->next can be assigned to current->next , temp node->next can be nulled and then temp node can be deleted. With any luck it might work. Alternatively, I could keep track of current node and prior node where current node is same as prior node next, as long as the list is longer than a single node. Then current node->next can be assigned to prior node->next and current node can be deleted. That should work, too. Good luck.
2) If I have APA and I want to remove the first A then I can store the head in a temp node, change head to to head->next, then delete the temp node.
However, if I have APCP and I want to remove the first P toget ACP I have to disconnect the first P from C and connect the A to the C without changing head and then I can delete P. In theory, if I use a searching node called current, then if current->next->value == P, then current->next can be assigned to a temp node, current->next->next can be assigned to current->next , temp node->next can be nulled and then temp node can be deleted. With any luck it might work. Alternatively, I could keep track of current node and prior node where current node is same as prior node next, as long as the list is longer than a single node. Then current node->next can be assigned to prior node->next and current node can be deleted. That should work, too. Good luck.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- File association won't add program to open list (Windows NT / 2000 / XP / 2003)
- Timer, tickcount, ticks? (C#)
- Abstract Class member function problem (C++)
- How do I edit my 'add/remove programmes' list? (Windows NT / 2000 / XP / 2003)
- Add/Remove Programs List Gone! (Windows 9x / Me)
Other Threads in the C Forum
- Previous Thread: fgetc help
- Next Thread: tcp client code



Linear Mode