am trying to delete an element from my list but am having a problem..i have managed to position my pointers well but av failed to to the actual element. i.e user enters a number to be deleted from the list..here's the bit of the deleting code that i have been working on

void delete_record(nodePtr& head, nodePtr& current)
{
	
	nodePtr previous=NULL;

	edit_record(head,current);

	string name;
	
	cout<<"Enter the name you wish to delete:";

	cin >> name;

	cout<< endl;

	if(current->link==name)
	{
		
		previous=head;

		current=head->link;

		while(current->name!=name)
		{
			previous=current;

			current=current->link;
		}
		current=current->link;

		previous=previous->link;

		delete previous;

	}

}
while(current->name!=name)
{
	previous=current;
                current=current->link;
}
current=current->link;
previous=previous->link;
delete previous;
}

Can I see your list struct?

I think you are losing a pointer in your code though. Before you delete the specified node (the one you want to delete), you need to make sure that the previous node links to the node after the specified node. That is, you need to grab the specified node, then set previous->link equal to the node after the current (specified) node. Then you can delete the specified node...Make sure you don't have any pointers to it though.

Also, in your while loop, what if you never find the name of the node? (errors...) You should account for if this happens (i.e. check if your list becomes NULL and then exit)

Also, I don't think your if statement makes sense...In fact, I don't think you need it...

struct list
{
	string name;

	int number;

	list* link;
};

typedef list* nodePtr;

i tried to think it through but cant seem to figure it out..i was using the if to test some parts of it..sorry

i have done some modifications to the code...i can now delete everything in the list apart from the second last item..here is an example of the list...

tom
dick
john
harry //inserted later by the user

the program is able to delete all the elements apart from john..the user is prompted to insert another name "harry" but maybe there is something wrong with the way john is stored after the insertion but i cant seem to trace it..I tried doing some little testing and found out that the 2nd last item is still pointing to NULL and i dont know why...here is the insertion function and the delete one..any help is appreciated

void add_entry(nodePtr& head, nodePtr& current) //adds name to end of the list
{
	nodePtr temp;
	new_list(head,current);
	string new_name;
	int num;

	cout<<"Enter name ";
	cin>>new_name;
	cout<<"Enter the number: ";
	cin>>num;

	current=head;

	while(current->link!=NULL)
	{
		current=current->link;

	}
	temp=new list;
	temp->name=new_name;
	temp->number=num;
	temp->link=NULL;
	current->link=temp;
	current=temp;

	cout<<"Element Added."<<endl<<endl;	
}

void delete_record(nodePtr& head, nodePtr& current) //deletes specified name
{
	
	nodePtr previous,target=NULL,tmp;

	edit_record(head,current);

	string name_in;
	
	cout<<"Enter the name you wish to delete:";

	cin >> name_in;

	cout<< endl;
	
	previous=head;

	current=head;

	while(current->name!=name_in && current->link!=NULL)
	{
		previous=current;
		current=current->link;
	}
	if(current->name==name_in)
	{
		if(current->link==NULL)
		{
			tmp = current;
			current=previous;
			current->link=NULL;
			delete tmp;
		}
		else
		{
			previous->link=current;

			current=current->link;

			target=previous->link;

			previous->link=current;
		
			delete target;
		}
	}

	else
	{
		cout<<"Name cannot be found."<<endl;
	}

}

i tried to think it through but cant seem to figure it out..i was using the if to test some parts of it..sorry

Sorry I didn't realize this hadn't been solved. I don't have time atm (I'll be out and about all day...), but I will sit down later tonight and help you out, unless someone else has fixed up your problem by then.

actually i figured it out...after a long review i realised a simple mistake on my side.i inserted the new node by the user at the end instead of the head of the list so in deleting the list i was referencing something else...thanx for your time anyways

This question has already been answered. Start a new discussion instead.