Was wondering if soemone could point me in the right direction with the insertafter function in a double linked list. The function looks like this.

const LinkedList::Iterator LinkedList::InsertAfter(const Iterator& Iter, const ItemType& Item)
	{
		nodePtr nNode = new Node(Item, NULL, NULL);

		// test if allocation was successful
		if (nNode != NULL) 
		{
		// was successful

		// add the node to the linked list
		
			nNode->next = tail->next;
		tail->next = NewNode(ItemType(), NULL, NULL);

		// set the tail to the new node
		
		}
		
		return(nNode);
		
	} // Inserts a new node after the one pointed to by Iter.  Returns
	 // a reference to this new node.

I'm not looking for an answer. I want to solve it myself, but I've been stuck rewriting this code for a couple of hours. When it compiles it just returns the tail. This is my closest attempt at getting it. The tail is suppose to be a dummy node.

new operator causes an exception if it cannot allocate the memory, so your check for NULL will not do anything. You need to put that line in a try/catch block. As an alternative, you can use nothrow (link) and leave that check in your code.

I would think you need to add the new node to the Iter parameter, not tail. (*Iter).next = nNode;

Edited 7 Years Ago by Ancient Dragon: n/a

I've changed some of the code.

const LinkedList::Iterator LinkedList::InsertAfter(const Iterator& Iter, const ItemType& Item)
	{
		nodePtr nNode = NewNode(Item, Iter.node, Iter.node);
		
		if (nNode != NULL) 
		{
			tail = NewNode(ItemType(), tail, tail->next);
			cout << nNode->item << endl;
		}
		return(nNode);
		
	} // Inserts a new node after the one pointed to by Iter.  Returns
	 // a reference to this new node.

The cout was to test to see if it was getting the right items. Which it is, but it seems to be put the Items together and ignore the endl. I'm having problems understanding on the iterator should work. I understand that i need to add the nNode pass the iterator, but whenever i try it crashes the program. I also cant use *iter.node->next, because it says which takes a right hand operand of type nodePtr.
I would just like some guidance on this. Thank you.

Edited 7 Years Ago by Flapjack: n/a

>>but whenever i try it crashes the program. I also cant use *iter.node->next, because it says which takes a right hand operand of type nodePtr.

That's not what I posted -- re-read my post, it requires the parentheses ()

I've done what you said with the (), the above statement short handed what i did. Gives me the same error.
I've been reading the couple of books i have on c++, and only one of them have anything about linked list iterators.

Edited 7 Years Ago by Flapjack: n/a

This should fix that compiler error

const LinkedList::Iterator LinkedList::InsertAfter(Iterator& Iter, const ItemType& Item)
   {
      nodePtr nNode = new Node(Item, NULL, NULL);
      Iter.node->next= nNode;
      return(nNode);


   }

Edited 7 Years Ago by Ancient Dragon: n/a

This should fix that compiler error

const LinkedList::Iterator LinkedList::InsertAfter(Iterator& Iter, const ItemType& Item)
   {
      nodePtr nNode = new Node(Item, NULL, NULL);
      Iter.node->next= nNode;
      return(nNode);


   }

I cant take const out. The teacher wants us to us his function and develop our code around it.

I cant take const out. The teacher wants us to us his function and develop our code around it.

Well there is const_cast but generally, you shouldn't have to do this.

const LinkedList::Iterator LinkedList::InsertAfter(const Iterator& Iter, const ItemType& Item)
	{
		nodePtr nNode = new Node(Item, tail->previous, tail->next);
		tail->previous = nNode;
		tail->next = nNode;
		tail = nNode;
		return(nNode);
	} // Inserts a new node after the one pointed to by Iter.  Returns
	 // a reference to this new node.

This will spit it out in the driver. The thing is i think it puts them into one node because when i go further into the driver it will say badptr. I took some code from my single link list and try to adjust it for this. I have to delete the first item in the linked list and then spit the rest out. It crashes. :(

Edited 7 Years Ago by Flapjack: n/a

I didn't link my head and tail. So that messed up part of it.

LinkedList::LinkedList()
	{
		//Linking head and tail
		head = NewNode(ItemType(), NULL, NULL);
		tail = NewNode(ItemType(), head, NULL);
		head->next = tail;
	} // Sets the head and tail to a dummy node.
const LinkedList::Iterator LinkedList::InsertBefore(const Iterator& Iter, const ItemType& Item)
	{
		Iterator temp = Iter;
		nodePtr nNode;
		//First case- Empty List
		if(Iter.node == NULL)
		{
		nNode = NewNode(Item, head, tail);	//Linking to the head and tail
		head->next = nNode; //Linking head to nNode
		tail->previous = nNode; //Linking tail to nNode
		}
		else
		{
		//Second Case- List has a one or more data nodes
		nNode = NewNode(Item, Iter.node->previous, Iter.node);	//Linking nNode to the previous and next node
		Iter.node->previous->next = nNode;	//Linking pervious node to the nNode
		Iter.node->previous = nNode;	//Linking the node Iter is at to the nNode
		}
		temp.node = nNode;
		return(temp);
		
	} // Inserts a new node before the one pointed to by Iter.  Returns
	 //  a reference to this new node.

	const LinkedList::Iterator LinkedList::InsertAfter(const Iterator& Iter, const ItemType& Item)
	{
		Iterator temp = Iter;
		nodePtr nNode;
		//First case- Empty List
		if(Iter.node == NULL)
		{
		nNode = NewNode(Item, head, tail); 	//Linking to the head and tail
		head->next = nNode;	//Linking head to nNode
		tail->previous = nNode;	//Linking tail to nNode
		}
		//Second Case- List has a one or more data nodes
		else
		{
		nNode = NewNode(Item, Iter.node, Iter.node->next);	//Linking nNode to the previous and next node	
		Iter.node->next->previous = nNode;	//Linking pervious node to the nNode
		Iter.node->next = nNode;	//Linking the node Iter is at to the nNode
		}

		temp.node = nNode;
		return(temp);
	} // Inserts a new node after the one pointed to by Iter.  Returns
	 // a reference to this new node.

This code now works.

Edited 7 Years Ago by Flapjack: n/a

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