Midterm Practice Exercises

printReverse
Write the definition of the function printReverse that displays the content of the list in reverse order.

		template <class Type>
		void linkedListType<Type>::printReverse () const
		{
			linkedListType<Type> tmp;

			nodeType<Type>* current = first;
			while (current != NULL)
			{
				nodeType<Type>* ptr = new nodeType<Type>;
				tmp.insertFirst (current->info);
				current = current->link;
			}

			tmp.print ();
		}
		

maximum
Write the definition of the function maximum that returns a pointer to the node that has the maximum info value.

		template <class Type>
		const nodeType<Type>* linkedListType<Type>::maximum () const
		{
			if (isEmptyList ())
				return NULL;

			nodeType<Type>* ptrMax = first;
			Type infoMax = first->info;

			nodeType<Type>* current = first->link;
			while (current != NULL)
			{
				if (infoMax < current->info)
				{
					infoMax = current->info;
					ptrMax = current;
				}
				current = current->link;
			}

			return ptrMax;
		}
		

swapLF
Write the definition of the function swapLF that swaps the info value of first and last nodes.

		template <class Type>
		void linkedListType<Type>::swapLF ()
		{
			Type tmp = first->info;
			first->info = last->info;
			last->info = tmp;
		}
		

Equal
Write the definition of the function Equal that takes a linked list object, return true of both list are equal, false otherwise.

		template <class Type>
		bool linkedListType<Type>::Equal (const linkedListType<Type>& otherlist) const
		{
			if (count != otherlist.count)
				return false;

			nodeType<Type>* current, * current2;
			current = first;
			current2 = otherlist.first;

			while (current != NULL && current2 != NULL)
			{
				if (current->info != current2->info)
					return false;

				current = current->link;
				current2 = current2->link;
			}

			return true;
		}