简体   繁体   中英

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:

#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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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