单链接列表的向量的可访问性(或可能是链接列表的链接列表)

时间:2014-03-22 03:20:47

标签: c++ vector linked-list

我整天都在想着在我的一个编程课程中为挑战任务敲定底层数据结构。

问题如下:

鉴于各种物品(每个物品包括标识符和重物)和一批容器(具有固定的重量),使用尽可能少的容器包装所有物品,而不会使任何物品超载。< / p>

我使用数组的大杂烩来解决逻辑问题,但这项任务的动态特性让我想通过使用向量和/或链接列表来优化事物。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <math.h>
#include <time.h>
#include <conio.h>
#include <vector>
#include <algorithm>

using namespace std;

struct Item
{
    int number;
    double weight;

    bool operator < (const Item& str) const
    {
        return (weight < str.weight);
    }
};

class Node
{
    int number;
    double weight;
    Node* next;

  public:
    Node()
    {};

    void SetID(int iNum)
    {
        number = iNum;
    };

    void SetWeight(double iWeight)
    {
        weight = iWeight;
    };

    void SetNext(Node* iNext)
    {
        next = iNext;
    }

    int GetID()
    {
        return number;
    };

    double GetWeight()
    {
        return weight;
    };

    Node* Next()
    {
        return next;
    };
};

class List
{
    Node* head;
    double weight;

  public:
    List()
    {
        head = NULL;
        weight = 0;
    };

    int Size()
    {
        Node* tmp;
        int count = 0;
        for (tmp = head; tmp != NULL; tmp = tmp->Next())
        {
            count++;
        }
        return count;
    };

    double Weight()
    {
        return weight;
    }; 

    void Print()
    {
        Node *tmp = head;
        if ( tmp == NULL )
        {
            cout << "   E M P T Y" << endl;
            return;
        }

        do
        {
            cout << setw(8) << tmp->GetID() << "  |  " << setw(8) << tmp->GetWeight() << endl;
            tmp = tmp->Next();
        } while ( tmp != NULL );
    };

    void Append(int iNum, double iWeight)
    {
        Node* newNode = new Node();
        newNode->SetID(iNum);
        newNode->SetWeight(iWeight);
        newNode->SetNext(NULL);

        Node *tmp = head;
        if ( tmp != NULL )
        {
            while ( tmp->Next() != NULL )
            {
                tmp = tmp->Next();
            }
            tmp->SetNext(newNode);
        }
        else
        {
            head = newNode;
        }
        weight += iWeight;
    };
};

double ItemWeights(vector<Item> iVect) 
{
    double total = 0;
    for(int i = 0; i < iVect.size(); i++)
    {
        total += iVect[i].weight;
    }
    return total;
}


int main()
{
    const double MAX_WEIGHT = 20;
    vector< Item > source;
    //
    //  Segment of code which propagates the vector data
    //  works fine, but is excluded for the sake of brevity
    //  
    double totalWeight = ItemWeights(source);

    //  Duplicate vector of items
    vector< Item > items(source);

    for(int i = 0; i < items.size(); i++)
    {
        cout << setw(8) << items[i].number << setw(8) << items[i].weight << endl;
    }
    cout << "\n Total weight = " << totalWeight << endl;
    cout << "\n\n Press any key to continue... ";
    getch();

//  Solution A-Original
//  vector< vector< Item > > boxesAO( vector< Item >);
//  boxesAO[0].push_back({items[items.size()].number, items[items.size()].weight});

    vector< List > boxesAO;

//  boxesAO[0].Append(items[items.size()].number, items[items.size()].weight);


    return 0;
}

我已经离开了我在代码中尝试过的一些方法(注释掉了) - 其中没有一种方法可行。正如我上面提到的,我已经使用了链接列表和2D数组的数组,但是大量的潜在输入使得这些问题最多。一堆空列表占用空间,或者更糟糕的是,没有足够的空间。

我认为这个载体&lt;列表&gt;是我最好的选择,但我无法弄清楚我应该如何访问任何List功能。

如果有人能够提供有关如何创建动态2D阵列的建议,那么#34;以及如何访问它的代码示例,我将非常感激。我提前致以最深切的谢意。

编辑:

@ jaredad7~那是我一直在尝试的,但它一直导致程序崩溃。

List box;
box.Append(items[items.size()].number, items[items.size()].weight);

这很好 - 没有任何问题。

早期代码传播Item结构的一维向量,它也可以正常工作。

vector< List > boxes;
boxes[0].Append(items[items.size()].number, items[items.size()].weight);

无论使用什么索引,这都可以很好地编译但在执行期间崩溃。 (我也使用couts进行调试,问题最主要在于尝试访问List函数。)

我怀疑.push_back或其他某些东西可能需要,但我还没有找到关于List对象矢量的大量信息。

1 个答案:

答案 0 :(得分:0)

如果可以的话,我的第一个建议就是使用矢量(如果允许的话)。至于访问向量成员的函数/属性,它的工作方式与数组相同,即:

vectorname[i].functionname(a,b,c);

在没有向量的情况下执行此操作的最佳方法是将节点用作项容器(结构),并在列表类中处理节点创建,删除等。然后,根据需要,您只需要一个容器来容纳一种类型的对象。您可以通过添加类模板使类型动态化(虽然看起来您只需要此项目的双倍)(如果您不熟悉C ++中的模板,请使用谷歌)。这将允许您的用户为每种类型的数据创建容器(非常像矢量)。