The_Icer 2016-09-08 05:56 采纳率: 62.5%
浏览 1009

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

我写了个链表类,但是报错,想请教一下。以下是代码。
//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 2016-11-15 17:50
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序