【问题标题】:Searching for data in a linked list在链表中搜索数据
【发布时间】:2016-04-21 18:27:04
【问题描述】:

我的程序的第一部分是从文件中提取特定数据并将其插入到链表中。我成功地创建了程序的那一部分,但我在搜索我的链接列表和打印数据时遇到了困难。到目前为止,这是我的代码:

struct Country
 {
  string  name;
  double  population;
 };

struct Node 
 {
  Country ctry;
  Node *next;
 };
Node *world;

void makeList(Node *&world);
void printCountry (Node *&world, string name);

int main ()
{
    string name;

    makeList(world);
    printCountry (world, name);
    return 0;
}

void makeList(Node *world)
{
    ifstream inFile("population.csv");

    if (!inFile.fail())
    {
        cout << "File has opened successfully." << endl;
    }
    else
    {
        cout << "File has failed to open." << endl;
        exit(1);
    }

   double temp, temp1, temp2, temp3, population;
   string countryName;

   Node *top = new Node; 
   world = top;

   while (!inFile.eof())
   {
        top -> next = NULL;

        inFile >> temp >> temp1 >> temp2 >> temp3 >> population;
        getline (inFile, countryName);

        top -> ctry.population = population;
        top -> ctry.name = countryName;

        if (!inFile.eof())
        {
            top -> next = new Node;
            top = top -> next;
        }
   } 

   // check if list is created successfully
   while (world -> next != NULL)
   {
    cout << world -> ctry.population << "   " << world -> ctry.name << endl;
    world = world -> next;
   }
}

void printCountry (Node *world, string name)
{
    string countryToFind;

    cout << "What country do you want to find? " << endl;
    cin >> countryToFind;

    while (world != NULL) 
    {
        if (world -> ctry.name == countryToFind)
        {
            cout << "Country has been found: " << world -> ctry.name << " has a population of "
                 << world -> ctry.population << endl;
            break;
        }

        else
        {
            if (world -> next == NULL)
            {
                cout << "End of file" << endl;
                break;
            }
            world = world -> next;
        }
    } 
}

当我运行 printCountry 时,它只是搜索列表并打印文件结尾。我在 printCountry 做错了什么?

【问题讨论】:

  • 如果您要发布代码,请尝试将其设为您的真实代码。 (复制/粘贴是当天的顺序)。例如:您将makeList 声明为void makeList(Node *&amp; world),但实现没有引用,只有一个指针类型:void makeList(Node *world)printCountry 也是如此。这将编译,但无法按原样链接。
  • void makeList(Node *&amp;world); 这行之后我没有继续阅读,因为我不明白。为什么要用这么奇怪的参数类型?
  • @tobi303 目的是通过引用更改头指针。如果实际实施的话,它会起作用。
  • @WhozCraig 哦,我明白了。感谢您的澄清。乍一看有点迷茫

标签: c++ struct linked-list ifstream


【解决方案1】:

你真的应该分开 1)数据结构(链表) 2) CSV 解析器 3) 将解析后的数据加载到链表中。

这是一个使用 C# 的示例。

public class LinkedList<T> : IEnumerable<T>
{
    private class Node<T>
    {
        public T Value { get; set; }
        public Node<T> Next { get; set; }
    }

    private Node<T> _startingNode;

    public void Add(T item)
    { 
        //define the next node 
        var nextNode = new Node<T>()
        {
            Value = item
        };

        //add the node to the end
        Node<T> lastNode = this.GetLastNode();
        if (lastNode == null)
        {
            this._startingNode = nextNode;
        }
        else
        {
            lastNode.Next = nextNode;
        }
    }

    public IEnumerator<T> GetEnumerator()
    {
        Node<T> lastNode = this._startingNode;
        while (lastNode != null)
        {
            yield return lastNode.Value;
            if (lastNode.Next == null)
            {
                yield break;
            }
            lastNode = lastNode.Next;
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    private Node<T> GetLastNode()
    {
        var lastNode = this._startingNode;
        while (lastNode != null)
        {
            if (lastNode.Next == null)
            {
                break;
            }
            lastNode = lastNode.Next;
        }
        return lastNode;
    }
}


public class Program
{
    public static void Main()
    {
        //load the list 
        var numberList = new LinkedList<int>();
        numberList.Add(100);
        numberList.Add(200);
        numberList.Add(300);
        numberList.Add(400);


        //do something with it 
        foreach (var item in numberList)
        {
            Console.WriteLine("Item {0}", item);
        }


    }

}

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2013-07-20
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2016-10-23
    • 1970-01-01
    相关资源
    最近更新 更多