Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C-struct中查找相同的元素_C++_Struct_Element - Fatal编程技术网

C++ 在C-struct中查找相同的元素

C++ 在C-struct中查找相同的元素,c++,struct,element,C++,Struct,Element,我必须编写一个函数,将元素添加到C-struct,但它不能添加相同的元素。例子: 输入: 1213 输出: ADDED 1 ADDED 2 NOT ADD 1 ADD 3 元素取自数组,下面是一段使用我需要编写的函数的代码: int tab[] = {1,4,1,3,5}; Node* head = 0; for (size_t i = 0, e = std::size(tab); i != e; ++i) { bool b = add(head,tab[i]); cout &

我必须编写一个函数,将元素添加到C-struct,但它不能添加相同的元素。例子: 输入:
1213

输出:

ADDED 1
ADDED 2
NOT ADD 1
ADD 3
元素取自数组,下面是一段使用我需要编写的函数的代码:

int tab[] = {1,4,1,3,5};
Node* head = 0;
for (size_t i = 0, e = std::size(tab); i != e; ++i) {
    bool b = add(head,tab[i]);
    cout << tab[i] << (b ? "     " : " NOT ")
         << "added" << endl;
}
这是我写的,但它添加了数组中的所有元素。我无法更改循环,只能添加功能:

bool add(Node*& head, int data){
    Node *n = new Node;
    n->data = data;
    n->next = 0;

    if(!head)
        head = n;
    else{
        Node *tmp = head;
        while(tmp->next)
            tmp = tmp->next;
        tmp->next = n;
    }
};

当前,您只需添加元素,而不查看它是否已经存在

定义可以是

bool add(Node*& head, int data){

  if(!head) {
    head = new Node;
    n->data = data;
    n->next = 0;
    return true;
  }

  Node *tmp = head;

  while (tmp->next) {
    if (tmp->data == data)
      return false;
    tmp = tmp->next;
  }
  if (tmp->data == data)
    return false;

  tmp->next = new Node;
  tmp->next->data = data;
  tmp->next->next = 0;
  return true;
}

我建议您添加一个构造函数,以便在每次创建新实例后不必设置数据和下一个字段

Node::Node(int d) : next(0), data(d) {
}

// add should be a static method of Node, to be able to access next and data while they are private
bool add(Node*& head, int data){
  if(!head) {
    head = new Node(data);
    return true;
  }

  Node *tmp = head;

  while (tmp->next) {
    if (tmp->data == data)
      return false;
    tmp = tmp->next;
  }

  if (tmp->data == data)
    return false;

  tmp->next = new Node(data);
  return true;
}

这是我的尝试。首先查找现有数据,然后添加(如果不存在)(现有代码没有更改)


所以我做了一些类似的事情,它与我的数据一起工作。我想它大体上是有效的

bool add(Node*& head, int data){

Node *n = new Node;
n->data = data;
n->next = 0;

if(!head)
    head = n;
else{
    Node *tmp = head;
    while(tmp->next){
        if(tmp->data == data)
            return false;
        else
        tmp = tmp->next;
    }
    tmp->next = n;
}
};

您需要做的是更改
add
代码,以便if首先使用循环在列表中查找元素,并且仅在未找到元素时添加它。这似乎是一件显而易见的事情,你坚持在哪一部分?我不知道如何反复浏览列表以找到相同的元素你的问题到底是什么?你的问题帖里没有问题。旁注:不需要
如果
中的
添加
,您可以在(head)head=head->next时执行
@Mrfk您已经在
else
块中遍历列表。在
while
块中,只需检查
tmp->data
的值,如果与
data
相同,则中止操作。这是一个简单的循环,
Node*tmp=head;虽然(tmp){if(tmp->data==data){found it}tmp=tmp->next;}
看起来确实比您已经编写的代码简单,但我猜指针很难。感谢您的输入,但是不,我不能添加相同的数据。我认为只有结果是相关的,我将我的答案编辑为“如果已经存在,则不添加”,但我必须说:
add
函数做的事情太多了。因为我们在做C-isms:应该有一个
Node*Node\u new(int)
,一个
bool Node\u isNil(Node*)
,一个
Node*Node\u last(Node*)
和一个
bool Node\u find(int)
。应该是
while(tmp)
而不是
,而(tmp->next)
,因为它找不到列表中最后一个元素。@john no,使用
while(tmp)
搜索最后一个元素以在其后添加新元素时,如何将新元素添加到列表的末尾?即使不使用它,它也会不必要地分配节点。这是真的,我的错。感谢您注意到它,当它是列表中的最后一项时,它也找不到相等的元素。您做了一次内存泄漏检查,将列表检查两次是多么糟糕的主意@布鲁诺,非常正确,我会把这个改进留给OP
bool add(Node*& head, int data) {
    Node *tmp = head;
    while (tmp) {
        if (tmp->data == data)
            return false; // data already present
        tmp = tmp->next;
    }

    Node *n = new Node;
    n->data = data;
    n->next = 0;

    if (!head) {
        head = n;
    }
    else {
        Node *tmp = head;
        while(tmp->next)
            tmp = tmp->next;
        tmp->next = n;
    }
    return true; // data added
}
bool add(Node*& head, int data){

Node *n = new Node;
n->data = data;
n->next = 0;

if(!head)
    head = n;
else{
    Node *tmp = head;
    while(tmp->next){
        if(tmp->data == data)
            return false;
        else
        tmp = tmp->next;
    }
    tmp->next = n;
}
};