i need to know the code for insert in middle, and this is what i wrote, i dont get any errors so theres some logic error in it, so please help me by giving me the correct code.

void List::insertATMiddle(const string x)
{
	Node* p = new Node;
	p->setData(x);
	p->setNext(NULL);

	Node* q;
	q = head;

	if ((q->getNext() != NULL ) && (q->getData() != x))
	{
		q = q->getNext();
	}
	q->setNext(p);
}

>so please help me by giving me the correct code
We'll never give you free code on this forum, did you read the forum rules? Let me guess...NO!! :)

Some information about linked lists:

Still Couldn't get the logic right :(

Still Couldn't get the logic right :(

What logic can't you get right?

If you cannot understand a single one of the links I posted then you should definitely do something else than C++ :(

ya links does not help. i have coded for insert before, insert after, insert at rear. the only problem i have is insert in between 2 values ... ya sure i'll give up programming and do some thing else.. thank you !

You definitely want to use a loop, not an if statement, since you need to run the list to find the correct spot for insertion. Once you find the right spot, then you can do the insertion. Without seeing code for doing that, I can't say whether setNext() will work or not.

In general what you want to do in this situation is create two pointers. One to search for the node with the value to trigger insertion and one to point to the node with the value that triggers the insertion. Let's say the trigger is "if the value in the current node is greater than or equal to the value in the node to insert" and the insertion is "insert current node immediately to the left of the current node". That means, when all is said and done, the node to insert into the list will end up pointing to the current node in the list and the node which initially pointed to the current node will end up pointing to the node to insert. Pseudocode for this version would look something like this:

node * current = first node of list;
node * previous = NULL;

while (current not equal NULL and current->value less than newNode->value)
    assign current to previous //keep track of the node to the left
    assign current->next to current;  //advance one node to the right

//now that you've found the right spot for insertion
if previous equals NULL  //implying insert to the left of first node
   assign first node to newNode->next;
   assign newNode to first node
else
   assign current to newNode->next
   assign newNode to newNode to previous

You definitely want to use a loop, not an if statement, since you need to run the list to find the correct spot for insertion. Once you find the right spot, then you can do the insertion. Without seeing code for doing that, I can't say whether setNext() will work or not.

In general what you want to do in this situation is create two pointers. One to search for the node with the value to trigger insertion and one to point to the node with the value that triggers the insertion. Let's say the trigger is "if the value in the current node is greater than or equal to the value in the node to insert" and the insertion is "insert current node immediately to the left of the current node". That means, when all is said and done, the node to insert into the list will end up pointing to the current node in the list and the node which initially pointed to the current node will end up pointing to the node to insert. Pseudocode for this version would look something like this:

node * current = first node of list;
node * previous = NULL;

while (current not equal NULL and current->value less than newNode->value)
    assign current to previous //keep track of the node to the left
    assign current->next to current;  //advance one node to the right

//now that you've found the right spot for insertion
if previous equals NULL  //implying insert to the left of first node
   assign first node to newNode->next;
   assign newNode to first node
else
   assign current to newNode->next
   assign newNode to newNode to previous

Thank You so very much ... this helps a lot. i was about to quit :X programming .. thanks for your post :idea:

ya sure i'll give up programming and do some thing else.. thank you !

I hope it's not because of my post :P

I hope it's not because of my post :P

:-/

>ya links does not help.
The Eternally Confuzzled link goes to my website. Could you explain why that tutorial didn't help so that I can improve it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.