Linked List
Linked List:
So far, we have studied arrays and pointers. We see, whenever we make an arrays we have to declare its size. i.e my_array[10] or some thing like my_array[10][10] (2D array). We solve this problem by using pointers. We used to ask size of array from user and then, make dynamic memory of that size. We did something like int* my_array = new int[size]. Where size is given by user. Now let discuss what is its demerit of using this process of creating dynamic memory. Let say, user want to make array of size 20. Using dynamic memory we will create array of size 20 easily, but now let say, user wants to add one more item to this array. i.e he/ she wants to grow this array. What we can do using this strategy, we will make an array of size 21 or more then, transfer the data of previous array the delete the previous array. What's wrong with using this type of implementation? Lets consider, we have made an array of size 10,000,000. Now we want to grow this array to 10,000,001. Using the above implementation will take very long time to grow this array, because we need to copy data from old array of size 10,000,000 to new array of size 10,000,001 which require much time. Now here, comes the use and requirement of Linked List.
One more thing, I want to add is that, when we allocate memory using 'new', we get contiguous memory. For example, if we want to get dynamic memory of size 100, we will get a block of memory of size 100. The memory slots will be adjacent to each other. Lets take an example to understand this:
int* my_array = new int[100];
Memory will be allocated from 0th index to 99th index
array[0] . . . . array[50] . . . . . array[99]
👆 👆 👆
0x0100 0x0104 0x0108
Gap of 4 bytes. As size of int is 4 bytes
Using linked list, we do not get contiguous memory, we only keep track of the address where our data is stores. Lets take an example to understand this:
data1 data2 data3
👆 👆 👆
0x0186 0x6123 0x9715
👆 👆 👆
Above is the data and below is their addresses, we see clearly, our data is stored at random addresses and we will just keep the track of that addresses.
Basic Idea:
The basic idea and concept of linked list is to solve the above mentioned problem and to grow any array in O(1) time. If we have an array of size 10,000,000 and we want to grow it to 10,000,001 we will be able to do this in just O(1) time.
Hope so, you have read the above article carefully, and you are familiar with the need, importance and advantages of using Linked List.
Now wee will see its implementation.
Implementation:
We make two classes, first class is Node class and the second class is Linked_list class.
Node class:
Node class is basically, used to store data. Generally, it includes data of any type int, char, float or any other user defined class type and a pointer of type Node(itself).
Here is the code for Node class. We have made it template class to make it generic.
Code:
template<typename T>
class Node
{
T data; // data
Node* nextptr; // pointer to next data
public:
template <class x>friend class List; // make List class friend of Node class
Node() // default constructor
{
nextptr = nullptr;
}
Node(T data, Node* ptr = 0) // parameterized constructor
{
this->data = data;
nextptr = ptr;
}
~Node() // destructor
{
}
};
Linked List Class:
The linked list class provide functionalities to user like add data to list, remove data from list, update any data from list. Linked list class uses Node class to perform these functionalities.
Linked list class generally have two pointer of type Node. One is often called as head and the second is often called as tail. Head stores the starting Node of the list and tail stores the ending address of the Node.
Here is the code for List class.
Comments
Post a Comment
Feel free to comment or ask something related to games.