如何将插入函数写入通用列表C ++? [英] How to write an insert function to a generic list C++ ?

查看:111
本文介绍了如何将插入函数写入通用列表C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用c ++编写通用列表的代码:在这个lise中我有一个geniric节点(这意味着它中的数据可以是任何类型),以及一个类迭代器,它的工作是迭代器指向一个列表中的specefic节点,迭代器也可以指向任何其他列表,所以在它的类中我添加了一个指向当前列表的指针。

我的问题是插入函数,我插入一个新节点到列表,

并且在这个函数中,如果迭代器指向列表的末尾,我们将新节点添加到列表的末尾,否则我们将节点插入节点之前的一个节点迭代器当前指向,如果iteratot指向一个不同的节点然后就是一个错误..



我的问题是该函数只插入第一个节点..什么可能是问题?我似乎无法弄清楚:\



我尝试过的事情:



 #include< iostream> 
#include< assert.h>

class List {

public:
List();
List(const List&);
~List();
列表< T>& operator =(const List& list);
模板<类E>
class ListNode {
private:
E data;
ListNode(const E& t,ListNode< E> * next):data(t),next(next){}
~ListNode(){delete next;}
ListNode< E> ; *下一个;
public:
friend class List< E> ;; //?
朋友级迭代器; // ??
E getData()const {
return this-> data;
}
ListNode< E> * getNext()const {
返回this-> next;
}
};
class Iterator {
List< T> * list;
ListNode< T> * current;
Iterator(List< T> * list = NULL,ListNode< T> * current = NULL);
朋友类列表< T> ;;
朋友类ListNode< T> ;;
public:
///// for iterator的函数
};
Iterator begin();
Iterator end();
void insert(const T& data,Iterator iterator = 0); // 你怎么了 //!!只需插入两个我想睡觉的元素
void remove(Iterator iterator);
class Predicate {
private:
T target;
public:
Predicate(T i):target(i){}
bool operator()(const T& i)const {
return i == target;
}
};
Iterator find(const Predicate& predicate);
class比较{
private:
T target;
public:
Compare(T i):target(i){}
bool operator()(const T& i)const {
return i<目标;
}
};
void sort(const Compare& compare);
bool operator ==(const List& list);
bool operator!=(const List& list);
int getSize()const;
bool empty()const;

int compareLinkedList(ListNode< T> * node1,ListNode< T> * node2);

private:
ListNode< T> *头;
ListNode< T> *尾巴;
int size;
};



//我可爱的插入函数(不是真的:'((((()
模板< typename T>
void List< T> ; :: insert(const T& data,Iterator iterator){
if(iterator.list!= this){
throw mtm :: ListExceptions :: ElementNotFound();
return;
}
ListNode< T> * newNode = new ListNode< T>(data,NULL);
if(iterator.current == this-> end()。current){
newNode-> next = this-> end()。current;
Iterator temp = end();
temp--;
temp.current-> next = newNode ;
}
else {
if(head == NULL){
head = newNode;
}
else {
Iterator temp1 =迭代器;
temp1--;
temp1.current-> next = newNode;
temp1 ++;
newNode-> next = temp1.current;

}

}
size ++;
}



以及我如何测试清单:


stati c void listExample(){
List< int>列表;
list.insert(1,list.end());
list.insert(2,list.end());
list.insert(3,list.end());

int counter = 1;
for(List< int> :: Iterator it = list.begin(); it!= list.end(); ++ it){// ????
ASSERT_EQUALS(counter ++,* it);
}

解决方案

如果要在列表中间插入项目,则必须自行决定其中,通常是插入位置。



为此

1.你想要插入位置,

2.添加新条目为项目之前的跟随者

3.将旧项目设为新项目的关注者



一些学习东西:关于 C中的列表

I am writing a code for generic list in c++ : in this lise i have a geniric node (which mean the data in it could be any type ) , and a class iterator which the iterator that it's job is to point to a specefic node in the list , also the iterator could point to any other list so in it's class i added a pointer to the current list .
my problem is with the insert function, where i insert a new node to the list,
and in this function if the iterator poiints to the end of the list the we add the new node to the end of the list else we insert the node one place before the node that the iterator currently points to , and if the iteratot points to a different node then thats an error ..

my problem is that the function inserts only the first node .. what could be the problem? i can't seem to figure it out :\

What I have tried:

#include <iostream>
#include <assert.h>

class List {

public:
    List();
    List(const List&);
    ~List();
    List<T>& operator=(const List& list);
    template <class E>
    class ListNode {
        private:
             E data;
             ListNode(const E& t, ListNode<E> *next):data(t), next(next){}
             ~ListNode(){delete next;}
             ListNode<E> *next;
        public:
            friend class List<E>;  //??
            friend class Iterator;   // ??
            E getData() const{
               return this->data;
            }
            ListNode<E>* getNext() const{
               return this->next;
            }
            };
    class Iterator {
        List<T>* list;
        ListNode<T>* current;
        Iterator( List<T>* list=NULL, ListNode<T>* current=NULL);
        friend class List<T>;
        friend class ListNode<T>;
    public:
     ///// functions for iterator
    };
    Iterator begin() ;
    Iterator end() ;
    void insert(const T& data, Iterator iterator=0); // what is wrong with you //!! just insert two more elements i want to sleep
    void remove(Iterator iterator);
    class Predicate{
    private:
      T target;
    public:
      Predicate(T i) : target(i) {}
      bool operator()(const T& i) const {
        return i == target;
      }
    };
    Iterator find(const Predicate& predicate);
    class Compare{
    private:
      T target;
    public:
      Compare(T i) : target(i) {}
      bool operator()(const T& i) const {
        return i < target;
      }
    };
    void sort(const Compare& compare);
    bool operator==(const List& list);
    bool operator!=(const List& list);
    int getSize() const;
    bool empty() const;

    int compareLinkedList(ListNode<T> *node1,  ListNode<T> *node2);

private:
   ListNode<T> *head;
   ListNode<T> *tail;
int size;
};



// my lovely insert function (not really :'((((  )
template <typename T>
void List<T>::insert(const T& data, Iterator iterator){
    if(iterator.list!=this){
        throw mtm::ListExceptions::ElementNotFound();
        return;
    }
    ListNode<T> *newNode = new ListNode<T>(data, NULL);
    if(iterator.current==this->end().current){
        newNode->next=this->end().current;
        Iterator temp=end();
        temp--;
        temp.current->next=newNode;
           }
    else {
    if (head == NULL) {
    head = newNode;
    }
   else {
       Iterator temp1=iterator;
        temp1--;
        temp1.current->next=newNode;
        temp1++;
        newNode->next=temp1.current;

   }

   }
    size++;
}



and how i tested the list :


static void listExample(){
    List<int> list;
    list.insert(1, list.end());
    list.insert(2, list.end());
    list.insert(3, list.end());

    int counter = 1;
  for (List<int>::Iterator it = list.begin(); it != list.end(); ++it) {   // ????
    ASSERT_EQUALS(counter++, *it);
  }

解决方案

If you want to insert an item in the middle of the list, you must decide where, normally an "insert at position".

For that
1. you enumarate to the inserted position,
2. add the new entry as follower of the item before
3. set the old item as follower of the new item

Some learning stuff: fundamental theory with pictures about
lists in C.


这篇关于如何将插入函数写入通用列表C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆