Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.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++ - Fatal编程技术网

查找链表C++中的所有匹配节点

查找链表C++中的所有匹配节点,c++,C++,我正在编写一个函数来查找链表中某个节点的所有出现次数,该函数将向主函数返回出现次数,然后主函数将显示这些出现次数。程序确实编译了,但它只是冻结了,当我输入要查找的正确名称时,似乎什么也没有发生。如果我输入了错误的名称(不在列表中),findall函数返回0,程序的其余部分工作正常。请看一看 main.cpp #include <iostream> #include <fstream> #include <string> using namespace std;

我正在编写一个函数来查找链表中某个节点的所有出现次数,该函数将向主函数返回出现次数,然后主函数将显示这些出现次数。程序确实编译了,但它只是冻结了,当我输入要查找的正确名称时,似乎什么也没有发生。如果我输入了错误的名称(不在列表中),findall函数返回0,程序的其余部分工作正常。请看一看

main.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "List.h"

void extra(list &);

/***********************************
 * Main
 * Test function - DO NOT CHANGE
 ***********************************/ 
void main()
{
  list a;
  extra(a);
}

/***********************************
 * Extra Credit
 * Test function - DO NOT CHANGE
 ***********************************/ 

void extra(list &a)
{ int i,n;
  node_ptr map[4];
  string first,last;

// Find node

  cout << endl;
  cout << "Enter First and Last name: ";
  cin >> first >> last;
  n = a.findall(first,last,map,4);
 // Display forwards

  cout << endl;
  cout << "Find List\n--------------\n";
  for (i = 0; i < n; i++)
  {
      map[i]->put(cout);
  }
}
清单h

#include "Node.h"
#include <iostream>
#include <string>
using namespace std;

class list 
{  public:
        list();                                             // Empty constructor
        ~list();                                            // Destructor

        int findall(string, string, node_ptr*, int);

        node *find(string, string);                         // Locate a note


    private:
        node *head;
};
Node.h

#include <iostream>
#include <string>
using namespace std;


class list;

class node
{ friend list;
  public:
    node();                           // Null constructor
    ~node();                          // Destructor 

    void put(ostream &out);           // Put

  private:
    string first,last;
    int age;
    node *next;
};

typedef node * node_ptr;
List.cpp

#include "List.h"
#include <iostream>
#include <string>
using namespace std;

/**
* Empty Constructor
*
*/

list::list()
{
    head = nullptr;
}

/**
* Destructor Constructor
*
*/

list::~list()
{  if (head == nullptr) return;

    node *p = head, *t;

    while (p) 
    {
        t = p;
        p = p->next;
        delete t;
    }

    head = nullptr;
}

/**
* Locate node
*
*/

node *list::find(string last, string first) 
{
    node *temp = head;
    while (temp)
    {
        if (temp->first == first && temp->last == last) return temp;
        temp = temp->next;
    }
    return nullptr;
}


/**
* Find all.
*
*/

int list::findall(string first, string last, node_ptr* map, int n)
{
    int ans;
    ans = 0;
    *map = find(first, last);
    while (*map != NULL)
    {
        ans++;
        *map = (*map)->next;
        *map = find(first, last);

    }
    return ans;
}
Node.cpp

#include "Node.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

/**
* Empty Constructor
* 
*/

node::node()
{
    last  = "";
    first = "";
    age   = 0;
    next  = nullptr;
}


/**
* Destructor
*
*/

node::~node()
{ if (next != nullptr) next = nullptr; 
}

/**
* Put
*
*/

void node::put(ostream &out)
{ out << setw(14) << left << last << setw(14) << first << setw(10) <<   age << endl; 
}
我真的很感谢你的帮助。谢谢。

芬德尔冻结了,因为它陷入了一个无休止的循环

如果没有找到匹配的节点,则第一次调用find将返回nullptr和findall exits

但是如果找到了匹配的节点,就会进入一个循环,调用find从头开始重新搜索整个列表。将找到与以前相同的节点。然后你再打电话找,再打,等等

为了解决这个问题,如果find返回一个匹配的节点,您需要在下面的find调用中传递下一个节点,这样它就可以从上一次搜索结束的地方开始搜索。重复此操作,直到到达列表的末尾

class list 
{  public:
    ...

        int findall(string first, string last, node_ptr *map, int n);

        node_ptr find(string first, string last, node_ptr start = nullptr);               // Locate a note

    ...
};

请您的问题提供一个答案。当您使用调试器一次一行地执行程序时,您做了哪些观察?stackoverflow.com不是一个你可以转储大量代码并期望其他人为你调试自己代码的网站。解决这些问题的正确工具是你的调试器。在询问堆栈溢出之前,应该逐行检查代码。如需更多帮助,请阅读。至少,您应该[编辑]您的问题,以包括一个重现您的问题的示例,以及您在调试器中所做的观察。while!infire.eof会在某个时候踢你的牙齿。TL:DR,要知道你是否已经到达了文件的末尾,唯一的方法就是读取失败,因为你到达了文件的末尾。您需要阅读,然后测试,如果阅读成功则使用。谢谢您的回复。然而,我正在尝试一种不改变发现的方法,因为它只能接受两个参数。我想我需要重写findall函数,这样我就不必使用find,因为我无法更改它。再次感谢您的帮助。我更新了我的答案。再次感谢您的帮助。我了解你大部分的findall代码。但是,我不确定这行代码是做什么的。*我理解为map++=temp:增加map ptr并将temp值分配给该ptr?谢谢。postfix++增量运算符的值高于*dereference运算符,因此该行相当于*map++=temp。后缀运算符返回增量之前的上一个值。代码将temp分配给map最初指向的当前地址,并递增map以指向下一个地址。该行相当于以下语句:node_ptr*temp_map=map++地图*温度映射=温度;
node_ptr list::find(string last, string first, node_ptr start) 
{
    node_ptr temp = (start) ? start : head;
    while (temp)
    {
        if ((temp->first == first) && (temp->last == last)) break;
        temp = temp->next;
    }
    return temp;
}

int list::findall(string first, string last, node_ptr* map, int n)
{
    int ans = 0;
    node_ptr temp = nullptr;
    while (ans < n)
    {
        temp = find(first, last, temp);
        if (!temp) break;
        *map++ = temp;
        ++ans;
        temp = temp->next;
    }
    return ans;
}
class list 
{  public:
    ...

        int findall(string first, string last, node_ptr *map, int n);

        node_ptr find(string first, string last);               // Locate a node

    ...
};
node_ptr list::find(string last, string first) 
{
    node_ptr temp = head;
    while (temp)
    {
        if ((temp->first == first) && (temp->last == last)) break;
        temp = temp->next;
    }
    return temp;
}

int list::findall(string first, string last, node_ptr* map, int n)
{
    int ans = 0;
    node_ptr temp = head;
    while (ans < n)
    {
        while (temp)
        {
            if ((temp->first == first) && (temp->last == last)) break;
            temp = temp->next;
        }
        if (!temp) break;
        *map++ = temp;
        ++ans;
        temp = temp->next;
    }
    return ans;
}