再救急哟,链表编不过去
template<typename T>
class Node
{
public:
T data;
Node *next;
Node(T);
Node();
};
template<typename T>
Node<T>::Node(T a):data(a),next(NULL){}
template<typename T>
Node<T>::Node():next(NULL){}
template<typename ElementType>
class Chain
{
private:
class Node
{
public:
ElementType data;
Node *next;
Node();
Node(ElementType);
};
typedef Node *NodePointer;
public:
/****Function Members****/
Chain();
~Chain();
Chain(const Chain<ElementType> & origChain);
bool empty() const;
void insert(ElementType,ElementType);
void erase(ElementType);
void replace(ElementType,ElementType);
void display() const;
int length();
ElementType PriorNode(ElementType);
ElementType NextNode(ElementType);
private:
/*******data member*******/
int mySize; //the size of the currenting list
NodePointer first; //the point pointing the first Node
};
template<typename ElementType>
Chain<ElementType>::Chain()
/*Creator
post:an empty chain has been created*/
{
first=0;
mySize=0;
}
template<typename ElementType>
Chain<ElementType>::~Chain()
/*destructor
pre:the chain exits
post:the chain has been destructed & the memory has been recycled*/
{
Node *ptr=first->next;
while(ptr!=0)
{
Node *temp=ptr;
ptr=ptr->next;
delete temp;
}
}
template<typename ElementType>
Chain<ElementType>::Chain(const Chain<ElementType> & origList)
/*copy creator
pre:origList exits
post:the origList has been copyed into a new chain*/
{
mySize=origList.mySize;
if(mySize==0) first=0;
else
{
Node<ElementType> *p,*q;
q=origList.first->next;
p=first->next=new Node;
p->data=c.first->data;
while((q=q->next)!=0)
{
p=p->next=new Node<ElementType>();
p->data=q->data;
}
}
}
template<typename ElementType>
bool Chain<ElementType>::empty() const
/*check whether the chain is empty
pre:the chain exits
post:if the chain is empty,return true;else return false*/
{
return mySize==0;
}
template<typename ElementType>
void Chain<ElementType>::insert(ElementType item,ElementType preItem)
/*insert a node into the chain,the node should be after the preditem
pre:the chain exits;preditem exits
post:the node has been inserted into the right position*/
{
if(mySize==0)
first->next=new Node(item);
else
{
Node *ptr=first->next;
if(preItem==NULL)
{
Node* newptr=new Node(item);
first->next=newptr;
newptr->next=ptr;
}else
{
while(ptr->data!=preItem)
{
ptr=ptr->next;
}
if(ptr->data==preItem)
{
Node *newptr=new Node(item);
newptr->next=ptr->next;
ptr->next=newptr;
}else
cout<<"There's no node contains the item."<<endl;
}
}
}
template<typename ElementType>
void Chain<ElementType>::erase(ElementType item)
/*delete a node contains the appointed data
pre:the chain exits;there's a node contains the appointed data
post:if the precondition is satisfied,the appointed data will be deleted;else the chain will not be changed*/
{
if(mySize==0)
{
cout<<"The chain is empty.Delete failed."<<endl;
exit(1);
}
Node* ptr=first->next;
while(ptr->data!=item)
{
Node *tempPtr=ptr;
ptr=ptr->next;
}
if(ptr->data==first->data)
{
first->next=ptr->next;
delete ptr;
}else if(ptr->data==item)
{
tempPtr=ptr->next;
delete ptr;
}
else cout<<"The chain has no node contains value item."<<endl;
}
template<typename ElementType>
void Chain<ElementType>::replace(ElementType InItem,ElementType OutItem)
/*replace the data of a Node
pre:the chain exits;the OutItem exits in the chain
post:the OutItem has been replace by the InItem*/
{
Node *ptr=first->next;
while(ptr->data!=OutItem && ptr=!0)
ptr=ptr->next;
if(ptr=0)
cout<<"There's no node contains the appointed item."<<endl;
else
ptr->data=InItem;
}
template<typename ElementType>
void Chain<ElementType>::display() const
/*cout every Node's data
pre:the chain exits
post:if the chain is empty,notice the customer;else cout each Node's data*/
{
Node *ptr=first->next;
if(mySize==0)
cout<<"The chain is empty,no elements can be output.";
else
{
while(ptr->next!=NULL)
{
cout<<ptr->data;
}
}
}
template<typename ElementType>
int Chain<ElementType>::length()
/*return the size of the chain
pre:the chain exists
post:mySize has been returned*/
{
return mySize;
}
template<typename ElementType>
ElementType Chain<ElementType>::PriorNode(ElementType item)
/*return the value of PriorNode of an appointed Node
pre:the chain exists;the item is a node's data of the chain
post:the PriorNode's data has been returned*/
{
Node *ptr=first->next;
Node *temp;
while(ptr->data!=item)
{
temp=ptr;
ptr=ptr->next;
}
if(ptr->data==item)
return temp->data;
else cout<<"The node containing the appointed data doesn't exists in the chain."<<end;
}
template<typename ElementType>
ElementType Chain<ElementType>::NextNode(ElementType item)
/*return the value of NextNode of an appointed Node
pre:the chain exists;the item is a node's data of the chain
post:the NextNode's data has been returned*/
{
if(mySize==0)
cout<<"The chain is empty.We get nothing."<<endl;
else
{
Node *ptr=first->next;
while(ptr->data!=item)
{
ptr=ptr->next;
}
if(ptr->data==item)
{
Node *NextPtr=ptr->next;
return NextPtr->data;
}else
{
cout<<"The node containing the appointed data doesn't exists in the chain."<<endl;
exit(1);
}
}
}
-----------------------------------------------------------
Chain.obj : error LNK2001: unresolved external symbol "public: __thiscall Chain<int>::Node::Node(int)" (??0Node@?$Chain@H@@QAE@H@Z)
出现这个错误,嗯,又解决不了了。明儿就要交了,哪位大侠能帮我看下。。。
首前一个NULL,尾后一个NULL非常混乱。
[解决办法]
文件没贴全.
[解决办法]
class Chain
{
private:
class Node
{
public:
ElementType data;
Node *next;
Node();
Node(ElementType);
};
typedef Node *NodePointer;
这个class Node你貌似已经定义过了啊,是不是redifine了?
[解决办法]
看错误提示,问题是由于Chain类的内部类Node的带int参数的构造函数仅仅声明但没有实现。我没有仔细看你的代码,但是发现在开头定义了Node类,为什么不在Chain中使用,而又重新定义一个同名的内部类呢?开头的Node的声明和定义貌似是完整的。
[解决办法]
楼主在以下代码里申明了内部类Node.但是并没有给出其构造函数的定义.
template <typename ElementType > class Chain { private: class Node { public: ElementType data; Node *next; Node(); Node(ElementType); };//其它代码省略}