• Breaking News

    Funny Coder

    Funny coder is an open source web for interested programmer. It is a programming environment.It's a way where you can code with fun.

    Saturday, March 26, 2016

    Linked List Details || A Full Linked list Lecture on C

    What is linked list:

    A linked list, or one way list is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. Each node is divided into two parts:
    1. The first part contains the information of the element/node
    2. The second part contains the address of the next node (link /next pointer field) in the list.
    3. There is a special pointer Start/List contains the address of first node in the list. If this special pointer contains null, means that List is empty.

    Or

    A linked list is a data structure which can change during execution.
    Which can follow the following conditions.
    1. All of the elements are connected by pointers.
    2. Last element points to NULL.
    3. It can grow or shrink in size during execution of a program.This is why linked list is called dynamic.
    4. It can be made just as long as required.
    5. It does not waste memory space.

    From Wikipedia : Linked list is a linear collection of data elements, called nodes pointing to the next node by means of pointer. It is a data structure consisting of a group ofnodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence.
    Singly-linked-list.svg





    Difference between Arrays and linked list:


    1. Arrays are direct information structures. linked lists are direct and non-straight information structures. 
    2. Linked lists are direct to access, and non-straight to store in memory 
    3. Array has homogeneous qualities. Furthermore, every component is free of each different positions. Every node in the linked list is associated with its past node which is a pointer to the node. 
    4. Array components can be adjusted effortlessly by recognizing the file esteem. It is a mind boggling process for altering the node in a connected rundown. 
    5. Array components can not be included, erased once it is pronounced. The nodes in the linked list can be included and erased from the list.
    6. Insert and delete in the array is so much difficult. These two works in linked list is so much easy.It needs to know the movement of elements for insertion and deletion in the array. It's no need in the dynamic linked list.In Linked List we have to Just Change the Pointer address field (Pointer),So Insertion and Deletion Operations are quite easy to implement                             For example, suppose we maintain a sorted list of id's in an array id[].                                                       id[] = [1000, 1010, 1050, 2000, 2040, …..].                                                                                   And if we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the elements after 1000 (excluding 1000).                                                                                              Deletion is also expensive with arrays until unless some special techniques are used. For example, to delete 1010 in id[], everything after 1010 has to be moved.
    7. In array space is wasted, but in the linked list only the specific space will use. This is why space is not wasted in the linked list.
    8. It's size is fixed and you can't extend it's size if you need in the next part of your program. But in the linked list it's size isn't fixed. And so, you can extend or broad the size of linked list.
    9. Elements will be stored in a fixed memory location in array. Elements will or will not be stored in a fixed memory locations.
    10. Array is randomly accessed but store the data in a sequential way in memory location. But in linked list is sequential accessed but data will be stored in randomly. So, in the linked list We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists.
    Image:





    There are three types of linked list:

    1. Singly linked list.
    2. Doubly linked list.
    3. Circular linked list.

    1.Singly Linked list : 

    Singly Linked Lists are a type of data structure. It is a type of list. In a singly linked list each node in the list stores the contents of the node and a pointer or reference to the next node in the list. It does not store any pointer or reference to the previous node. It is called a singly linked list because each node only has a single link to another node. To store a single linked list, you only need to store a reference or pointer to the first node in that list. The last node has a pointer to nothingness to indicate that it is the last node.

    singly-linked-list.png

    In Singly linked list every element contains some data and a link to the next element, which allows to keep the structure.
    For example in code :

    1. typedef struct node
    2. {
    3.         int data;
    4.         struct Node *next;
    5. }node;
    node* head = (node*) malloc(sizeof(node)); 

    In this code there is a data named data and a pointer called next which type is Node. Then for future operation we can use these next pointer to go to the next address of data2 in the linked list. By these procedure we will go up to the next ...When we will find a null address or pointer, then we stop our linked list. This is a simple functionality of singly linked list.

    Get the code of singly linked list from  here:

    Linked List C Example-Singly linked list with three methods


    To See more : https://en.wikibooks.org/wiki/Data_Structures/Singly_Linked_Lists

    2. Two way or Doubly linked list:

    A doubly linked list is a list that contains links to next and previous nodes. Unlike singly linked lists where traversal is only one way, doubly linked lists allow traversals in both ways.


    doubly-linked-list-funny-coder


    For an example of doubly linked list :
    1. typedef struct node
    2.      int data;
    3.      struct node* next;
    4.      struct node* prev
    5. node;
    6.  node* head = (node*) malloc(sizeof(node)); 

    In the doubly linked list there needs one data and two pointer variable. One pointer will point to the next node which in line no - 3 and others will point to the previous node which in line no - 4

    2. Circular linked list:

    Circular linked list is such types of linked list which first element points to last element and last element point to first element. That means a circular linked list has no beginning and ending.
    The last item will point back to the first item and first item point back to the last item. Circular linked list allows us to traverse the list in either direction.
    Circular linked list

    Circular linked list can be made both in singly and doubly linked list. To clear your thought on that see the below image:
    Circular linked list both singly and doubly

    Just see the below code for insert method of singly circular list :
    1. while(pointer -> next != start)
    2.         {
    3.                 pointer = pointer ->  next;
    4.         }
    Thanks viewers, with my little knowledge I've tried to clear the linked list minimum concept of yours. If my post help you a little, that will be a great to me.
    Get the code of singly linked list from  here:

    Linked List C Example-Singly linked list with three methods


    Ok, since if you face any problem, you kindly write your comment in the comment box...Or mail me:
    manirujjamanakash@gmail.com.
    Or in facebook : https://web.facebook.com/manirujjaman.akash.
    You can better take your eyes in our facebook fanfage for getting regular post from funny-coder
    https://web.facebook.com/prgraamming.jagath









    No comments:

    Post a Comment

    Fashion

    Beauty

    Travel