题目是写一个链表,在前面插入十个数.
题目是 写一个链表,在前面插入十个数. 谁能帮帮我 谢谢
#include<iostream>
using namespace std;
template<typename T>class List;
template<typename T>class Node{
public:
friend class List<T>;
Node(T d,Node<T> *n = NULL):data(d),next(n){}
private:
T data;
Node<T> *next;
};
template<typename T>class List{
public:
List():head(NULL){};
void InsertNode(T x);
void Display();
private:
Node<T> *head;
};
template<typename T>void List<T>::InsertNode(T x)
{
Node<T> *p = new Node<T>(x);
if( head == NULL )
{
head = p;
}
else
{
p->next = head;
head = p;
}
}
template<typename T>void List<T>::Display()
{
Node<T> *p = head;
while(p)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
void main()
{
List<int>L;
for( int i = 0; i < 10; ++i)
L.InsertNode(i);
L.Display();
}
上机运行去