In this tutorial, you will learn the
1. Code for calculating number of elements in linked list.
2. Code for sorting of a linked list.
3. Code of destructor of linked list.
What is the code for calculating number of elements in a linked list?
- int count()
- int count=0;
- Node *cur;
- return count;
What is the code for sorting of linked list?
An ordered linked list is a linked list in which data is arranged in ascending order. Sometimes, you need an ordered linked list for some tasks
- void sort()
- Node *cur= head;
- int temp;
- int counter=count();
- for (int i=0; i<counter; i++)
- cur = head;
- while (cur->next!=NULL)
- if (cur->data > cur->next->data)
- cur = cur->next;
What is the code for destructor of a linked list?
If you recall that while adding nodes in a linked list, dynamic nodes were added. As there was an operator of “new”. As dynamic memory must me deleted, so there must be a destructor in linked list.
- ~Linklist ()
Here we delete every node one by one until the linked list is empty.
Note: C++ codes along merged with the previous linked list codes are given along with these. In next tutorial , I will try to wind up basic functions of linked list like insertion in middle, deletion from middle and copy constructor.
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.