【发布时间】:2021-01-12 23:08:09
【问题描述】:
我的任务是删除链接列表中的所有重复条目。假设列表是从最小到最大排序的,因此我需要做的就是删除彼此相邻的重复项。
例如:如果列表在调用 remove_duplicates() 之前包含 {1,2,2,2,3,3,4,5,5,5,5,5,6},它应该包含 {1,2 ,3,4,5,6} 之后。
我的问题是,每当调用 remove_duplicates() 时,我的代码也会打印重复项。
这是我的代码:
list.cpp:
#include <iostream>
using namespace std;
#include "list.h"
// on some machines member variables are not automatically initialized to 0
List::List()
{
m_head = NULL;
m_length = 0;
}
// delete all Nodes in the list
// since they are dynamically allocated using new, they won't go away
// automatically when the list is deleted
// Rule of thumb: destructor deletes all memory created by member functions
List::~List()
{
while (m_head)
{
Node *tmp = m_head;
m_head = m_head->m_next;
delete tmp;
}
}
// always insert at the front of the list
// Note: this works even in the SPECIAL CASE that the list is empty
void List::insert(int value)
{
if (!m_head || value < m_head->m_value)
{
m_head = new Node(value, m_head);
}
else
{
Node *ptr = m_head;
while (ptr->m_next != NULL && value > ptr->m_next->m_value)
{
ptr = ptr->m_next;
}
ptr->m_next = new Node(value, ptr->m_next);
}
m_length++;
}
// iterate through all the Nodes in the list and print each Node
void List::print()
{
for (Node *ptr = m_head; ptr; ptr = ptr->m_next)
{
cout << ptr->m_value << endl;
}
}
void List::remove_duplicates()
{
Node *curr = m_head;
Node *temp = m_head;
Node *delPtr = NULL;
if(curr == NULL)
{
return;
}
if(curr != NULL)
{
if(curr -> m_value == curr ->m_next ->m_value)
{
delPtr = curr;
curr = curr -> m_next;
temp ->m_next = curr;
delete delPtr;
}
else
{
curr = curr -> m_next;
}
}
}
list.h:
class List
{
public:
List();
~List();
void insert(int value); // insert at beginning of list
void print(); // print all values in the list
int length() {return m_length;}
void remove_duplicates();
int *convert_to_array(int &size);
private:
class Node
{
public:
Node(int value, Node *next)
{m_value = value; m_next = next;}
int m_value;
Node *m_next;
};
Node *m_head;
int m_length;
};
main.cpp:
#include <assert.h>
#include <iostream>
using namespace std;
#include "list.h"
int main()
{
List list;
int value;
// read values and insert them into list
while (cin >> value)
{
list.insert(value);
}
cout << "printing original list: \n";
list.print();
list.remove_duplicates();
cout << "printing list after removing duplicates: \n";
list.print();
}
注意:我的导师已经给了我一切,我应该写的唯一代码是void List::remove_duplicates()
我在 stackoverflow 上查找了其他示例,但似乎没有一个可以帮助我解决我的情况。 另外,我想提一下,我对链接列表以及它们的功能仍然很陌生。因此,我不确定从这里去哪里。任何帮助将不胜感激
【问题讨论】:
-
我在 stackoverflow 上查找了其他示例,但似乎没有一个对我的情况有帮助。 -- 老实说,你不会学习如何弄清楚通过首先查看代码示例来了解链表的复杂性。您应该做的第一件事是在纸上绘制一个链表结构,其中指针是线,链接是框。然后制定在纸上如何删除重复项,然后将您在纸上的内容写入代码。 '
-
您能解释一下您的 remove_duplicates 应该如何删除多个重复项吗?
-
@PaulMcKenzie 我认为我写下的内容很有道理,但我会先尝试将其写在纸上。
-
这是另一个想法。假设它不是一个链表,而是一个可调整大小的数组(如
std::vector)。您将如何在循环中删除重复项?假设您有两个索引(就像您有两个链接,curr 和 curr->next)。还假设有一个“擦除”功能可以擦除某个索引处的项目。确保索引不会弄乱或您不会错过任何重复项会涉及什么逻辑?不管这个逻辑是什么,对于链表来说都是一样的逻辑,只是你处理的是 curr 和 curr->next,而不是索引i和i+1。
标签: c++ class linked-list nodes