修改链接列表,以便每个元素可以存储指向另一个链接列表的指针

时间:2017-07-24 21:05:05

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

我已经实现了链接列表类但是我不确定如何将其指向另一个链接列表。我想拥有它,以便在同一个类中有3个指针。一个用于数据,一个用于下一个,最后一个用于另一个链接列表。

有人能指出我正确的方向吗?

标题文件

class Node{

public:
    typedef int value_type;
    //COSTRUCTOR- get two values link and positive integer
    Node(const value_type& i_data = value_type(), Node *i_link= nullptr) //value_type()=0 //Constructor
    {
        this->data_field = i_data;
        this->link_field= i_link;
    }
    //Member functions to set the data and next;
    void set_data(const value_type& new_link);
    void set_next(Node *new_next);
    //Constant member function to retrieve the current data:
    value_type get_data()const;
    //Two slightly different members functions te retrieve current link
    Node* get_next();
    const Node* get_next() const;
    //friend std::ostream& operator<< (std::ostream& os, Node& p);
private:

    Node* link_field;
    value_type data_field;

};
size_t LinkedList_size(const Node* headptr);
void LinkedListInsertAtHead( Node*& headptr, const Node::value_type& entry);
//   LinkedList_insert_at(list,input, 99);
void LinkedList_insert_at( Node*& headptr, int index, const Node::value_type& entry);
void LinkedList_delete( Node*& headptr);
void LinkedList_delete_from(Node*& headptr,int index);
void LinkedList_print(const Node* headptr);

实施档案

#include <iostream>
#include <map>
#include "node.h"
// Node(const value_type& data, Node* next);//consturctor

void Node::set_data(const value_type& new_data) { this-> data_field = new_data; }
void Node::set_next(Node* new_next)             { this-> link_field = new_next; }
Node::value_type Node::get_data() const      { return this-> data_field; }
           Node* Node::get_next()            { return this-> link_field; }
     const Node* Node::get_next() const      { return this-> link_field; }

size_t LinkedList_size(const Node* headptr) //point to a const node
{
    ///  Node* = {[n1] [n2] [n3] [n4] [n5] [n5] } ,@t first: headptr = n1;
    ///  curr = n1;    curr = headptr
    ///  curr = n1->n2,  curr= curr-> headptr
    size_t count = 0;
    for(const Node* curr = headptr; curr != nullptr; curr = curr->get_next())
    {
    count++;
    }
    return count;
}
void LinkedListInsertAtHead( Node*& headptr, const Node::value_type& entry)
{
    //0->0->0->null
    headptr = new Node(entry, headptr);//inserts and return a new address

}

void LinkedList_insert_at( Node*& headptr, int index, const Node::value_type& entry)
//0->O->O ->O
//insert_at(0):   N->->O->O ->O
//insert_at(1):   O->O->O->O ->O
//insert_at(n=4): O->O->O->O ->N
{
    if(index==0){
        LinkedListInsertAtHead(headptr, entry);
        return;
    }
    Node* node_before = nullptr;
    Node* node_after = headptr;//->get_next();

    for(int i = 0; i < index; ++i)
    {
        node_before = node_after;

        node_after = node_after->get_next();
    }

    Node* insert_node= new Node(entry, node_after);

    node_before->set_next(insert_node);

}
void LinkedList_delete( Node*& headprt)
// 36[data] 46[dat] 94[data] [] [] [] []
{
    Node* curr = headprt; //delete [data] and address"36"

    headprt = headprt->get_next();//assign to ->46[data]
    delete curr;

}

void LinkedList_delete_from(Node*& headptr,int index)
{
    if(index==0)
    {
    LinkedList_delete(headptr);
    return;
    }

    Node* node_before = nullptr;
    Node* curr = headptr;//->get_next();  //curr short for current

    for(int i=0; i<index; ++i)
    {
    node_before = curr;
    curr = curr->get_next();
    }

    node_before->set_next(curr->get_next());

    delete curr;

}

void LinkedList_print(const Node* headptr)
{
/*    THIS  DOES THE SAME;
    const Node* temp = headptr;
    while(temp!= nullptr)
    {
        //cout insertion operator

       std::cout << temp->get_data() <<" ";
        temp = tempp->get_next();///will connect to next data []->get_next(return this->[next])->[]->nullptr

    }
   */
while(headptr != nullptr)
{

    std::cout << headptr->get_data()<<" ";
    headptr = headptr->get_next();///will connect to next data []->get_next(return this->[next])->[]->nullptr

}

std::cout<<std::endl;//to skip a line

}

1 个答案:

答案 0 :(得分:0)

也许你的问题是前瞻声明:

// Forward declarations
class Linked_List;
class Data;

class Node
{
  Linked_List * p_linked_list;
  Data *        p_data;
  Node *        p_next;
};

方法实现需要DataLinked_List的定义。