User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,800 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 3,817 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: Programming Forums

add function of a liked list class

Join Date: Apr 2006
Location: City of God
Posts: 145
Reputation: orko is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 8
orko orko is offline Offline
Junior Poster

Help add function of a liked list class

  #1  
Oct 31st, 2006
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:

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
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 2:10 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC