#include<iostream>
using namespace std;
template <class T>
struct node
{
T data;
node<T>*link;
};
template<class T>
class nodelink
{
private:
node<T> *head;
public:
nodelink() { head = new node<T>; }
~nodelink();
node<T>* creat();
void show(node<T>*head);
node<T>* reverse(node<T>*head);
};
template<class T>
nodelink<T>::~nodelink()
{
node<T>*p=head;
while (head)
{
p = head;
head = head->link;
delete p;
}
}
template<class T>
node<T>* nodelink<T>::creat()
{
node<T> *p, *temp;
p = head;
int flag = 1, d;
cout << "输入数据" << endl;
while (flag)
{
temp = new node<T>;
cout << "输入数据(以0结束)" << endl;
cin >> d;
if (d != 0)
{
p->data = d;
p->link = temp;
p = temp;
}
else
flag = 0;
}
return head;
}
template<class T>
node<T>* nodelink<T>::reverse(node<T> *head)
{
if (head == NULL || head->link == NULL)
return head;
node<T>*p1, *p2, *P3;
p1 = head;
p2 = head->link;
while (p2)
{
p3 = p2->link;
p2->link = p1;
p1 = p2;
p2 = p3;
}
head->link = NULL;
head = p1;
return head;
}
template<class T>
void nodelink<T>::show(node<T> *head)
{
if (head == NULL)
cout << "链表为空" << endl;
node<T>*p = head;
while (p)
{
cout << p->date << " ";
p = p->link;
}
cout << endl;
}
int main()//测试函数
{
nodelink<int> mylist;
node(int)*p或者node*p报错?为什么?
}
using namespace std;
template <class T>
struct node
{
T data;
node<T>*link;
};
template<class T>
class nodelink
{
private:
node<T> *head;
public:
nodelink() { head = new node<T>; }
~nodelink();
node<T>* creat();
void show(node<T>*head);
node<T>* reverse(node<T>*head);
};
template<class T>
nodelink<T>::~nodelink()
{
node<T>*p=head;
while (head)
{
p = head;
head = head->link;
delete p;
}
}
template<class T>
node<T>* nodelink<T>::creat()
{
node<T> *p, *temp;
p = head;
int flag = 1, d;
cout << "输入数据" << endl;
while (flag)
{
temp = new node<T>;
cout << "输入数据(以0结束)" << endl;
cin >> d;
if (d != 0)
{
p->data = d;
p->link = temp;
p = temp;
}
else
flag = 0;
}
return head;
}
template<class T>
node<T>* nodelink<T>::reverse(node<T> *head)
{
if (head == NULL || head->link == NULL)
return head;
node<T>*p1, *p2, *P3;
p1 = head;
p2 = head->link;
while (p2)
{
p3 = p2->link;
p2->link = p1;
p1 = p2;
p2 = p3;
}
head->link = NULL;
head = p1;
return head;
}
template<class T>
void nodelink<T>::show(node<T> *head)
{
if (head == NULL)
cout << "链表为空" << endl;
node<T>*p = head;
while (p)
{
cout << p->date << " ";
p = p->link;
}
cout << endl;
}
int main()//测试函数
{
nodelink<int> mylist;
node(int)*p或者node*p报错?为什么?
}