一个关于链表头插入的问题
#include <iostream.h>
template <class T>
class Node
{
private:
Node <T> *next;
public:
T data;
Node <T> (const T& item,Node <T> *ptrnext=NULL);
//表头插入
void InsertFront(Node <T> *head,const T &item);
Node <T> *NextNode(void) const;
Node <T> *GetNode(const T& item,Node <T> *ptrnext=NULL);
};
template<class T>
Node<T>::Node(const T &item,Node<T> *ptrnext):data(item),next(ptrnext)
{
}
template<class T>
Node<T>* Node<T>::NextNode() const
{
return next;
}
template<class T>
Node<T>* Node<T>::GetNode(const T &item,Node<T> *ptrnext)
{
Node<T> *newNode;
newNode=new Node<T>(item,ptrnext);
if(newNode==NULL)
{
cout<<"error"<<endl;
exit(1);
}
return newNode;
}
template<class T>
//此为头插入
void Node<T>::InsertFront(Node <T> *head,const T &item)
{
head=GetNode(item,head);
}
template<class T>
void Node<T>::PrintList(Node <T> *head)
{
Node <T> *curPtr=head;
while(curPtr!=NULL)
{
cout<<curPtr->data<<endl;
curPtr=curPtr->NextNode();
}
}
void main()
{
/*我想在表头插入若干数据,然后输出一下看看效果,但是会出请帮我看看,谢谢!!!!*/
Node <int> *head=NULL;
for(int i=0;i<10;i++)
head->InsertFront(head,i);
cout<<head->NextNode()<<endl;
cout<<head->NextNode()->data<<endl;
}