I have a problem on getting the nth list item( function: DataType get(int n) const ),though it may seem easy.
here is my code..

#ifndef ORDERED_LIST
#define ORDERED_LIST

#include <fstream>
using namespace std;

template <class DataType>
class OrderedList
{
public:

	OrderedList();

~OrderedList();

void clear();

DataType get(int n) const;

void insert(const DataType & dataVal);

int size();

void showAscending();



private:

	class Node;
	typedef Node * NodePointer;
class Node
	{
	public:

		DataType	 data; // element storage
		NodePointer  prev; // pointer to previous node
		NodePointer  next; // pointer to next node

		Node(const DataType & dataValue, 
			NodePointer back = 0, NodePointer fwd = 0) 
		
		{
			data = dataValue;
			prev = 0;
			next = 0;
		};

	}; 
NodePointer first, last; // pointers to first and last nodes
	int         numNodes;    // number of existing nodes

}; 

template <class DataType>
OrderedList<DataType>::OrderedList(): first(0), last(0), numNodes(0) {}

template <class DataType>
OrderedList<DataType>::~OrderedList()
{
	NodePointer nPtr = first;

	while (nPtr != 0)
	{
		first = nPtr->next;
		delete nPtr;
		nPtr = first;
	}
}

template <class DataType>
void OrderedList<DataType>::clear()
{
	while (size() > 0) remove(1);
}

////..............Get method.......................//////

template <class DataType>
DataType OrderedList<DataType>::get(int n) const
{
	
}

I have a problem on this method..i am confused what do i need to return??
is that
return numNodes;
or
first node or last node
or data value..

Note:The list values are in a double linked list. The list positions are
numbered starting at 1 and the list values are in ascending order (the value
at position 2 is greater than or equal to the value at position 1, the value
at position 3 is greater than or equal to the value at position 2, etc.)

Any help would be highly appreciate..

>i am confused what do i need to return??
It returns DataType. DataType is the type of the data that the list holds, so you'd return the data at the Nth node. Using your notation:

template <class DataType>
DataType OrderedList<DataType>::get(int n) const
{
  NodePointer nPtr = first;

  // Find the nth node

  return nPtr->data;
}

What is the data at the nth node?
so,


NodePointer nptr = first;
return nptr->data;

is all i need.

>What is the data at the nth node?
How should I know? It's your list.

>is all i need.
Well, between those two statements you actually need to find the Nth node. :icon_rolleyes:

This article has been dead for over six months. Start a new discussion instead.