Skip to main content

LINKED LIST

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. 



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

Popular posts from this blog

Remote Dictionary Server

  Today I want to discuss about REDIS Cache. In a simple way, it’s like making web apps more effective. What they say ? From Redis  official website , “Redis is an open-source, in-memory data structure store, used as a database, cache and message broker. It supports data structures . I t can be thought as a NOSql DB which store data as a key value pair in the system memory. Now, let’s talk about caching. Difference Between MongoDB And Redis MongoDB is a disk based database optimized for operational simplicity. It has a huge data volumes. And Redis is an in-memory, persistent  data Structure store that is popular for performing operations with maximum effectiveness. Caching Caching is a process where the data are stored in caches so that those can be retrieved faster. It is for making user experience better. Redis Cache As Redis is an in-memory data structure store  where data access operations are performed faster. That’s why it’s the perfect method for caching. The ...

Some Important learning sites for Dynamic programming

Now, We will discuss some of the most useful sites for learning Dynamic Programming. Here, I have mentioned some of those . :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: medium.freecodecamp.org -  Demystifying Dynamic Programming iarcs.org.in -  Dynamic Programming - Tiling topcoder.com -  Dynamic Programming – From Novice to Advanced illinois.edu -  Dynamic Programming  ;(Exercises are recommended) codechef.com -  Dynamic Programming geeksforgeeks.org -  Dynamic Programming  ;(Contains a lot of practice sessions) MIT OCW (Contains some Advanced topics as well) Dynamic Programming I Dynamic Programming II Dynamic Programming III Dynamic Programming IV Happy Learning...Thanks ๐Ÿ˜‰๐Ÿ˜‰๐Ÿ˜Š๐Ÿ˜Š  

Params in C#

C# HAS A FEATURE. IT CAN SPECIFY A METHOD PARAMETER WHICH WILL TAKE A VARIABLE NUMBER OF  ARGUMENTS. WE USED TO DO THOSE THINGS BY USING POINTER IN C++ C# MAKES OUR LIFE EASIER. ALL WE NEED IS A KEYWORD. AND THAT IS "PARAMS" LET'S HAVE THE FUN REMEMBER, THE PARAMS PARAMETER MUST BE A SINGLE ARRAY.   public class MyClass         {                          public static void amarmethod(params int[] amararrayerkichuelements)             {                                 for (int i = 0; i < amararrayerkichuelements.Length; i++)                 {                     Console.Write(amararrayerkichuelements[i] + " ");                 } ...