首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >一个linkedlist可以存储不同的数据类型吗?

一个linkedlist可以存储不同的数据类型吗?
EN

Stack Overflow用户
提问于 2014-03-23 01:53:57
回答 2查看 13.9K关注 0票数 6

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

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

但是假设我将整数存储在一个链表中的一个节点中,该链表指向另一个持有字符串的节点。首先,这是允许的吗?第二,这又有什么用呢?

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

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

由于LinkedList是随机存储在内存中(使用从一个节点到另一个节点的指针),而不是相邻的数组,这是否会使C++/C等非自动垃圾回收语言在内存分配和释放方面变得更加困难?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-23 02:21:57

,但是假设我将整数存储在链表中的一个节点中,该链表指向另一个持有字符串的节点。首先,这是允许的吗?第二,这又有什么用呢?

是的,只要列表声明为List<Object>List<Serializable>,它就是允许的,String和Integer都可以扩展/实现。

在这种特殊情况下,它不是很有用。但考虑一下List<Vehicle>。它可以存储汽车、自行车、卡车或任何其他类型的车辆的实例。

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

这些文档记录在the javadoc中。我想说的是,添加和迭代可能是最常见的。但是我还没有做任何统计测量。

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

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

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

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

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

票数 5
EN

Stack Overflow用户

发布于 2020-10-26 06:12:25

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

代码语言:javascript
复制
//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

sadf

999

sfa

12

67

23

45.677

12.4356

这就是您可以用来创建链表而不必担心数据类型的方法

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22581348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档