c++ - 在链表中存储数组元素

标签 c++ arrays linked-list

<分区>

这是我在链表中​​附加一个整数的代码。

#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) 的数组,代码应该创建两个链表,每个链表的第一个节点的地址应该存储在一个数组中,以便运行 for 循环将按顺序打印所有链表。 示例:

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

数组的数量和数组中元素的数量是动态的。

最佳答案

那么你应该使用这个二维 vector std::vector< std::vector<int> > vect;

关于c++ - 在链表中存储数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693618/

相关文章:

c - 如何在for循环中连接字符串?

java - 项目超过 700,000 的频率列表(ArrayList 或 LinkedList)

c++ - 在 vs2008 中使用 CurlPP

c++ - 为什么我重新绑定(bind)c++的引用,编译器不报错

c++ - 如何编辑 Qvector 中 Qvector 的内容

c++ - 扫描数字数组并计算范围 (0 - 24)(25 - 49) 等之间的数字的算法

c++ - 如何在不检查每个元素的情况下立即检查结构的零值?

php - 从 2 个数组中获取唯一值

c - 如何解决链表中损坏的指针?

c - 如何将新项目插入到C中的链接列表中(在列表末尾)