weixin_43248378 2018-12-24 21:23 采纳率: 0%
浏览 247

C++链表实现有点问题,请帮我看看

#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;

}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 19:28
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办