void List::remove_last()
{
	
	if(first == NULL)
	{
		first -> data = ' ';
	}
	else
	{
		
		Node *newnode;
		newnode = new Node;
		newnode = NULL;
		newnode -> data = last -> back -> data;
		last = newnode;
		
	}
}

am I doing something wrong? I'm trying to remove the end of list node.

Dani AI

Generated

Summary and a few practical notes that extend the thread's troubleshooting:

This thread is a classic pointer-management case. 's original attempt accidentally allocated then discarded a pointer (which is a logic error), and flagged that. The real runtime bug turned out to be leaving the list in an inconsistent state after moving the tail: the predecessor node's forward link was not cleared, so the list still referenced a node that had been removed.

Correct removal behavior (high level)

  1. If the list is empty, do nothing.
  2. If there is exactly one node, free it and make both head and tail represent “empty.”
  3. Otherwise, choose the predecessor of the current tail, update that predecessor so it no longer points forward (so the list ends there), free the old tail, and update the tail pointer to the predecessor.

Important gotchas and defensive practices

  • Always check for the empty and single-node cases explicitly before touching neighbors to avoid null dereferences.
  • Update the surviving node’s pointers to reflect the new boundary before deleting the removed node; that keeps traversal code from encountering a dangling pointer.
  • After deletion, avoid accessing the freed node and consider setting any local pointers that referred to it to null to reduce accidental use-after-free.
  • Test with edge cases: empty list, single-node list, two-node list, and multiple identical values.

Removing by value (replying to )

  • Traverse from head until a matching value is found. If none, report “not found.”
  • When removing a found node, handle three situations (head, tail, middle) and update adjacent pointers accordingly.
  • Decide whether to remove only the first match or all matches; both are straightforward but differ in traversal details and complexity (O(n) time).

Modern options: prefer std::list for general use, or add runtime checks and run tools like Valgrind/AddressSanitizer to catch leaks and invalid memory accesses.

Recommended Answers

All 4 Replies

newnode = new Node;
newnode = NULL;

Eerrrr? That's a bit of weird code. You're trying to derefence that pointer on the next line! ;)

newnode = new Node;
newnode = NULL;

Eerrrr? That's a bit of weird code. You're trying to derefence that pointer on the next line! ;)

void List::remove_last()
{
	
	if(first == NULL && last == NULL)
	{}
	else
	{
		
		Node *temp;
		temp = last -> back;
		delete last;
		last = temp;
		
	}
}

how about this code? either way it is not not running right.

nvm figured it out, i forgot to do temp->next = NULL; before i assigned it to last.

what is code of removing if we want user to enter value from the list to remove in doubly linked list?

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.