#ifndef NODETYPE_H
#define NODETYPE_H
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
#endif // NODETYPE_H
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "nodeType.h"
#include <iostream>
template <class Type>
class linkedList
{
public:
linkedList();
~linkedList();
linkedList(const linkedList<Type>& other);
linkedList& operator=(const linkedList<Type>& other);
void destroyList();
void initializeList();
void print() const;
virtual bool search(const Type& searchItem) const = 0;
virtual void insertFirst(const Type& newItem) = 0;
virtual void insertLast(const Type& newItem) = 0;
virtual void deleteNode(const Type& nodetodel) = 0;
protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private:
void copyList(const linkedList<Type>& other);
};
#endif // LINKEDLIST_H
#include "linkedList.h"
template <class Type>
linkedList<Type>::linkedList() //ctor
{
first = NULL;
last = NULL;
count = 0;
}
template <class Type>
linkedList<Type>::~linkedList() //dtor
{
destroyList();
}
template <class Type>
linkedList<Type>::linkedList(const linkedList<Type>& other)
{
first = NULL;
copyList(other);
}
template <class Type>
linkedList<Type>& linkedList<Type>::operator=(const linkedList<Type>& rhs)
{
if (this != &rhs)
copyList(rhs);
return *this;
}
template <class Type>
void linkedList<Type>::destroyList()
{
nodeType<Type> *current;
while(first!=NULL)
{
current = first;
first = first->link;
delete current;
}
last = NULL;
count = 0;
}
template <class Type>
void linkedList<Type>::initializeList()
{
destroyList();
}
template <class Type>
void linkedList<Type>::print() const
{
nodeType<Type> *current;
current = first;
while(current != NULL)
{
std::cout << current->info;
current = current->link;
}
}
template <class Type>
void linkedList<Type>::copyList(const linkedList<Type>& other)
{
nodeType<Type> *current;
nodeType<Type> *newNode;
if(first != NULL)
destroyList();
if(other.first == NULL)
initializeList();
else
{
current = other.first;
count = other.count;
first = new nodeType<Type>;
first->info = current->info;
first->link = NULL;
last = first;
current = current->link;
while(current != NULL)
{
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
#ifndef ORDEREDLIST_H
#define ORDEREDLIST_H
#include "linkedList.h"
template <class Type>
class OrderedList : public linkedList<Type>
{
public:
bool search(Type& item);
void insert(Type& item);
void deleteNode(Type& item);
protected:
private:
};
#endif // ORDEREDLIST_H
#include "OrderedList.h"
template<class Type>
bool OrderedList<Type>::search(Type& item)
{
bool found = false;
nodeType<Type> *current;
current = first;
while(current != NULL && !found)
{
if(current->info >= item)
found = true;
else
current = current->link;
}
if(found)
found = (current->info == item);
return found;
}
template<class Type>
void OrderedList<Type>::insert(Type& item)
{
nodeType<Type> *current; //pointer to traverse the list
nodeType<Type> *trailCurrent; //pointer just before current
nodeType<Type> *newNode;
bool found;
newNode = new nodeType<Type>; //create the node
newNode->info = item; //store newItem in the node
newNode->link = NULL; //set the link field of the node
//to NULL
if (first == NULL) //Case 1
{
first = newNode;
last = newNode;
count++;
}
else
{
current = first;
found = false;
while (current != NULL && !found) //search the list
if (current->info >= newItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == first) //Case 2
{
newNode->link = first;
first = newNode;
count++;
}
else //Case 3
{
trailCurrent->link = newNode;
newNode->link = current;
if (current == NULL)
last = newNode;
count++;
}
}
}
La classe OrderedList dovrebbe ereditare la classe linkedList ma quando provo a fare il build il debugger riporta
D:\programmic++\linked_list\OrderedList.cpp|36|error: 'first' was not declared in this scope|
e così via per tutte le variabili membro dichiarate in linkedList. Cosa sbaglio?