[ C++ ] Linked list example


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


struct data
{
    int value;
    data *next;
};


int main()
{
    ifstream in("a.txt");
    data *root = NULL, *now;
    int v;
    while (in >> v)
    {
        cout << "讀取到 " << v << endl;


        data *d = new data;
        d->value = v;
        d->next = NULL;


        if (root == NULL)
            root = d;
        else
            now->next = d;


        now = d;
    }


    now = root;
    while (now != NULL)
    {
        cout << now->value << endl;
        now = now->next;
    }
}




留言

熱門文章