2. What is a linked list #
Created Tuesday 14 January 2020
-
Arrays always need continguos memory, which may not be available every time. This is because gaps in memory are present normally.
-
Linked list is the way to go.
-
It has a basic unit called node, which stores the data and the address(i.e pointer) of the next node.
There are 2 kinds of lists:
- Singly linked lists. Have the address of the next node.
- Douby linked lists. Have the address of the next and the previous node.
- To operate with nodes, we need our own data structure, as none of the primitive data types support it.
- We make a node class with data members
- data(int)
- address(Node*)
Doubt: Node * seems infinitely recursive. If it had been Node, it would be recursive. But Node* is not, it is just a pointer with a jump = sizeof(int + pointer), and jump is everything that a pointer stores, except of course the start address, which will be given by the OS. Node class, for linked list
Jargon:
- Head node. The first Node.
- Tail Node. The last Node. It points to NULL.
- We will use this class only as a node. Everything else is done in a functional way.