3
\$\begingroup\$

Note: Not a native English speaker.

I have attempted to make a class similar to vectors in c++ (Though, I now know that it's just a (doubly) linked list). Just wanna know, how good or efficient my program is, how well I am as a programmer. What about my coding style. Is it messy to read? constructive criticism is welcomed and appreciated.

#include <iostream>

struct node
{
    int num;
    node* next = NULL;
    node* prev = NULL;
};

class _vector
{
    node *n1 = new node();
    node *n = n1;
public:
    void push_back(int num)
    {
        node* temp = new node();

        this->n1->num = num;
        this->n1->next = temp;
        temp->prev = n1;
        this->n1 = temp;
    }

    void pop_back()
    {
        n1 = n1->prev;
        n1->next = NULL;
    }

    void display()
    {
        while(n->next != NULL)
        {
            std::cout<<n->num<<'\n';
            n = n->next;
        }
    }
};

int main()
{
    _vector v;

    for(int i=0;i<100;i++)
        v.push_back(i);

    v.pop_back();
    v.pop_back();
    v.pop_back();
    v.pop_back();
    v.pop_back();

    v.display();

    return 0;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ I'd recommend reading through the countless linked-list posts on this site. You're bound to learn a lot from them. \$\endgroup\$ – yuri May 29 at 21:17
  • \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. Please see what you may and may not do after receiving answers. Modifying the code after it has been reviewed may lead to multiple reviews of multiple versions of the same code in the same question. That gets terribly messy very fast. Feel free to post a follow-up question if you've improved your original code in a significant manner. \$\endgroup\$ – Mast May 30 at 11:43
  • \$\begingroup\$ Okay thanks. I was not aware of that. But I made a very minor change. Everyone still be able to follow. \$\endgroup\$ – vikram May 30 at 13:54
7
\$\begingroup\$

If we continue with the idea that this is a doubly linked list rather than a vector, I'd say your push_back and pop_back are a good start. Your Struct is also perfect for the groundwork of a doubly linked list.

However, on that note, you're missing a few operations for this data structure to be considered a proper doubly linked list.

A doubly linked list should provide these operations (to the best of my memory):

  • Insert (front/back/after index)
  • Delete (front/back/at index)
  • Traverse

Currently you've only implemented insert and delete from the back. As for traversal, I could see display being a traversal in a sense, but I'd argue that displaying is not an operation strictly inherent to data structures. I would also like to point out that your current implementation of display would not allow you to traverse and display the list more than once.

Overall though, I'd like to point out again that you have a very strong start. It's easy enough to provide alternative push_front, push_after, pop_front, and pop functions from what you have; and I'd recommend changing display to some sort of traversal method instead (maybe an iterator pattern) and move the displaying to your main function all together.

\$\endgroup\$
  • \$\begingroup\$ Another alternative to display is an overloaded operator<< friend that allows the linked list to simply be fed to an output stream (e.g., cout). \$\endgroup\$ – AleksandrH May 29 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.