I'm using Visual C++ 6.0,I think my code is true,It can compile and run,but when I debug,it has this error....
"NodeList.h"
Code:
#ifndef NODELIST_H
#define NODELIST_H
template <class Entry>
struct Node{
Node();
Entry entry;
Node<Entry> * next;
Node(const Entry &item, Node<Entry> * p =NULL);
};
#endif
"NodeList.cpp"
Code:
#ifndef NODELIST_CPP
#define NODELIST_CPP
#include "NodeList.h"
template <class Entry>
Node<Entry>::Node(){
next = NULL;
}
template <class Entry>
Node<Entry>::Node(const Entry & item, Node<Entry> * p){
entry = item;
next = p;
}
#endif
"List.cpp"
Code:
#ifndef LIST_CPP
#define LIST_CPP
#include "List.h"
template <class Entry>
List<Entry>::List(){
count = 0;
pchinh = NULL;
pcurrent = NULL;
CurrentPosition = 0;
}
template <class Entry>
List<Entry>::~List(){
while(!Empty())
Remove();
}
template <class Entry>
List<Entry>::List(const List<Entry> & list0){
Node<Entry> *psao, * pduyet = list0.pchinh;
if (pduyet==NULL) pchinh = NULL;
else
{
pchinh = psao = new Node<Entry> (pduyet->entry);
while (pduyet->next != NULL)
{
pduyet = pduyet->next;
psao->next = new Node<Entry>(pduyet->entry);
psao = psao-> next;
}
}
return sucess;
}
template <class Entry>
List<Entry>& List<Entry>::operator=(const List<Entry> & list0){
Node <Entry> *ptam, *psao, *pduyet = list0.pchinh;
count = 1;
if (pduyet==NULL) ptam = NULL;
else
{
ptam = psao = new Node<Entry> (pduyet->entry);
while(pduyet->next!= NULL)
{
pduyet = pduyet->next;
psao->next = new Node<Entry>(pduyet->entry);
psao = psao -> next;
count++;
}
}
while(!Empty())
Remove();
pchinh = ptam;
return (*this);
}
template <class Entry>
ErrorCode List<Entry>::Insert(const Entry&item, int position){
Node <Entry> *ptruoc, *psau, *newnode = new Node<Entry>(item);
if (newnode == NULL)
{
cout<<"Da het bo nho!Khong cap phat duoc nua.";
return overflow;
}
if (position < 0 || position > count)
{
cout<<"Vi tri chen khong hop le!";
return errorrange;
}
if (position == 0)
{
if (pchinh ==NULL)
{
pchinh = newnode;
count++;
return sucess;
}
else
{
newnode -> next = pchinh;
pchinh = newnode;
count++;
return sucess;
}
}
else
ptruoc = SetPosition(position-1);
psau = ptruoc->next;
newnode->next = psau;
ptruoc -> next = newnode;
count++;
return sucess;
}
template <class Entry>
ErrorCode List<Entry>::Remove(int position){
Node<Entry> * psau, *ptruoc, *ptam = pchinh;
if (position < 0 || position >= count)
{
cout<<"Vi tri xoa khong hop le!";
return errorrange;
}
if (position == 0)
if (pchinh == NULL)
{
cout<<"Chua co phan tu nao de xoa!";
return errorcode;
}
else
{
pchinh = pchinh->next;
count--;
delete ptam;
return sucess;
}
else
ptruoc = SetPosition(position-1);
psau = ptruoc->next;
ptam = psau;
psau = psau -> next;
delete ptam;
count--;
return sucess;
}
template <class Entry>
ErrorCode List<Entry>::Replace(const Entry &item, int position){
Node<Entry> * psau, *ptruoc;
if (position < 0 || position > count)
{
cout<<"Vi tri thay the khong hop le!";
return errorrange;
}
if (position == 0)
if (pchinh==NULL)
{
cout<<"Chua co phan tu nao de thay the!";
return errorcode;
}
else
{
pchinh->entry = item;
return sucess;
}
else
ptruoc = SetPosition(position-1);
psau = ptruoc->next;
psau->entry = item;
return sucess;
}
template <class Entry>
ErrorCode List<Entry>::Retrieve(Entry &item,int position){
Node<Entry> * psau, *ptruoc;
if (position < 0 || position > count)
{
cout<<"Vi tri tim khong hop le!";
return errorrange;
}
if (position == 0){
item = pchinh->entry;
cout<<item<<endl;
return sucess;
}
else
{
ptruoc = SetPosition(position-1);
psau = ptruoc -> next;
}
item = psau->entry;
cout<<item<<endl;
return sucess;
}
template <class Entry>
Node<Entry> * List<Entry>::SetPosition(int position) const{
Node<Entry> * ptam = pchinh;
if (CurrentPosition > =position)
{
for (int i =0;i<position;i++)
ptam = ptam->next;
pcurrent = ptam;
CurrentPosition = position;
return ptam;
}
else
for (int i = CurrentPosition;i<position;i++)
pcurrent = pcurrent -> next;
CurrentPosition = position;
return pcurrent;
}
template <class Entry>
int List<Entry>::Size()const {
return count;
}
template <class Entry>
bool List<Entry>::Empty() const {
if (count == 0)
return true;
else
return true;
}
template <class Entry>
bool List<Entry>::Full() const{
NodeList<Entry> * location;
try
{
location = new NodeList<Entry>;
delete location;
return false;
}
catch(std::bad_alloc exception)
{
return true;
}
}
template <class Entry>
void List<Entry>::Clear() const {
int i;
while (count!=0)
Remove(i);
}
template <class Entry>
void List<Entry>::Traverse(void (*duyet)(Entry & item)){
Node<Entry> * ptam= pchinh;
for (int i =0;i<count;i++)
{
(*duyet)(ptam->entry);
ptam = ptam->next;
}
}
#endif
"List.h"
Code:
#ifndef LIST_H
#define List_H
#include "NodeList.h"
enum ErrorCode{sucess, underflow, overflow, errorrange, errorcode};
template <class Entry>
class List{
public:
List();
~List();
List(const List<Entry> &);
List<Entry> &operator=(const List<Entry> &);
int Size() const;
void Clear() const;
bool Empty() const ;
bool Full() const;
ErrorCode Insert(const Entry &item = 0, int position = 0);
ErrorCode Remove(int position = 0);
ErrorCode Replace(const Entry &item = 0 , int position = 0 );
ErrorCode Retrieve(Entry &item , int position = 0);
void Traverse(void (*duyet)(Entry&x));
private:
int count;
Node<Entry> * pchinh;
mutable int CurrentPosition;
mutable Node<Entry> * pcurrent;
protected:
Node<Entry> * SetPosition(int position) const;
};
#endif
"UseList.cpp"
Code:
#include "stdafx.h"
#include "List.cpp"
#include "NodeList.cpp"
#include <iostream>
#include <conio.h>
using namespace std;
void main(){
List<int> list1;
List<int> list2;
int i;
list1.Insert(2);
list1.Insert(4,1);
list1.Insert(7,2);
list1.Insert(12,3);
list1.Insert(14,4);
list1.Insert(9,5);
//list1.Remove(4);
list1.Replace(5);
list1.Replace(9,3);
list2 = list1;
getch();
}
Example when I debug Remove function I have arrow:
Code:
template <class Entry>
ErrorCode List<Entry>::Remove(int position){
Node<Entry> * psau, *ptruoc, *ptam = pchinh;
arrow point here although don't have code this line
After that arrow is here if (position < 0 || position >= count)
{
cout<<"Vi tri xoa khong hop le!";
return errorrange;
}
After that Arrow points here(don't have code this line)
if (position == 0)
if (pchinh == NULL)
{
cout<<"Chua co phan tu nao de xoa!";
return errorcode;
}
else
{
pchinh = pchinh->next;
count--;
delete ptam;
return sucess;
}
else
ptruoc = SetPosition(position-1);
psau = ptruoc->next;
ptam = psau;
psau = psau -> next;
delete ptam;
count--;
return sucess;
}
So Value of parameters change and I can't manage them....