typedef NodeType* NodePtr;
struct NodeType
{
ComponentType component;
NodePtr Link;
};
// method that construct an empty list
HybridList::HybridList()
// constructor
// postcondition: head == NULL
{
head == NULL;
}
// a method that test for empty list
bool HybridList::IsEmpty() const
{
return (head == NULL); // return true if head == NULL.
}
// printing the list
void HybidList::Print() const
{
//component member have been output
NodePtr currentPtr = head;
while(currentPtr != NULL)
{
cout<< currentPtr->component <<endl;
currentPtr = currentPtr->Link;
}
}
// inserting item function
void HybridList::Insert( ComponentType item)
{
// precondition: component members of the list are in ascending order
//postcondition: new Node containing item is in proper place
NodePtr newNodePtr = new NodeType; // set-up node to be inserted
newNodePtr->component = item;
NodePtr prevPtr = NULL;
NodePtr currentPtr = head;
while(currentPtr != NULL && item > currentPtr->component)
{
prevPtr = currentPtr;
currentPtr = currentPtr->Link; // searching for proper place for item
}
// insert item
newNodePtr->Link = currentPtr;
if (prevPtr == NULL) head = newNodePtr;
else
prevPtr->Link = newNodePtr;
delete newNodePtr;
}
@dnambembe, that's a list and he wanted a binary tree. Though you shouldn't do people's homework anyway.
@ahmadijaz, we're not going to do it for you. Write a basic binary tree node class, see how you can iterate through it (hint: use a recursive function). Then ask what exactly is your problem and we'll help.