Homework 10


Due : 9.30am, 21 July

For this homework, you may assume the type definitions used in class today:

struct NodeType {
   int i;
   NodeType *Next;
};

NodeType *Head;

Write a function to delete the tail of a singly linked list. Assume that there is only a head pointer and no tail pointer. Use the following prototype:

void DeleteTail ();

Solution

void DeleteTail ()
{
   if (Head == NULL)
   {
   }
   else if (Head -> Next == NULL)
   {
      delete Head;
      Head = NULL;      
   }
   else
   {
      NodeType *p = Head;
      while (p->Next->Next != NULL)
         p = p->Next;
      delete p->Next;
      p->Next = NULL;
   }
}

Last updated : 21 July 2000 11.17am