Structure or class which is better for linked list?

To implement a linked list which is better

Structure use

#include <iostream> using namespace std; struct Node { int data; Node* next; }; 

Class usage

 class ListNodeClass { private: ItemType Info; ListNodeClass * Next; public: ListNodeClass(const ItemType & Item, ListNodeClass * NextPtr = NULL): Info(Item), Next(NextPtr) { }; void GetInfo(ItemType & TheInfo) const; friend class ListClass; }; typedef ListNodeClass * ListNodePtr; 

Or their best way to make a linked list in C ++?

+6
source share
2 answers

The only thing that class and struct is different from C ++ is the default interface. if you write:

 struct MyStruct { int a; } 

and

 class MyClass { int a; } 

the only difference is the field a in both cases. The field MyStruct a open, and the field MyClass a is private. Of course, you can manipulate them using public and private keywords in structures and classes.

If you are programming in C ++, you should use classes.

+3
source

A linked list is one thing, and its nodes are another. Nodes are part of a list implementation. They should not be visible in the list interface, so their shape does not matter much. I would do it

 class List { private: struct Node { int data; Node* next; }; public: ... }; 
+2
source

Source: https://habr.com/ru/post/955288/


All Articles