为以两个迭代器为参数的链表创建交换函数? 此作业,我必须使用链表的标准实现[例如:代码>类列表< /代码>,类节点< /代码>,类迭代器< /C> >,全部从大C++教科书]交换列表的两个元素(假设元素是节点),但我的问题是,我们必须使用两个迭代器作为交换的参数
因此,它看起来像为以两个迭代器为参数的链表创建交换函数? 此作业,我必须使用链表的标准实现[例如:代码>类列表< /代码>,类节点< /代码>,类迭代器< /C> >,全部从大C++教科书]交换列表的两个元素(假设元素是节点),但我的问题是,我们必须使用两个迭代器作为交换的参数,c++,linked-list,swap,C++,Linked List,Swap,因此,它看起来像void List::swap(迭代器i1,迭代器i2) 我解决这个问题的方法是,我必须创建临时变量来保存我要交换的节点及其位置的数据,然后在将另一个节点的数据插入其位置时删除其中一个 例如,我的意思是,对于i2,我有两个变量:一个保存其数据[字符串],另一个保存其位置[我们可以使用迭代器类中的get()函数找到它] 那么我会: 擦除i1[使用列表::擦除(迭代器iter)功能] 将迭代器移动到i1 然后,插入一个带有i2数据的新节点[使用List::insert(迭代器ite
void List::swap(迭代器i1,迭代器i2)
我解决这个问题的方法是,我必须创建临时变量来保存我要交换的节点及其位置的数据,然后在将另一个节点的数据插入其位置时删除其中一个
例如,我的意思是,对于i2
,我有两个变量:一个保存其数据[字符串],另一个保存其位置[我们可以使用迭代器类中的get()
函数找到它]
那么我会:
- 擦除
i1
[使用列表::擦除(迭代器iter)
功能]
- 将迭代器移动到
i1
- 然后,插入一个带有
i2
数据的新节点[使用List::insert(迭代器iter,字符串s)
]
然而,链表对我来说仍然是新的和令人困惑的,我不确定这些实现是否有意义,而且我必须使用两个迭代器作为参数这一事实让我很反感
感谢你们的帮助
编辑:下面是列表、节点和迭代器的实现。函数定义将占用太多空间
列表
class List
{
public:
/**
Constructs an empty list;
*/
List();
/**
Appends an element to the list.
@param data the value to append
*/
void push_back(string data);
/**
Inserts an element into the list.
@param iter the position before which to insert
@param s the value to append
*/
void insert(Iterator iter, string s);
/**
Removes an element from the list.
@param iter the position to remove
@return an iterator pointing to the element after the
erased element
*/
Iterator erase(Iterator iter);
/**
Gets the beginning position of the list.
@return an iterator pointing to the beginning of the list
*/
Iterator begin();
/**
Gets the past-the-end position of the list.
@return an iterator pointing past the end of the list
*/
Iterator end();
void push_front(string s);
void reverse();
void swap(Iterator i1, Iterator i2);
private:
Node* first;
Node* last;
friend class Iterator;
};
节点
class Node
{
public:
/**
Constructs a node with a given data value.
@param s the data to store in this node
*/
Node(string s);
private:
string data;
Node* previous;
Node* next;
friend class List;
friend class Iterator;
};
迭代器
class Iterator
{
public:
/**
Constructs an iterator that does not point into any list.
*/
Iterator();
/**
Looks up the value at a position.
@return the value of the node to which the iterator points
*/
string get() const;
/**
Advances the iterator to the next node.
*/
void next();
/**
Moves the iterator to the previous node.
*/
void previous();
/**
Compares two iterators
@param b the iterator to compare with this iterator
@return true if this iterator and b are equal
*/
bool equals(Iterator b) const;
private:
Node* position;
List* container;
friend class List;
};
如果不了解列表和迭代器是如何定义的,就很难给出建议。我的建议是不要自己实现swap,而是使用(如果迭代器满足要求)或者。@Someprogrammerdude我确信std::iter_交换不会有帮助。@Zmu你可以用铅笔、纸和一点耐心来解决所有问题。作为更新@Someprogrammerdude,我只是交换了值,代码就可以运行了。这不太令人满意,但我意识到指令没有明确说明交换指针的位置,只是“交换两个元素”。没有看到列表和迭代器是如何定义的,很难给出建议。我的建议是不要自己实现交换,而是使用(如果迭代器满足要求)或者。@Someprogrammerdude我确信std::iter_交换不会有帮助。@Zmu你可以用铅笔、纸和一点耐心来解决所有问题。作为更新@Someprogrammerdude,我只是交换了值,代码就可以运行了。这不太令人满意,但我意识到指令并没有明确指出交换指针的位置,只是“交换两个元素”。