简体   繁体   中英

C++ new pointer from pointer to pointer?

I have a template linkedList that I would like to dynamically create "head" pointers for...

I seem unable to get any syntax to work.. my best guess is:

linkedList<int>** ptr;
ptr = new (linkedList<int>*)[1];

But it doesn't work. I'm fairly new to C++ so any help is appreciated! Thanks!

To get a pointer, do:

T* ptr = new T;

where T is your type.
For a pointer-to-pointer, do:

T** ptrptr = new T*;

allocating the space for one pointer, which still needs to be filled like the first method:

*ptrptr = new T;

Now you got a valid pointer-to-pointer.

Is there some reason you are not using std::list? (or std::forward_list)

Check out the header files for std::list, or your nearest C++ book, or in fact cppreference.com

Your linked list class template should have a function to return the head of the list. Look at std::list::begin() in your compiler's c++ library. The std::list::iterator type is a pointer to whatever goes in the list. (ie T*)

Though I'm not sure pointer array is really needed for your linked list, as for just new construct, the following form will be compiled.

ptr = new (linkedList<int>*[1]);

EDIT :
This allocates pointer array:

linkedList<int>** ptr = new (linkedList<int>*[1]);

This allocates array:

linkedList<int>* ptr = new linkedList<int>[1];

This allocates one element:

linkedList<int>* ptr = new linkedList<int>;

Normally the head of a linked list would look something like:

node<int> *head = NULL;

When you want to create and insert a node, you'd use something like:

insert(node<int> *&list, int value) { 
// insert new node containing `value` at head of `list`.

    node<int> *temp = new node(value);

    temp->next = list;
    list=temp;
}

You could use this something like:

node<int> *list = NULL;

for (int i=0; i<10; i++)
    insert(list, i);

Of course, unless this is for homework (or something on that order), you should stop working on this immediately, and just std::list (or boost::slist , if you want a singly-linked list).

Edit: I'm adding more detail mentioned by the OP in comment. For the moment, the avl_tree::insert does not attempt to maintain balance. It's just a plain-jane un-balanced insert, but it should be adequate to demonstrate what we care about at the moment.

template <class T>
struct linked_list { 
    node *head;

    void insert(T v) {
        node<T> *n = new node(v, head);
        head = n;
    }

    linked_list() : head(NULL) {}

    template <class T>
    struct node { 
        node *next;
        T data;

        node(T const &v, node *n=NULL) : data(v), next(n) {}
    };
};

template <class keyType, class dataType>
class avl_tree { 
    struct node {
        node *left, *right;
        char balance;
        keyType key;
        dataType data;

        node(keyType const &k, dataType const &d) 
            : left(NULL), right(NULL), balance(0), key(k), data(d)
        { }

        bool operator<(node const &other) { 
            return key < other.key;
        }
    } *root;

    bool insert(node const *new_node, node *&tree) { 
        if (tree == NULL) {
            tree = new_node;
            return true;
        }
        else if (*new_node < *tree)
            return insert(new_node, tree->left);
        else if (*new_node > *tree)
            return insert(new_node, tree->right);
        else // new_node == tree
            return false; // couldn't insert -- already present.
    }

public:

    avl_tree() : root(NULL) {}

    void insert(keyType const &key, dataType const &data) { 
        node *temp = new node(key, data);
        insert(temp, root);
    }
};

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