Query regarding header file

till now we are writing code for singly linked list by scratch by defining the object and creating our own functions, so as the header file for doubly linked list is #include so what is the header file for singly linked list and how to use it and to define our own functions when using the c++ stl.

c++ stl does have container for single linked list , it is called as
forward_list and is present in #include <forward_list>
functionalities are somewhat same as offered by stl::list but differs in time complexity
and coming to your question on how to define our functions when using c++ stl , the thing is , stl offers very less room for customization from the user side if you want to add some function then it is recommended to define linked list from scrach but if you want use standard functionalities of list or any container then using stl is recommended.

note :

  1. forward_list is only available on c++ 11 or higher
  2. forward_list is very unlikely to be used in any algorithm as it is slow compared to its counterpart i,e stl :: list which is present in #include <list> (doubly linked list)

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer

So from that i am getting that we must include the header file of a particular container we are using always in our program and use all the functionalities of it provided by stl in my program but if i want to create a user defined function then i had to make the classes and objects and then code for it from the scratch considering now i can use the predefined stl functions in my classes and objects to create the desired function

exactly, see the thing is , if you are aware of the concept of classes, list , vectors etc are all classes and they have there private members like head pointer etc,which is not accessible to you for modification

Ok i got it.
One more question how to use comparator to sort a linked list according to our wish as we can’t access the elements of linked list directly

One more thing ,
If i had included the header file list in our program and now i had defined a node class in my code containing data and next pointer. I have a object n of the node class. So now my question is that can i use the stl functions in my object n like push_back etc or again i had to create functions like push_back to be used in my object seperately.

you can sort using sort function provide in forward_list
ref :- https://ide.codingblocks.com/s/265223

No , you can’t you have to define those function to use

refer this :- http://www.cplusplus.com/reference/forward_list/forward_list/#:~:text=std%3A%3Aforward_list&text=Forward%20lists%20are%20implemented%20as,next%20element%20in%20the%20sequence.

there is detailed explaination for each functionality along with examples

So isn’t hectic to write the same functions again and again as majority of questions can’t be solved by the functions provided to us by stl.

actually when you explore stl more u will find that most of the commonly used algos and ds are present. so don’t take lightly on stl. but also keep knowledge of implementation

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.