【问题标题】:Can a linkedlist store different data types?链表可以存储不同的数据类型吗?
【发布时间】:2014-03-22 17:53:56
【问题描述】:

我的问题很笼统。我刚开始研究数据结构,我是通过链表来的。我知道它们是一系列节点。每个节点都可以存储一些数据,并且它知道列表中的下一个节点。

所以一个节点有一个对象 O 和一个指向下一个称为对象 B 的对象的指针,该对象 B 又具有另一个指针 .. 直到我们到达具有指向 null 的指针的节点。

但是假设我将整数存储在链接列表中的一个节点中,该链接列表指向另一个持有字符串的节点。首先,是否允许?其次,这有什么用处?

另外,链表上最常见的操作是什么? getSize()remove()insert()getElement()concatenate()

如果我要存储一百万个手机号码,使用链表是否有效?如果没有,链表的最佳使用会出现在哪里?

由于LinkedList 是随机存储在内存中的(使用从一个节点到另一个节点的指针),不像相邻的数组,这是否会使像C++/C 这样的非自动垃圾收集语言在内存分配和有空吗?

【问题讨论】:

    标签: java memory-management data-structures types linked-list


    【解决方案1】:

    但是假设我将整数存储在链接列表中的一个节点中,该链接列表指向另一个持有字符串的节点。首先,是否允许?其次,这有什么用处?

    是的,只要将列表声明为List<Object>List<Serializable>(String 和Integer 都扩展/实现),就允许这样做。

    在这种特殊情况下,它不会很有用。但是考虑List<Vehicle>。它可以存储 Car、Bike、Truck 或任何其他类型的 Vehicle 的实例。

    链表上最常见的操作是什么?

    the javadoc 中记录的那些。我会说添加和迭代可能是最常见的。但我没有进行任何统计测量。

    如果我要存储一百万个手机号码,使用链表是否有效?

    这取决于您需要对列表进行的操作。如果您只需要在开头或结尾添加,它将是 O(1)。迭代不是问题。找到电话的索引将是 O(n)。在列表中的给定索引处访问也将是 O(n)。

    一般来说,对于几乎所有用例,ArrayList 都比 LinkedList 快得多。 LinkedList 更快的唯一用例是它总是在 biginning 时插入,或者在使用 Iterator 迭代时删除/插入元素。

    这是否会使 C++/C 等非自动垃圾收集语言在内存分配和释放方面变得更加困难?

    我没有足够的这些语言经验来回答,但是是的,因为你必须管理记忆,所以更难。

    【讨论】:

      【解决方案2】:

      是的,当然根据问题的标题,答案非常简单易行。 您可以在我设计的链表中插入任何数据类型值,并且这样做非常简单。我使用节点和布尔变量的不同构造函数来检查插入了哪个类型值,然后我根据操作和命令我的程序中的那个值。

      //IMPLEMENTATION OF SINGLY LINKED LISTS
      #include"iostream"
      #include"conio.h"
      #include <typeinfo>
      using namespace  std;
      
      
      
      class node //struct
      {
      public:
          node* nextptr;
          int data;
          ////////////////////////////////just to asure that user can insert any data type value in the linked list
          string ss;
          char cc;
          double dd;
          bool stringTrue=0;
          bool intTrue = 0;
          bool charTrue = 0;
          bool doubleTrue = 0;
          ////////////////////////////////just to asure that user can insert any data type value in the linked list
          node()
          {
              nextptr = NULL;
          }
          node(int d)
          {
              data = d;
              nextptr = NULL;
              intTrue = 1;
          }
          ////////////////////////////////just to asure that user can insert any data type value in the linked list
          node(string s)
          {
              stringTrue = 1;
              ss = s;
              nextptr = NULL;
          }
          node(char c)
          {
              charTrue = 1;
              cc = c;
              nextptr = NULL;
          }
          node(double d)
          {
              doubleTrue = 1;
              dd = d;
              nextptr = NULL;
          }
          ////////////////////////////////just to asure that user can insert any data type value in the linked list
          //TO Get the data
          int getintData()
          {
              return data;
          }
          string getstringData()
          {
              return ss;
          }
          double getdoubleData()
          {
              return dd;
          }
          char getcharData()
          {
              return cc;
          }
          //TO Set the data
          void setintData(int d)
          {
              data = d;
          }
          void setstringData(string s)
          {
              ss = s;
          }
          void setdoubleData(double d)
          {
              dd = d;
          }
          void setcharData(char c)
          {
              cc = c;
          }
          char checkWhichInput()
          {
              if (intTrue == 1)
              {
                  return 'i';
              }
              else if (stringTrue == 1)
              {
                  return 's';
              }
              else if (doubleTrue == 1)
              {
                  return 'd';
              }
              else if (charTrue == 1)
              {
                  return 'c';
              }
          }
          //////////////////////////////Just for the sake of implementing for any data type//////////////////////////////
          node* getNextptr()
          {
              return nextptr;
          }
          void setnextptr(node* nptr)
          {
              nextptr = nptr;
          }
      };
      
      
      class linkedlist
      {
          node* headptr;
          node* addnodeatspecificpoition;
          
      public:
          linkedlist()
          {
              headptr = NULL;
          }
          void insertionAtTail(node* n)
          {
              if (headptr == NULL)
              {
                  headptr = n;
              }
              else
              {
                  node* rptr = headptr;
                  while (rptr->getNextptr() != NULL)
                  {
                      rptr = rptr->getNextptr();
                  }
                  rptr->setnextptr(n);
              }
          }
      
          void insertionAtHead(node *n)
          {
              node* tmp = n;
              tmp->setnextptr(headptr);
              headptr = tmp;
          }
      
          int sizeOfLinkedList()
          {
              int i = 1;
              node* ptr = headptr;
              while (ptr->getNextptr() != NULL)
              {
                  ++i;
                  ptr = ptr->getNextptr();
              }
              return i;
          }
      
          bool isListEmpty() {
              if (sizeOfLinkedList() <= 1)
              {
                  return true;
              }
              else 
              {
                  false;
              }
          }
      
          void insertionAtAnyPoint(node* n, int position)
          {
              if (position > sizeOfLinkedList() || position < 1) {
                  cout << "\n\nInvalid insertion at index :" << position;
                  cout <<".There is no index " << position << " in the linked list.ERROR.\n\n";
                  return;
              }
      
              addnodeatspecificpoition = new node;
              addnodeatspecificpoition = n;
              addnodeatspecificpoition->setnextptr(NULL);
      
              
      
              if (headptr == NULL)
              {
                  headptr = addnodeatspecificpoition;
              }
              else if (position == 0)
              {
                  addnodeatspecificpoition->setnextptr(headptr);
                  headptr = addnodeatspecificpoition;
              }
              else
              {
                  node* current = headptr;
                  int i = 1;
                  for (i = 1; current != NULL; i++)
                  {
                      if (i == position)
                      {
                          addnodeatspecificpoition->setnextptr(current->getNextptr());
                          current->setnextptr(addnodeatspecificpoition);
                          break;
                      }
                      current = current->getNextptr();
                  }
              }
          }
      
       
          friend ostream& operator<<(ostream& output,const linkedlist& L)
          {
              char checkWhatInput;
              int i = 1;
              node* ptr = L.headptr;
              while (ptr->getNextptr() != NULL)
              {
                  ++i;
                  checkWhatInput = ptr->checkWhichInput();
                  /// <summary>
                  switch (checkWhatInput)
                  {
                  case 'i':output <<ptr->getintData()<<endl;
                      break;
                  case 's':output << ptr->getstringData()<<endl;
                      break;
                  case 'd':output << ptr->getdoubleData() << endl;
                      break;
                  case 'c':output << ptr->getcharData() << endl;
                      break;
                  default:
                      break;
                  }
                  /// </summary>
                  /// <param name="output"></param>
                  /// <param name="L"></param>
                  /// <returns></returns>
                  
                  ptr = ptr->getNextptr();
              }
      
              /// <summary>
              switch (checkWhatInput)
              {
              case 'i':output << ptr->getintData() << endl;
                  break;
              case 's':output << ptr->getstringData() << endl;
                  break;
              case 'd':output << ptr->getdoubleData() << endl;
                  break;
              case 'c':output << ptr->getcharData() << endl;
                  break;
              default:
                  break;
              }
              /// </summary>
              /// <param name="output"></param>
              /// <param name="L"></param>
              /// <returns></returns>
      
              if (ptr->getNextptr() == NULL)
              {
                  output << "\nNULL (There is no pointer left)\n";
              }
              return output;
          }
          ~linkedlist() {
              delete addnodeatspecificpoition;
          }
      };
      
      
      
      int main()
      {
      
          linkedlist L1;
      
      
          //Insertion at tail
          L1.insertionAtTail(new node("dsaf"));
          L1.insertionAtTail(new node("sadf"));
          L1.insertionAtTail(new node("sfa"));
          L1.insertionAtTail(new node(12));
          L1.insertionAtTail(new node(67));
          L1.insertionAtTail(new node(23));
          L1.insertionAtTail(new node(45.677));
          L1.insertionAtTail(new node(12.43556));
      
      
          //Inserting a node at head
          L1.insertionAtHead(new node(1));
          //Inserting a node at any given point
          L1.insertionAtAnyPoint(new node(999), 3);
      
          cout << L1;
      
          cout << "\nThe size of linked list after insertion of elements is : " << L1.sizeOfLinkedList();    
      }
      
      

      输出是
      1
      dsaf
      萨夫
      999
      sfa
      12
      67
      23
      45.677
      12.4356
      这就是你可以用来创建链表而不用担心数据类型的方法

      【讨论】:

        猜你喜欢
        • 2021-08-16
        • 1970-01-01
        • 2019-01-14
        • 1970-01-01
        • 2019-06-24
        • 1970-01-01
        • 1970-01-01
        • 2016-09-05
        • 2017-04-30
        相关资源
        最近更新 更多