【问题标题】:Creating a linked list without pointers but indices [duplicate]创建没有指针但索引的链表[重复]
【发布时间】:2017-07-01 09:40:31
【问题描述】:

我的老师希望我创建类似链表的东西,但使用索引而不是指针。所以Node 包含int dataint index

你能告诉我我该怎么做吗?我知道如何用指针创建它,但没有它们怎么办?不过,他提到池是一个容器。

【问题讨论】:

  • 您在 C 中使用链表,在 C++ 中您只需使用 std::map
  • 数组并使用数组索引作为链接?
  • 你可以使用std::vector,但它不会是链表
  • @Havenard,链表和std::map是不同的结构,不能用另一个替换其中一个

标签: c++ data-structures linked-list


【解决方案1】:

你的结构 Node 会是这样的

struct Node {
    int data;
    int index; // index of the entry in the pool to the next node
}

您可以使用vector 或数组来创建池

vector<Node*> pool; // stores the pointer to next nodes

现在可以访问下一个节点了

Node* nextNode = pool[currNode->index];

【讨论】:

    【解决方案2】:

    一种可能的方法是使用Nodes 的数组,其中每个节点存储prevnextNode 的(数组)索引。因此,您的 Node 看起来像:

    struct Node 
    {
        T data;
        int prev;    // index of the previous Node of the list
        int next;    // index of the next Node of the list
    }
    

    此外,您可能必须动态分配 Node 数组,对数组中的 getfree 空间进行一些记账:例如 bool将未占用索引存储在Node 数组中的数组,以及两个函数,每次添加或删除新的Node / 索引时都会更新它(它将被分段,因为节点并不总是连续的);在数组中找到Node 的索引:例如,从数组的第一个地址中减去Node 的地址。

    以下是使用上述技术的双向链表的可能接口如下所示:

    template <typename T>                          // T type Node in your case
    class DList
    {
        T** head;                                  // array of pointers of type Node
        int first;                                 // index of first Node
        int last;                                  // index of last Node
    
        bool* available;                           // array of available indexes 
        int list_size;                             // size of list
    
        int get_index();                           // search for index with value true in bool available
        void return_index(int i);                  // mark index as free in bool available
    
        std::ptrdiff_t link_index(T*& l) const     // get index of Node
    
        void init();                               // initialize data members
        void create();                             // allocate arrays: head and available
    
        void clear();                              // delete array elements
        void destroy();                            // delete head
    
    public:
        DList();                                   // call create() and init()
        ~DList();                                  // call clear() and destroy()
    
        void push_back(T* l);
        void push_front(T* l);
        void insert(T*& ref, T* l);                // insert l before ref
    
        T* erase(T* l);                            // erase current (i.e. l) and return next
        T* advance(T* l, int n);                   // return n-th link before or after currnet
    
        std::size_t size() const;
        int capacity () const { return list_size; }
    };
    

    您可以将其用作基准并自行实施。

    template <typename T>
    void DList<T>::push_back(T* l)
    {
        if (l == nullptr)
        {
            throw std::invalid_argument("Dlist::push_back()::Null pointer as argument!\n");
        }
    
        int index = get_index();
        head[index] = l;
    
        if (last != -1)
        {
            head[last]->next = index;
            head[index]->prev = last;
        }
        else
        {
            first = index;
            head[index]->prev = -1;
        }
    
        last = index;
        head[index]->next = -1;
    }
    

    【讨论】:

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