首页 文章

在链表中存储数组元素[关闭]

提问于
浏览
0

这是我在链表中附加整数的代码 .

#include "iostream"
using namespace std;

class LinkList
{
private:
      struct Node
      {
        int data;
        Node* link;
      }*p;

public:
      LinkList();
      ~LinkList();

       void Print();         // Prints the contents of linkedlist
       void Append(int num); // Adds a new node at the end of the linkedlist
       void Delete(int num); // Deletes the specified node from the linkedlist

       void AddatBeg(int num);// Adds a new node at the beginning of the linkedlist
       void AddAfter(int c, int num); // Adds a new node after specified number of nodes
       int Count();          // Counts number of nodes present in the linkedlist

};

LinkList::LinkList()
{
  p = NULL;
}

LinkList::~LinkList()
{
  if (p == NULL)
        return;

  Node* tmp;
  while(p != NULL)
  {
       tmp = p->link ;
     delete p;
       p = tmp;
  }
}

// Prints the contents of linkedlist
void LinkList::Print()
{
  if (p == NULL)
  {
        cout<< "EMPTY";
        return;
  }

  //Traverse
  Node* tmp = p;
  while(tmp != NULL)
  {
       cout<<tmp->data<<endl;
       tmp = tmp->link ;
  }
}

// Adds a new node at the end of the linkedlist
void LinkList::Append(int num)
{
      Node *newNode;

      newNode = new Node;
      newNode->data = num;
      newNode->link = NULL;

       if(p == NULL)
      {
       //create first node
         p = newNode;
      }
       else
      {
             //Traverse
            Node *tmp = p;
             while(tmp->link != NULL)
            {
                  tmp = tmp->link;
            }

             //add node to the end
        tmp->link = newNode;
      }
}

// Deletes the specified node from the linkedlist
void LinkList::Delete( int num )
{
   Node *tmp;

   tmp = p;
   //If node to be delete is first node
   if( tmp->data == num )
   {
      p = tmp->link;
      delete tmp;
      return;
   }

   // traverse list till the last but one node is reached
   Node *tmp2 = tmp;
   while( tmp!=NULL )
   {
      if( tmp->data == num )
      {
         tmp2->link = tmp->link;
         delete tmp;
         return;
      }

      tmp2 = tmp;
      tmp = tmp->link;
   }
   cout<< "\nElement "<<num<<" not Found." ;
}

int LinkList::Count()
{
      Node *tmp;
       int c = 0;

       //Traverse the entire Linked List
       for (tmp = p; tmp != NULL; tmp = tmp->link)
            c++;

       return (c);
}

int main()
{
      LinkList* pobj = new LinkList();
      pobj->Append(11);
      pobj->Print();

       delete pobj;
       return 0;
}

我要找的是一个代码,我可以在链表中插入数组元素 . 例如,如果有两个包含元素(1,2,3)和(4,5,6)的数组,则Code应创建两个Linked列表,并且每个链表的第一个节点的地址应存储在一个数组中,以便运行for循环将按顺序打印所有链表 . 例:

Linked List 1 = (1,2,3)
Linked List 2 = (4,5,6)

数组中的数组数和元素数将是动态的 .

2 回答

  • 2

    然后你应该使用这个2D矢量 std::vector< std::vector<int> > vect;

  • 2

    NOTE: 我想你的问题是出于学习目的 . 如果没有,请不要实现自己的链表,请使用 std::vector . 首选向量到 std::list ,链接列表的性能低于向量,因为缓存未命中 . 检查this question about performance comparisons between std::list and syd::vector .

    链接列表通常实现为 double-linked lists ,以使 push_backpop_back 操作O(1)(即,push_back不需要遍历) .

    实现此目的的方法是在节点中存储不仅指向下一个节点的链接,还指向前一个节点的链接:

    struct node
    {
        node* previous;
        node* next;
        int data;
    };
    

    除了起始节点之外,链表类还存储表示列表末尾的节点:

    node* begin;
    node* end;
    

    请注意,我说 "A node that represents the end of the list" ,而不是"the last node of the list" . 也就是说, end->previous 指向列表的最后一个节点 . end->next 指向任何东西(nullptr) . Check this picture .

    因此,通过这种方式, push_back 实现仅将 end->preious 指向新节点( new_node->next 指向 end ),并且 new_node->previous 指向插入后的 end->previous 的值 .

相关问题