在链表中搜索数据

时间:2016-04-21 18:27:05

标签: c++ struct linked-list ifstream

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

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中做错了什么?

1 个答案:

答案 0 :(得分:0)

你应该真的分开 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);
        }


    }

}