DELETING A NODE FROM THE END OF A LIST

HI EVERYONE.

I'M TRYING TO DELETE FROM THE END OF A LINKLIST
Actualy I delete the last node but the problem is that I lost the end of the list, in other words my list leaved of pointing to NULL.
I reaaly don't know how to fixed, (it was more easy to delete from head)

anw if someone can help me I will really really really appreaciate.
THANKS.

[code]
void linkedstack::pop()
{

if(head==NULL){
cout<<"The linklist is empty"<<endl;
return;}
else
{
node *current=head;

while(current->next!= NULL)
{

current=current->next;

}



node* save=current;
current=NULL;
delete save;
size--;
Last edited on
You should set the next pointer of the new last node to null. What you are doing now is that you delete the second last node without repoint any next pointer. You while loop will not work if the list contains 1 element because current->next will be null so current->next->next is not valid.
oh that is so true I modified it right now, actually my code should stop when current->next is equal to NULL then, point SAVE to CURRENT, delete save that it shold be the last node and then put current equal to NULL but it doesn't work!!!

Someone can tell me why please!
Hello,

If you write a primative linked list, it may become more clear

in .h file,

create your linked list typedef

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef struct _mylist {
   struct _myList *Next;
   int    Foo;
} mylist;

// add two methods and three mylist declarations to your class

class TForm1 : public TForm
{
__published:	// IDE-managed Components
private:	// User declarations
  void CreateList(TObject* Sender);          // Declare method to create list
  void DeleteLastNode(TObject* Sender);      // And one to delete last node
public:		// User declarations
  mylist *List;                              // Define the list
  mylist *CurrentNode;                       // And a node pointer
  mylist *SaveNode;                          // And a place to hold last node
  __fastcall TForm1(TComponent* Owner);
};



// In the CPP module:

// First add a few nodes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void TForm1::CreateList(TObject* Sender)
{
   int AddFiveNodes = 5;
   // Load a couple of nodes
   CurrentNode = NULL;

   while(AddFiveNodes-- != 0) {
      if(CurrentNode)
         CurrentNode = CurrentNode->Next = new mylist[sizeof(mylist)];
      else
         CurrentNode = List = new mylist[sizeof(mylist)];
      CurrentNode->Next = NULL;
      // Just for chuckles
      CurrentNode->Foo = AddFiveNodes;
   }
}


// Now delete last node
1
2
3
4
5
6
7
8
9
10
11
12
13
void TForm1::DeleteLastNode(TObject* Sender)
{

   // Now, to delete the last node:
   CurrentNode = List;
   // First move to end of linked list
   while(CurrentNode->Next) {            // Exit loop when Next is NULL
      SaveNode = CurrentNode;            // Save CurrentNode, if next points somewhere
      CurrentNode = CurrentNode->Next;   // Move to next node
   }
   delete CurrentNode;                   // Delete last node
   SaveNode ->Next = NULL;          // Clear Next pointer (SaveNode will point to next to last node).
}
Topic archived. No new replies allowed.