2 the icer The_Icer 于 2016.09.08 13:56 提问

一个有关链表类的写法的问题

我写了个链表类,但是报错,想请教一下。以下是代码。
//node.h
#ifndef NODE_H
#define NODE_H
using namespace std;
template
class Node{
public:
Node(const T &i,Nodep=0);
void insert(Node*p);
Node*deleteafter();
Node*nextNode();
const Node*nextNode()const;
T data;
private:
Node*next;
};
template
Node::Node(const T&i,Node*p):data(i),next(p){}
template
void Node::insert(Node*p){
p->next=this->next;
this->next=p;
}
template
Node
Node::deleteafter(){
Nodeptr=next;
if(next==0){
return 0;
}
else{
next=next->next;
}
return ptr;
}
template
Node
Node::nextNode(){
return next;
}
template
const Node* Node::nextNode()const{return next;}
#endif
//LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "node.h"
template
class LinkedList{
private:
Nodefront,*rear;
Node*prevptr,*currptr;
int size;
int position;
Node*newNode(const T& item, Node*ptrNext=0);
void freeNode(Node*P);
void copy(const LinkedList & L);
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 endofList()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();
};
#endif
//LinkedList.cpp
#include "LinkedList.h"
#include
using namespace std;
template
LinkedList::LinkedList(){
front=NULL;
rear=NULL;}
template
LinkedList::LinkedList(const LinkedList&L):size(L.size),position(L.position){
front=new Node;
*front=
(L.front);
rear=new Node;
rear=(L.rear);
prevptr=new Node;
prevptr=(L.prevptr);
currptr=new Node;
currptr=(L.currptr);
}
template
LinkedList::~LinkedList(){clear();}
template
LinkedList& LinkedList::operator=(const LinkedList&L){
size=L.size;
position=L.position;
front=new Node;
front=(L.front);
rear=new Node;
rear=(L.rear);
prevptr=new Node;
prevptr=(L.prevptr);
currptr=new Node;
currptr=(L.currptr);
return *this;
}
template
int LinkedList::getSize()const{return size;}
template
bool LinkedList::isEmpty()const{
if(getSize()==0)
return true;
else
return false;
}
template
void LinkedList::reset(int pos){position=pos;}
template
void LinkedList::next(){
currptr=currptr->nextNode();
prevptr=prevptr->nextNode();
position++;
}
template
bool LinkedList::endofList()const{
if(position==size)
return true;
else
return false;
}
template
int LinkedList::currentPosition()const{return position;}
template
void LinkedList::insertFront(const T &item){
Node*ptr=new Node(item);
ptr->nextNode()=front;
front=ptr;
size++;
}
template
void LinkedList::insertRear(const T &item){
Node*ptr=&item;
rear->nextNode()=rear;
rear=ptr;
size++;
}
template
void LinkedList::insertAt(const T&item){
Node*ptr(item);
prevptr->insert(ptr);
size++;
}
template
void LinkedList::insertAfter(const T&item){
Node*ptr(item);
currptr->insert(ptr);
size++;
}

template
T LinkedList::deleteFront(){
Node*ptr=front->nextNode();
delete *front;
front=ptr;
size--;
return front;
}

template
void LinkedList::deletecurrent(){
prevptr->deleteafter();
size--;
}
template
T& LinkedList::data(){return currptr->data;}
template
const T& LinkedList::data()const{return currptr->data;}
template
void LinkedList::clear(){
reset();
for(int i=1;i<=size;i++){
deleteFront();
}
}
//main.cpp
#include
#include "LinkedList.h"
using namespace std;
int main(){
LinkedListlist;
for(int i=0;i int item;
cin>>item;
list.insertFront(item);
}
cout<<"List:";
list.reset();
while(!list.endofList()){
cout< list.next();
}
cout int key;
cout cin>>key;
list.next();
while(!list.endofList()){
if(list.data()==key)
list.deletecurrent();
list.next();
}
cout<<"List:";
list.reset();
while(!list.endofList()){
cout<<list.data()<<" ";
list.next();
}
cout<<endl;
return 0;
}

1个回答

dabocaiqq
dabocaiqq   2016.11.16 01:50
Csdn user default icon
上传中...
上传图片
插入图片