#include
using namespace std;
template
class Node{
private:
Nodenext;
public:
T data;
Node(const T& data=0,Node *next=0); //构造函数
void insertAfter(Node *p); //在此节点后插入一个节点
Node deleteAfter(); //删除该节点后一个节点
Node* nextNode(); //获得下一个节点
const Node* nextNode() const; //获得下一个节点 (常)
};
template
Node::Node(const T& data,Nodenext/=0*/):data(data),next(next) {}
template
void Node::insertAfter(Node *p){
p->nextNode()=this->next;
this->next=p;
}
template
Node* Node::deleteAfter(){
if(next==0)
return 0;
Node *temp=next->nextNode();
delete next;
next=temp;
}
template
Node* Node::nextNode(){
return next;
}
template
const Node* Node::nextNode() const{
return next;
}
template
class LinkedList{
private:
Node *front,*rear;
Node *prePtr,*currPtr;
int size;
int position;
public:
LinkedList();
LinkedList(const LinkedList& L);
~LinkedList();
LinkedList& operator =(const LinkedList& L);
int getSize() const;
bool isEmpty() const;
void reset(int pos=0); //初始化游标位置
void next(); //使游标移到下一个节点
bool isendOfList() const; //判断游标是否到了链尾
int currentPosition() const; //返回当前游标位置
void insertFront(const T& item); //在表头插入
void insertRear(const T& item); //在表尾插入
void insertAt(const T& item); //在指定位置之前插入
void insertAfter(const T& item); //在指定位置之后插入
T deleteFront(); //删除头节点
void deleteCurrent(); //删除当前节点
T& data();
const T& data() const;
void clear();
};
template
LinkedList::LinkedList():front(0),rear(0),prePtr(0),currPtr(0){}
template
LinkedList::LinkedList(const LinkedList& L){
this->clear();
size=L.size;
L.reset(1);
front=new Node;
front=0;
rear=front;
for(int i=0;i
this->insertFront(L.currPtr->data);
if(L.currPtr!=L.rear)
L.next();
}
} //未完成
template
LinkedList::~LinkedList(){
this->clear();
}
template
int LinkedList::getSize() const{
return size;
}
template
bool LinkedList::isEmpty() const{
return size==0;
}
template
void LinkedList::reset(int pos){
position=pos;
if(position=1)
currPtr=front;
for(int i=1;i
currPtr=front->nextNode();
if(i==1)
prePtr=front;
else
prePtr=prePtr->nextNode();
}
}
template
void LinkedList::next(){
currPtr=currPtr->nextNode();
if(this->position=1) //当前指针在表头时
prePtr=front;
prePtr=prePtr->nextNode();
}
template
bool LinkedList::isendOfList() const{
return currPtr==rear;
}
template
int LinkedList::currentPosition() const{
return prePtr-front+1;
}
template
void LinkedList::insertFront(const T& item){
if(isEmpty())
{
Node *temp=new Node;
front=temp;
rear=temp;
}
else
{
Node *temp=new Node;
temp->data=item;
temp->nextNode()=front;
front=temp;
}
}
template
void LinkedList::insertRear(const T& item){
if(isEmpty)
{
Node *temp=new Node;
front=temp;
rear=temp;
}
else
{
Node *temp=new Node;
temp->data=item;
rear->nextNode()=temp;
}
}
template
void LinkedList::insertAt(const T& item){
Node *temp=new Node;
temp->data=item;
temp->nextNode()=currPtr;
prePtr->nextNode()=temp;
}
template
void LinkedList::insertAfter(const T& item){
Node *temp=new Node;
temp->data=item;
temp->nextNode()=currPtr->nextNode().nextNode();
currPtr->nextNode()=temp;
}
template
T LinkedList::deleteFront(){
} //待定
template
void LinkedList::deleteCurrent(){
currPtr=currPtr->nextNode();
delete prePtr->nextNode();
prePtr->nextNode()=currPtr;
}
template
T& LinkedList::data(){
return currPtr->data;
}
template
const T& LinkedList::data()const{
return currPtr->data;
}
template
void LinkedList::clear(){
prePtr=0;
currPtr=0;
while(front!=rear){
Node *temp=front;
front=front->nextNode();
delete temp;
}
delete front;
front=0;
rear=0;
}
int main(){
LinkedListlist;
for(int i=0;i<10;i++){
int item;
cin>>item;
list.insertFront(item);
}
cout<<"List:";
list.reset();
while(!list.isendOfList()){
cout<<list.data()<<" ";
}
return 0;
}