简体   繁体   English

是否可以创建一个可以容纳指针而不是整数或字符串的链表?

[英]Is it possible to create a linked list that can hold pointers instead of integers or string?

I'm new to programming but I have a question about linked lists. 我是编程新手,但对链表有疑问。 I am creating a double linked list and I want my nodes to store pointers not just strings or ints. 我正在创建一个双向链接列表,我希望节点存储的指针不只是字符串或整数。 Would code like this work? 这样的代码会工作吗?

template <typename T>
class LinkedList
{
private:
    struct Node {
        string name;
        int age;
        int location;
        Node* next;
        Node* previous;
        Node(T info){ name = info->getName(); age = info->getAge; location = info->getLocation; next = NULL; previous = NULL; }


    };
    Node* head = NULL;
    Node* tail = NULL;

This is assuming that (T info) is a pointer to a class object that has been created from: 假设(T info)是指向从以下对象创建的类对象的指针:

#include "Person.h"


Person::Person(string name, int age, int location)
{
    this->name = name;
    this->age = age;
    this->location = location;
}


Person::~Person()
{
}

string Person::getName()
{
    return name;
}

int Person::getAge()
{
    return age;
}

int Person::getLocation()
{
    return location;
}

I want to be able to create a Person from my person class and then store that person (name, age, and location) in my linked list: 我希望能够从我的人员类创建一个人员,然后将该人员(姓名,年龄和位置)存储在我的链接列表中:

Person* newPerson = new Person(name, age, location);
LL.insert(newPerson);

LL is a linked list I created inside my main.cpp and insert is a function of my linked list. LL是我在main.cpp中创建的一个链表,而insert是我的链表的功能。

Maybe this isn't the best way to do this but can I do this? 也许这不是执行此操作的最佳方法,但是我可以执行此操作吗?

A linked list is a data structure that defines a certain relationship between multiple items of data. 链表是一种数据结构,定义了多个数据项之间的某种关系。

That data is usually strings, or ints, as you noted. 如您所述,该数据通常是字符串或整数。 But it can be anything. 但这可以是任何东西。

You can think of a link list as a bookshelf. 您可以将链接列表视为书架。 You typically put books on a bookshelf. 您通常将书放在书架上。 But you can also put your shoes up there, and the shoes will sit on the bookshelf the same way as books do. 但是,您也可以将鞋子放在那里,鞋子将以与书本相同的方式坐在书架上。

Yes you can hold pointers to other objects. 是的,您可以保留指向其他对象的指针。 In your case a person object. 在您的情况下,一个人反对。

You need a new class or struct, then create a pointer to that. 您需要一个新的类或结构,然后创建一个指向它的指针。

class LinkedList
{
    private:
    struct Node {
        Person *person;
        Node *next;
    };
    Node* head = NULL;
    Node* tail = NULL;
};

Alternately you can use a linked list of void pointers 或者,您可以使用无效指针的链接列表

class LinkedList
{
    private:
    struct Node {
        void *data;
        Node *next;
    };
    Node* head = NULL;
    Node* tail = NULL;
};

Using a templated type you can ensure that the contents are all of the same type. 使用模板化的类型,您可以确保内容都是相同的类型。

template <typename T>
class LinkedList
{
       private:
       struct Node {
          T    *data; 
          Node *next;
       };
    Node* head = NULL;
    Node* tail = NULL;
};

I don't have access to aa C++ compiler at this moment so the syntax may need correction. 我目前无法使用C ++编译器,因此语法可能需要更正。

By the way use of a tail member seems to imply you're trying for a doubly linked list. 顺便说一句,使用尾部成员似乎意味着您正在尝试使用双向链表。 If you don't need to go backwards in the list this isn't necessary. 如果您不需要在列表中倒退,则没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM