We will talk about linked list today. Actually, it's a pretty much basic data structure..So,the very first question is what is linked list? ๐ฒ๐ฒ
-> Actually, linked list is nothing but a list. We all know that array is also a list.. Linked list is also a list of some variables. Now, we should
why we should use linked list instead of Array?
Mmmm..Let's think for a moment...Okey..๐๐.Now, we can see the flows of arrays and linked list.
An array is a pre-allocated block of memory holding an ordered bunch of items. Aaaaa....what does it mean?๐๐
Actually, when you declared ๐๐
-> int arra[5];
in your code, then the thing is: RAM will allocate blocks of necessary size according to your array size. ๐๐
But, in linked-list, there is no limitations. You can create infinite size of list of elements using this list. But remember one thing, your memory size is limited...!!!! ๐ซ๐ซ
Okey, Let's come to the point.
Now, we will make a function of creatingNode(). This type of function will help us to create a new node. The essential items which are needed to arrange this is given below:-
-> Actually, linked list is nothing but a list. We all know that array is also a list.. Linked list is also a list of some variables. Now, we should
why we should use linked list instead of Array?
Mmmm..Let's think for a moment...Okey..๐๐.Now, we can see the flows of arrays and linked list.
An array is a pre-allocated block of memory holding an ordered bunch of items. Aaaaa....what does it mean?๐๐
Actually, when you declared ๐๐
-> int arra[5];
in your code, then the thing is: RAM will allocate blocks of necessary size according to your array size. ๐๐
But, in linked-list, there is no limitations. You can create infinite size of list of elements using this list. But remember one thing, your memory size is limited...!!!! ๐ซ๐ซ
Okey, Let's come to the point.
LINKED-LIST
1.singly-linked list 2.Double-linked list
Firstly, we have to know that, in linked list, we will take elements as nodes. Each node has two different things:
1. data-> it will hold a numeric value or character or something else.
2. Next-> it will hold the address of the next element.
Now, we will go to the main section.
Liked list means making linked between lists. This next pointer will make the link between two elements by catching their address. Now we will see a code and can clear those things.
Woooohh..wait.. I forget to say one thing๐๐. That is:
As node is consisted with two things. So, it must be created through class or structure. Okey?
struct Node
{
int data;
Node *next;
};
This will be the structure of Node. Yes, we have finished creating node.
Now, we will go to implementation ..
class list
{
protected:
Node *head,*tail;
public:
list()
{
head=NULL;
tail=NULL;
}
};
See below:
The memory is allocating like this:
+---+-----+ +----+----+ +----+----+ | # | # | | # | # | | # | # | +---+-----+ +----+----+ +----+----+
If we don't assign head and tail as NULL, then those value will be treated as garbage value.
So, we make them NULL.๐ฆ๐๐
Now, we will make a function of creatingNode(). This type of function will help us to create a new node. The essential items which are needed to arrange this is given below:-
CreatingNode() |
1.
NODE TYPE POINTER(WHICH IS ALREADY DEFINED)
2.
Inserting value to it’s data field.
Above two things will be in the function.
Now, we will consider a case. That we want to know what would happen if the linked list is still empty? We will have to check it. Do you remember that the head points to the first node? It means if the head is equal to NULL then we can conclude that the linked list is empty.
Secondly, If there is one node of a linked-list, then that node will be considered as head and tail both.
Now, consider again a case.. If a linked-list is created and we have to insert a node at the end of the linked list. We know that the last node is called tail.So, we are going to create new node next to the tail node.
Now, the process of creating a new node at the end is like this:
1. Firstly linked the new node with tail node. Means, passing the new node address to the next pointer of tail node.
2. Secondly, as tail node points to the last. So, let the tail points to the last node.
|
Let's see the structure:
void createnode(int value)
{
Node *temp = new Node;
temp->data = value;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
We have already created node and completed additional tasks. So, now we have to display the whole node. Now watch this->
void print()
{
Node *temp = new Node;
temp = head;
while (temp != NULL)
{
cout << temp->data << endl;
temp = temp->next;
}
}
We have completed creating linked-list part.. The second part will be started from inserting and deleting nodes in linked-list.......
Thank you.... ๐๐๐
Happy Coding.. ๐๐๐
|
Comments
Post a Comment