【问题标题】:Accessibility for Vectors of Singly Linked Lists (or possibly a Linked List of Linked Lists)单链表(或可能是链表的链表)向量的可访问性
【发布时间】:2014-03-22 03:20:46
【问题描述】:

我整天都在绞尽脑汁,试图为我的一门编程课程中的一项挑战任务敲定底层数据结构。

问题如下:

给定各种各样的物品(每个物品都包括一个标识符和一个重量)和一些容器(具有固定的重量容量),使用尽可能少的容器包装所有物品,而不会使任何一个超载。

我已经使用大杂烩的数组敲定了逻辑方面,但是这个分配的动态特性让我想通过使用向量和/或链表来优化事物。

#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;
}

我已经在代码中留下了一些我尝试过的方法(已注释掉)——这些方法都不起作用。正如我上面提到的,我已经让它与链表数组和二维数组一起工作,但大量的潜在输入最多只能使这些问题成为问题。要么是一堆空列表占用空间,要么更糟糕的是,没有足够的空间。

我认为 vector 是我的最佳选择,但我不知道应该如何访问任何 List 功能。

如果有人能提供有关如何创建“动态二维数组”的建议以及如何访问它的代码示例,我将不胜感激。提前致以最诚挚的谢意。

编辑:

@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 对象向量的太多信息。

【问题讨论】:

    标签: c++ vector linked-list


    【解决方案1】:

    如果可以,我的第一个建议是使用矢量(如果允许的话)。至于访问vector成员的函数/属性,和数组一样,也就是:

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

    不使用向量的最佳方法是将节点用作项目容器(结构),并在列表类中处理节点创建、删除等。然后,您实际上只需要一个容器来容纳您需要的任意数量的一种类型的对象。您可以通过添加类模板(如果您不熟悉 C++ 中的模板,请使用 google)使类型动态化(尽管看起来您只需要该项目的双打)。这将允许您的用户为每种类型的数据创建一个容器(很像矢量)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多