List.h
#pragma once
#include <iostream>
using namespace std;
template<class Type> class List; //前置声明list类
template<class Type> class ListIterator; //前置声明迭代器类
template<class Type>
class ListNode
{
friend class List<Type>; //友元类
friend class ListIterator<Type>; //友元类
private:
Type data;
ListNode* next;
ListNode(Type); //私有构造函数,只能由友元类List调用
};
template<class Type>
class List
{
friend class ListIterator<Type>; //友元类
public:
List() { first = 0; };
void Insert(Type);
bool Delete(Type);
bool Invert();
bool Concatenate(List<Type>);
void Show(); //测试使用,显示链表数据
private:
ListNode<Type> *first;
};
//链表迭代器
template<class Type>
class ListIterator
{
public:
ListIterator(const List<Type>& itList):list(itList), cur(itList.first){};
bool NotNULL();
bool NextNotNULL();
Type* First();
Type* Next();
private:
const List<Type> &list;
ListNode<Type>* cur;
};
/**********************链表迭代器函数实现**********************/
template<class Type>
bool ListIterator<Type>::NotNULL()
{
if(cur)
return true;
else
return false;
}
template<class Type>
bool ListIterator<Type>::NextNotNULL()
{
if(cur && cur->next)
return true;
else
return false;
}
template<class Type>
Type* ListIterator<Type>::First()
{
if(list.first)
return &list.first->data;//&为取地址 返回地址
else
return 0;
}
template<class Type>
Type* ListIterator<Type>::Next()
{
if(cur)
{
cur = cur->next;
return &cur->data; //&为取地址 返回地址
}
else
return 0;
}
/*******************ListNode的成员函数实现**********************/
template<class Type>
ListNode<Type>::ListNode(Type element)
{
this->data = element;
this->next = 0;
}
/*******************List的成员函数实现**********************/
template<class Type>
void List<Type>::Insert(Type k)
{
ListNode<Type>* newnode = new ListNode<Type>(k);
newnode->next = first;
first = newnode;
}
template<class Type>
void List<Type>::Show()
{
ListNode<Type> *cur;
for(cur = this->first; cur; cur = cur->next)
{
cout << cur->data;
if(cur->next)
cout << " -> " ;
}
cout << endl;
}
template<class Type>
bool List<Type>::Delete(Type k)
{
ListNode<Type>* pre = 0; //前一个
ListNode<Type>* cur = first;
while(cur&&cur->data!=k)
{
pre = cur;
cur = cur->next;
}
if(cur) //判断cur是否为空
{
if(pre) //判断是否为第一个元素
pre->next = cur->next;
else
first = first->next;
delete cur;
return 1;
}
else
{
return -1;
}
}
template<class Type>
bool List<Type>::Invert()
{
ListNode<Type> *p = first, *q = 0;
while(p)
{
ListNode<Type> *r = q;
q = p;
p = p->next;
q->next = r;
}
first = q;
}
template<class Type>
bool List<Type>::Concatenate(List<Type> listb)
{
if(!first)
{
first = listb.first;
return 1;
}
if(listb.first)
{
ListNode<Type> *p = first;
while(p->next) //找到第一个链表的最后一个
{
p = p->next;
}
p->next = listb.first;
}
}
main.h
#include <iostream>
#include <list> //stl中的链表
#include "List.h" //自己设计的链表
using namespace std;
int main()
{
cout << "测试" << endl;
List<int> intList;
List<int> intList1;
list<int> stlList; //stl中的链表
cout << "标准C++中链表和迭代器" << endl;
stlList.push_front(5);
stlList.push_front(10);
stlList.push_front(15);
stlList.push_front(25);
for(list<int>::iterator it = stlList.begin(); it!=stlList.end();it++)
{
cout << *it << " ";
}
cout << endl;
cout<< "自己设计的链表和迭代器" << endl;
intList.Insert(5);
intList.Insert(10);
intList.Insert(15);
intList.Insert(25);
ListIterator<int> it(intList);
if(it.NotNULL())
{
cout << *it.First() ;
while(it.NextNotNULL())
{
cout << "->" << *it.Next();
}
cout << endl;
}
intList1.Insert(35);
intList1.Insert(45);
intList1.Insert(55);
intList1.Insert(66);
//intList.Show();
//intList1.Show();
//intList.Concatenate(intList1);
//intList.Invert();
//intList.Delete(10);
//intList.Show();
return 0;
}