不太聪明的蜗牛 2023-10-21 21:02 采纳率: 0%
浏览 6

运行不出来结果list容器



```c++
#include<iostream>
using namespace std;
template<class T>
class List
{
    struct Node                //双向结点声明
    {
        T data;    
        Node *prev,*next;
        Node(const T &d=T(),Node *p=NULL,Node *n=NULL):data(d),prev(p),next(n){}
    };
    int size;                //数据结点个数
    Node *head;                //头结点指针
    Node *tail;                //尾结点指针
    void init()                //初始化函数
    {size=0;head=new Node;tail=new Node;head->next=tail;tail->prev=head;}

public:
    class const_iterator
    {
    protected:
        Node *current; 
        T& retrieve()const{return current->data;}
        const_iterator(Node *p):current(p){}//构造函数初始化成员变量 
        friend class List<T>;//特定的List<T>实例可以访问当前类的私有和保护成员
    public:
        const_iterator():current(NULL){}
        const T& operator*()const{return retrieve();}//current->data
        const_iterator& operator++()//前++
        {
            current=current->next;
            return *this;
        }
        const_iterator operator++(int)//后++
        {
            const_iterator old=*this;    //old=current;
            ++(*this);                    //current=current->next;
            return old;
        }
        const_iterator& operator--()//前--
        {
            current=current->prev;
            return *this;
        }
        const_iterator operator--(int)//后--
        {
            const_iterator old=*this;    //old=current;
            --(*this);                    //current=current->next;
            return old;
        }
        bool operator==(const const_iterator & rhs)const
            {return current==rhs.current;}
        bool operator!=(const const_iterator & rhs)const
            {return current!=rhs.current;}
        Node* gethead(){
                return head;
            }

    };
    class iterator:public const_iterator
    {
    protected:
        Node *current;
        iterator(Node *p):const_iterator(p){}
        friend class List<T>;
    public:
        iterator(){}
        /*T& operator*(){
        return retrieve();}*/
        const T& operator*()const{return const_iterator::operator*();}
        iterator& operator++()//前++
        {
            current=current->next;
            return *this;
        }
        iterator operator++(int)//后++
        {
            iterator old=*this;            //old=current;
            ++(*this);                    //current=current->next;
            return old;
        }
        iterator& operator--()//前--
        {
            current=current->prev;
            return *this;
        }
        iterator operator--(int)//后--
        {
            iterator old=*this;            //old=current;
            --(*this);                    //current=current->next;
            return old;
        }
    };
    List(){init();}                                //默认构造函数
    List(const List<T> &l){init();operator=(l);}//复制构造函数
    ~List(){Clear();delete head;delete tail;}    //析构函数
    const List& operator=(const List& l);        //复制赋值运算符函数
    int Size()const{return size;}                //求数据个数
    bool Empty()const{return size==0;}            //判空函数
    void Clear(){while(!Empty())Pop_front();}    //清表

    iterator begin(){return iterator(head->next);}
    const_iterator begin()const{return const_iterator(head->next);}
    iterator end(){return iterator(tail);}
    const_iterator end()const{return const_iterator(tail);}
    
    T& Front(){return *begin();}            //返回首元素的引用
    const T& Front()const{return *begin();}    //返回首元素的常量型引用
    T& Back(){return *--end();}                //返回尾元素的引用            
    const T& Back()const{return *--end();}    //返回尾元素的常量型引用
    void Push_front(const T& item){Insert(begin(),item);}//首插
    void Push_back(T&item){Insert(end(),item);}//尾插void Push_back(const T& item){Insert(end(),item);}//尾插
    void Pop_front(){Erase(begin());}        //删除首结点
    void Pop_back(){Erase(--end());}        //删除尾结点
    iterator Erase(iterator itr);            //删除指示器位置上的结点
    iterator Insert(iterator itr,const T& item);//在指示器的位置插入item
    T RemoveMin()
        {
    Node*current=head;
while(current!=NULL&&current->data>current->next->data)//找到最小值 
{
current=current->next; 
}    
T m=current->data; 
if(current->next!=NULL)
{
    current->next->prev=current->prev;
}
else
{
    tail=current->prev;
}
if(current->prev!=NULL)
{
    current->prev->next=current->next;
}
else
{
    head=current->next;
}
return m;
}
void printlist()
{
    Node* current=head;
while(current!=NULL)
{
    cout<<current->data;
    current=current->next; 
}
 } 
};
template<class T>//复制赋值成员运算符函数的实现
const List<T>& List<T>::operator=(const List<T>& l)
{
    Clear();                    //清为空表
    for(const_iterator itr=l.begin();itr!=l.end();++itr) //把表l的结点逐个复制
        Push_back(*itr);
    return *this;
}

template<class T>
typename List<T>::iterator List<T>::Erase(iterator itr)
{
    Node *p=itr.current;
    iterator re(p->next);
    p->prev->next=p->next;
    p->next->prev=p->prev;
    delete p;
    size--;
    return re;
}
template<class T>
typename List<T>::iterator List<T>::Insert(iterator itr,const T& item)
{
    Node *p=itr.current;
    size++;
    p->prev->next=new Node(item,p->prev,p);
    p->prev=p->prev->next;
    return iterator(p->prev);
}
int main()
{
List<int> l;
int a[10]={1,2,3,9,5,6,7,8,9,0};
for(int i=0;i<10;i++)
{
l.Push_back(a[i]);
}
l.RemoveMin();
l.printlist();
return 0;
}


  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-21 23:13
    关注

    【以下回答由 GPT 生成】

    我看到你的代码中有一些不完整的部分,导致无法成功运行。以下是一些需要修复的问题:

    1. 你的代码缺少一些结束的大括号 }。请确保你的代码块有完整的开始和结束。

    2. bool operator== 的定义处,你没有给出实际的比较逻辑。你需要使用 current 指针来比较两个迭代器是否指向同一个节点。

    3. List 类中,你使用了模板,但没有进行模板参数的传递。你需要在类的定义和成员函数的定义中传递模板参数。

    修复以上问题后,你的代码应该类似于以下形式:

    #include<iostream>
    using namespace std;
    template<class T>
    class List
    {
        struct Node                //双向结点声明
        {
            T data;    
            Node *prev,*next;
            Node(const T &d=T(),Node *p=NULL,Node *n=NULL):data(d),prev(p),next(n){}
        };
        int size;                //数据结点个数
        Node *head;                //头结点指针
        Node *tail;                //尾结点指针
        void init()                //初始化函数
        {size=0;head=new Node;tail=new Node;head->next=tail;tail->prev=head;}
    
    public:
        class const_iterator
        {
        protected:
            Node *current; 
            T& retrieve()const{return current->data;}
            const_iterator(Node *p):current(p){}//构造函数初始化成员变量 
            friend class List<T>;//特定的List<T>实例可以访问当前类的私有和保护成员
        public:
            const_iterator():current(NULL){}
            const T& operator*()const{return retrieve();}//current->data
            const_iterator& operator++()//前++
            {
                current=current->next;
                return *this;
            }
            const_iterator operator++(int)//后++
            {
                const_iterator old=*this;    //old=current;
                ++(*this);                    //current=current->next;
                return old;
            }
            const_iterator& operator--()//前--
            {
                current=current->prev;
                return *this;
            }
            const_iterator operator--(int)//后--
            {
                const_iterator old=*this;    //old=current;
                --(*this);                    //current=current->next;
                return old;
            }
            bool operator==(const const_iterator& rhs) const {
                return current == rhs.current;
            }
        };
        //...
    };
    

    请尝试修复以上问题,如果还有其他问题,或者你需要更详细的解释,请告诉我。



    【相关推荐】



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

报告相同问题?

问题事件

  • 创建了问题 10月21日

悬赏问题

  • ¥15 mySQL5.7.34安装遇到的问题
  • ¥15 结构功能耦合指标计算
  • ¥20 visual studio中c语言用ODBC链接SQL SERVER
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊