Skip to main content
Typo in markdown
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

Don't use using namespace std;

It's bad practice that can cause a lot of problems that are easy to avoid, most people are used to the prefix, and three letters aren't that hard to type.


Don't use `system("pause")system("pause")

It's not portable and there are better ways to hold the console open.


Prefer nullptr to NULL

NULL is a macro that will be silently converted to 0 whenever possible. nullptr is a language extension and will only work contextually with pointers.


Use proper encapsulation

A linked list should be a class that acts as a container of Nodes. Your Node class should not know how to sort all Nodes. This is the job of the container. Furthermore Nodes are an implementation detail that should not be exposed to the user.

class LinkedList
{
public:
    // user functions should go here
private:
    struct Node
    {
        int value;
        Node* next{ nullptr };
    }
}

A few things to note.

  • I put the public section first. This way anyone reading the code will know what functions and variables are available to them.
  • I used struct for the Node since it represents plain data. It is also easier to use because struct is default public so the LinkedList will be able to access it easily (But since it is properly encapsulated inside the container nothing else can.)
  • I initialized new nodes to nullptr.

Printing and Sorting aren't typically done as member functions. They should be standalone functions that accept a container, or a range of a container to perform their tasks.


Use RAII whenever possible

You shouldn't be manually allocating memory. Especially since you aren't freeing it. Every instance of new should come with an instance of delete but you shouldn't be using them at all.


Prefer '\n' to std::endl

std::endl does more than just move to the next line. It is generally preferred not to use it casually.


Keep working on it. Try to implement the container with some of the member functions found at std::list. Browse The Core Guidelines from time to time. And bring us more code to review.

Don't use using namespace std;

It's bad practice that can cause a lot of problems that are easy to avoid, most people are used to the prefix, and three letters aren't that hard to type.


Don't use `system("pause")

It's not portable and there are better ways to hold the console open.


Prefer nullptr to NULL

NULL is a macro that will be silently converted to 0 whenever possible. nullptr is a language extension and will only work contextually with pointers.


Use proper encapsulation

A linked list should be a class that acts as a container of Nodes. Your Node class should not know how to sort all Nodes. This is the job of the container. Furthermore Nodes are an implementation detail that should not be exposed to the user.

class LinkedList
{
public:
    // user functions should go here
private:
    struct Node
    {
        int value;
        Node* next{ nullptr };
    }
}

A few things to note.

  • I put the public section first. This way anyone reading the code will know what functions and variables are available to them.
  • I used struct for the Node since it represents plain data. It is also easier to use because struct is default public so the LinkedList will be able to access it easily (But since it is properly encapsulated inside the container nothing else can.)
  • I initialized new nodes to nullptr.

Printing and Sorting aren't typically done as member functions. They should be standalone functions that accept a container, or a range of a container to perform their tasks.


Use RAII whenever possible

You shouldn't be manually allocating memory. Especially since you aren't freeing it. Every instance of new should come with an instance of delete but you shouldn't be using them at all.


Prefer '\n' to std::endl

std::endl does more than just move to the next line. It is generally preferred not to use it casually.


Keep working on it. Try to implement the container with some of the member functions found at std::list. Browse The Core Guidelines from time to time. And bring us more code to review.

Don't use using namespace std;

It's bad practice that can cause a lot of problems that are easy to avoid, most people are used to the prefix, and three letters aren't that hard to type.


Don't use system("pause")

It's not portable and there are better ways to hold the console open.


Prefer nullptr to NULL

NULL is a macro that will be silently converted to 0 whenever possible. nullptr is a language extension and will only work contextually with pointers.


Use proper encapsulation

A linked list should be a class that acts as a container of Nodes. Your Node class should not know how to sort all Nodes. This is the job of the container. Furthermore Nodes are an implementation detail that should not be exposed to the user.

class LinkedList
{
public:
    // user functions should go here
private:
    struct Node
    {
        int value;
        Node* next{ nullptr };
    }
}

A few things to note.

  • I put the public section first. This way anyone reading the code will know what functions and variables are available to them.
  • I used struct for the Node since it represents plain data. It is also easier to use because struct is default public so the LinkedList will be able to access it easily (But since it is properly encapsulated inside the container nothing else can.)
  • I initialized new nodes to nullptr.

Printing and Sorting aren't typically done as member functions. They should be standalone functions that accept a container, or a range of a container to perform their tasks.


Use RAII whenever possible

You shouldn't be manually allocating memory. Especially since you aren't freeing it. Every instance of new should come with an instance of delete but you shouldn't be using them at all.


Prefer '\n' to std::endl

std::endl does more than just move to the next line. It is generally preferred not to use it casually.


Keep working on it. Try to implement the container with some of the member functions found at std::list. Browse The Core Guidelines from time to time. And bring us more code to review.

added 31 characters in body
Source Link
Summer
  • 2.4k
  • 2
  • 16
  • 31

Don't use using namespace std;

It's bad practice that can cause a lot of problems that are easy to avoid, most people are used to the prefix, and three letters aren't that hard to type.


Don't use `system("pause")

It's not portable and there are better ways to hold the console open.


Prefer nullptr to NULL

NULL is a macro that will be silently converted to 0 whenever possible. nullptr is a language extension and will only work contextually with pointers.


Use proper encapsulation

A linked list should be a class that acts as a container of Nodes. Your Node class should not know how to sort all Nodes. This is the job of the container. Furthermore Nodes are an implementation detail that should not be exposed to the user.

class LinkedList
{
public:
    // user functions should go here
private:
    struct Node
    {
        int value;
        Node* next{ nullptr };
    }
}

A few things to note.

  • I put the public section first. This way anyone reading the code will know what functions and variables are available to them.
  • I used struct for the Node since it represents plain data. It is also easier to use because struct is default public so the LinkedList will be able to access it easily (But since it is properly encapsulated inside the container nothing else can.)
  • I initialized new nodes to nullptr.

Printing and Sorting aren't typically done as member functions. They should be standalone functions that accept a container, or a range of a container to perform their tasks.


Use RAII whenever possible

You shouldn't be manually allocating memory. Especially since you aren't freeing it. Every instance of new should come with an instance of delete but you shouldn't be using them at all.


Prefer '\n' to std::endl

std::endl does more than just move to the next line. It is generally preferred not to use it casually.


Keep working on it. Try to implement the container with some of the member functions found at std::list. And browseBrowse The Core Guidelines from time to time. And bring us more code to review.

Don't use using namespace std;

It's bad practice that can cause a lot of problems that are easy to avoid, most people are used to the prefix, and three letters aren't that hard to type.


Don't use `system("pause")

It's not portable and there are better ways to hold the console open.


Prefer nullptr to NULL

NULL is a macro that will be silently converted to 0 whenever possible. nullptr is a language extension and will only work contextually with pointers.


Use proper encapsulation

A linked list should be a class that acts as a container of Nodes. Your Node class should not know how to sort all Nodes. This is the job of the container. Furthermore Nodes are an implementation detail that should not be exposed to the user.

class LinkedList
{
public:
    // user functions should go here
private:
    struct Node
    {
        int value;
        Node* next{ nullptr };
    }
}

A few things to note.

  • I put the public section first. This way anyone reading the code will know what functions and variables are available to them.
  • I used struct for the Node since it represents plain data. It is also easier to use because struct is default public so the LinkedList will be able to access it easily (But since it is properly encapsulated inside the container nothing else can.)
  • I initialized new nodes to nullptr.

Printing and Sorting aren't typically done as member functions. They should be standalone functions that accept a container, or a range of a container to perform their tasks.


Use RAII whenever possible

You shouldn't be manually allocating memory. Especially since you aren't freeing it. Every instance of new should come with an instance of delete but you shouldn't be using them at all.


Prefer '\n' to std::endl

std::endl does more than just move to the next line. It is generally preferred not to use it casually.


Keep working on it. Try to implement the container with some of the member functions found at std::list. And browse The Core Guidelines from time to time

Don't use using namespace std;

It's bad practice that can cause a lot of problems that are easy to avoid, most people are used to the prefix, and three letters aren't that hard to type.


Don't use `system("pause")

It's not portable and there are better ways to hold the console open.


Prefer nullptr to NULL

NULL is a macro that will be silently converted to 0 whenever possible. nullptr is a language extension and will only work contextually with pointers.


Use proper encapsulation

A linked list should be a class that acts as a container of Nodes. Your Node class should not know how to sort all Nodes. This is the job of the container. Furthermore Nodes are an implementation detail that should not be exposed to the user.

class LinkedList
{
public:
    // user functions should go here
private:
    struct Node
    {
        int value;
        Node* next{ nullptr };
    }
}

A few things to note.

  • I put the public section first. This way anyone reading the code will know what functions and variables are available to them.
  • I used struct for the Node since it represents plain data. It is also easier to use because struct is default public so the LinkedList will be able to access it easily (But since it is properly encapsulated inside the container nothing else can.)
  • I initialized new nodes to nullptr.

Printing and Sorting aren't typically done as member functions. They should be standalone functions that accept a container, or a range of a container to perform their tasks.


Use RAII whenever possible

You shouldn't be manually allocating memory. Especially since you aren't freeing it. Every instance of new should come with an instance of delete but you shouldn't be using them at all.


Prefer '\n' to std::endl

std::endl does more than just move to the next line. It is generally preferred not to use it casually.


Keep working on it. Try to implement the container with some of the member functions found at std::list. Browse The Core Guidelines from time to time. And bring us more code to review.

Source Link
Summer
  • 2.4k
  • 2
  • 16
  • 31

Don't use using namespace std;

It's bad practice that can cause a lot of problems that are easy to avoid, most people are used to the prefix, and three letters aren't that hard to type.


Don't use `system("pause")

It's not portable and there are better ways to hold the console open.


Prefer nullptr to NULL

NULL is a macro that will be silently converted to 0 whenever possible. nullptr is a language extension and will only work contextually with pointers.


Use proper encapsulation

A linked list should be a class that acts as a container of Nodes. Your Node class should not know how to sort all Nodes. This is the job of the container. Furthermore Nodes are an implementation detail that should not be exposed to the user.

class LinkedList
{
public:
    // user functions should go here
private:
    struct Node
    {
        int value;
        Node* next{ nullptr };
    }
}

A few things to note.

  • I put the public section first. This way anyone reading the code will know what functions and variables are available to them.
  • I used struct for the Node since it represents plain data. It is also easier to use because struct is default public so the LinkedList will be able to access it easily (But since it is properly encapsulated inside the container nothing else can.)
  • I initialized new nodes to nullptr.

Printing and Sorting aren't typically done as member functions. They should be standalone functions that accept a container, or a range of a container to perform their tasks.


Use RAII whenever possible

You shouldn't be manually allocating memory. Especially since you aren't freeing it. Every instance of new should come with an instance of delete but you shouldn't be using them at all.


Prefer '\n' to std::endl

std::endl does more than just move to the next line. It is generally preferred not to use it casually.


Keep working on it. Try to implement the container with some of the member functions found at std::list. And browse The Core Guidelines from time to time