Homework 9


Due : 9.30am, 20 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 find the maximum and minimum values stored in an unsorted linked list. Use the following prototype:

void MaxMin ( int &Max, int &Min );

Solution

void MaxMin ( int &Max, int &Min )
{
   if (Head == NULL)
   {
      Max = 1;
      Min = 2;
      return;
   }
   Max = Head->i;
   Min = Head->i;
   NodeType *p = Head->Next;
   while (p != NULL)
   {
      if (p->i < Min)
         Min = p->i;
      if (p->i > Max)
         Max = p->i;
      p = p->Next;
   }
}

Last updated : 20 July 2000 11.41am