Showing posts with label linkedlist. Show all posts
Showing posts with label linkedlist. Show all posts

Wednesday, September 2, 2020

Delete Node from First Postion : Singly Linked List

Delete First Node from Singly Linked List

Program :
void del_beg()
{
struct node *temp;

temp = start;
start = start->next;

free(temp);
printf("nThe Element deleted Successfully ");
}

Attention :
Step 1 : Store Current Start in Another Temporary Pointer
temp = start;
Step 2 : Move Start Pointer One position Ahead
start = start->next;
Step 3 : Delete temp i.e Previous Starting Node as we have Updated Version of Start Pointer
free(temp);


Insert node at Last / End Position in Singly Linked List



Inserting node at start in the SLL (Steps):
  1. Create New Node
  2. Fill Data into “Data Field“
  3. Make it’s “Pointer” or “Next Field” as NULL
  4. Node is to be inserted at Last Position so we need to traverse SLL upto Last Node.
  5. Make link between last node and newnode
void insert_at_end()
{
struct node *new_node,*current;

new_node=(struct node *)malloc(sizeof(struct node));

if(new_node == NULL)
   printf("nFailed to Allocate Memory");

 printf("nEnter the data : ");
 scanf("%d",&new_node->data);
 new_node->next=NULL;

 if(start==NULL)
 {
   start=new_node;
   current=new_node;
 }
 else
 {
   temp = start;
     while(temp->next!=NULL)
     {
     temp = temp->next;
     }
   temp->next = new_node;
 }
}

Diagram :
insert_last
Attention :
  1. If starting node is not available then “Start = NULL” then following part is executed
if(start==NULL)
       {
       start=new_node;
       current=new_node;
       }
  1. If we have previously created First or starting node then “else part” will be executed to insert node at start
  2. Traverse Upto Last Node., So that temp can keep track of Last node
else
       {
       temp = start;
   while(temp->next!=NULL)
  {
  temp = temp->next;
  }
  1. Make Link between Newly Created node and Last node ( temp )
temp->next = new_node;

To pass Node Variable to Function Write it as –
void insert_at_end(struct node *temp)

Insert node at Start/First Position in Singly Linked List



Inserting node at start in the SLL (Steps):
  1. Create New Node
  2. Fill Data into “Data Field“
  3. Make it’s “Pointer” or “Next Field” as NULL
  4. Attach This newly Created node to Start
  5. Make newnode as Starting node
void insert_at_beg()
{
struct node *new_node,*current;

new_node=(struct node *)malloc(sizeof(struct node));

 if(new_node == NULL)
    printf("nFailed to Allocate Memory");

 printf("nEnter the data : ");
 scanf("%d",&new_node->data);
 new_node->next=NULL;

   if(start==NULL)
   {
   start=new_node;
   current=new_node;
   }
   else
   {
   new_node->next=start;
   start=new_node;
   }
}

Diagram :
insert_start
Attention :
  1. If starting node is not available then “Start = NULL” then following part is executed
if(start==NULL)
       {
       start=new_node;
       current=new_node;
       }
  1. If we have previously created First or starting node then “else part” will be executed to insert node at start
else
       {
       new_node->next=start;
       start=new_node;
       }

Linked-List : Insert Node at Middle Position in Singly Linked List


void insert_mid()
{
    int pos,i;
    struct node *new_node,*current,*temp,*temp1;

    new_node=(struct node *)malloc(sizeof(struct node));

    printf("nEnter the data : ");
    scanf("%d",&new_node->data);

    new_node->next=NULL;
    st :
    printf("nEnter the position : ");
    scanf("%d",&pos);

    if(pos>=(length()+1))
       {
       printf("nError : pos > length ");
       goto st;
       }

    if(start==NULL)
       {
       start=new_node;
       current=new_node;
       }
    else
       {
       temp = start;
             for(i=1;i< pos-1;i++)
             {
             temp = temp->next;
             }
       temp1=temp->next;
       temp->next = new_node;
       new_node->next=temp1;
       }
}

Explanation :
Step 1 : Get Current Position Of “temp” and “temp1” Pointer.
temp = start;
                for(i=1;i< pos-1;i++)
  {
  temp = temp->next;
  }
Step 2 :
temp1=temp->next;
Step 3 :
temp->next = new_node;
Step 4 :
new_node->next = temp1


Introduction to Linked List


  1. It is a data Structure which consists if group of nodes that forms a sequence.
  2. It is very common data structure that is used to create tree,graph and other abstract data types.Basic Node Strucutre in Linked List
Linked list comprise of group or list of nodes in which each node have link to next node to form a chain

Linked List definition

  1. Linked List is Series of Nodes
  2. Each node Consist of two Parts viz Data Part & Pointer Part
  3. Pointer Part stores the address of the next node
Linked List Basic Structure in Programming

What is linked list Node ?

  1. Each Linked List Consists of Series of Nodes
  2. In above Diagram , Linked List Consists of three nodes A,B,C etc
  3. Node A has two part one data part which consists of the 5 as data and the second part which contain the address of the next node (i.e it contain the address of the next node)

Addres and Linked List Node Strucutre in Programming

Linked list Blocks

Linked list is created using following elements –
NoElementExplanation
1NodeLinked list is collection of number of nodes
2Address Field in NodeAddress field in node is used to keep address of next node
3Data Field in NodeData field in node is used to hold data inside linked list.
We can represent linked list in real life using train in which all the buggies are nodes and two coaches are connected using the connectors.
linked list real life example
In case of railway we have peoples seating arrangement inside the coaches is called as data part of lined list while connection between two buggies is address filed of linked list.
Like linked list, trains also have last coach which is not further connected to any of the buggie. Engine can be called as first node of linked list

Advantages of linked list

Firstly understand the basic concept of linked list and take a look at below diagram of linked list –
Addres and Linked List Node Strucutre in Programming
List of advantages :
  1. Linked List is Dynamic data Structure .
  2. Linked List can grow and shrink during run time.
  3. Insertion and Deletion Operations are Easier
  4. Efficient Memory Utilization ,i.e no need to pre-allocate memory
  5. Faster Access time,can be expanded in constant time without memory overhead
  6. Linear Data Structures such as Stack,Queue can be easily implemeted using Linked list

Drawbacks / Disadvantages of Linked List

Disadvantages of Linked List in C Programming

1. Wastage of Memory –

  1. Pointer Requires extra memory for storage.
  2. Suppose we want to store 3 integer data items then we have to allocate memory –
in case of array –
Memory Required in Array = 3 Integer * Size
                         = 3 * 2 bytes
                         = 6 bytes
in case of array –
Memory Required in LL    = 3 Integer * Size of Node
                         = 3 * Size of Node Structure
                         = 3 * Size(data + address pointer)
                         = 3 * (2 bytes + x bytes)
                         = 6 bytes + 3x bytes
*x is size of complete node structure it may vary

2. No Random Access

  1. In array we can access nth element easily just by using a[n].
  2. In Linked list no random access is given to user, we have to access each node sequentially.
  3. Suppose we have to access nth node then we have to traverse linked list n times.
Suppose Element is present at the starting location then –
We can access element in first Attempt
Suppose Element is present at the Last location then –
We can access element in last Attempt

3 . Time Complexity

  1. Array can be randomly accessed , while the Linked list cannot be accessed Randomly
  2. Individual nodes are not stored in the contiguous memory Locations.
  3. Access time for Individual Element is O(n) whereas in Array it is O(1).

4. Reverse Traversing is difficult

  1. In case if we are using singly linked list then it is very difficult to traverse linked list from end.
  2. If using doubly linked list then though it becomes easier to traverse from end but still it increases again storage space for back pointer.

5. Heap Space Restriction

  1. Whenever memory is dynamically allocated , It utilizes memory from heap.
  2. Memory is allocated to Linked List at run time if and only if there is space available in heap.
  3. If there is insufficient space in heap then it won’t create any memory.

Difference Between array and Linked List : Array Vs Linked List

Difference between Array and Linked List

1. Access :Random / Sequential

  • Stack elements can be randomly Accessed using Subscript Variable
  • e.g a[0],a[1],a[3] can be randomly accessed
  • While In Linked List We have to Traverse Through the Linked List for Accessing Element. So O(n) Time required for Accessing Element .
  • Generally In linked List Elements are accessed Sequentially.

2 . Memory Structure :

  • Stack is stored in contiguous Memory Locations , i.e Suppose first element is Stored at 2000 then Second Integer element will be stored at 2002 .
  • But It is not necessary to store next element at the Consecutive memory Location .
  • Element is stored at any available Location , but the Pointer to that memory location is stored in Previous Node.

3 . Insertion / Deletion

  • As the Array elements are stored in Consecutive memory Locations , so While Inserting elements ,we have to create space for Insertion.
  • So More time required for Creating space and Inserting Element
  • Similarly We have to Delete the Element from given Location and then Shift All successive elements up by 1 position
  • In Linked List we have to Just Change the Pointer address field (Pointer),So Insertion and Deletion Operations are quite easy to implement

4 . Memory Allocation :

  • Memory Should be allocated at Compile-Time in Stack . i.e at the time when Programmer is Writing Program
  • In Linked list memory can be allocated at Run-Time , i.e After executing Program
  • Stack uses Static Memory Allocation and Linked List Uses Dynamic Memory Allocation
  • Dynamic Memory allocation functions – malloc,calloc,delete etc…


Singly Linked List : C Programming Data Structure

  1. In this type of Linked List two successive nodes are linked together in linear fashion .
  2. Each Node contain address of the next node to be followed.
  3. In Singly Linked List only Linear or Forward Sequential  movement is possible.
  4. Elements are accessed sequentially , no direct access is allowed.

Explanation :

  1. It is most basic type of Linked List in C.
  2. It is simple sequence of Dynamically allocated Nodes.
  3. Each Node has its successor and predecessor.
  4. First Node does not have predecessor while last node does not have any successor.
  5. Last Node have successor reference as “NULL“.
  6. In the above Linked List We have 3 types of nodes.
1.First Node
2.Last Node
3.Intermediate Nodes
  1. In Singly Linked List access is given only in one direction thus Accessing Singly Linked is Unidirectional.
  2. We can have multiple data fields inside Node but we have only single “Link” for next node.

Doubly Linked List : Traverse Bi-directional

  1. In Doubly Linked List , each node contain two address fields .
  2. One address field for storing address of next node to be followed and second address field contain address of previous node linked to it.
  3. So Two way access is possible i.e We can start accessing nodes from start as well as from last .
  4. Like Singly Linked List also only Linear but Bidirectional  Sequential  movement is possible.
  5. Elements are accessed sequentially , no direct access is allowed.
Doubly Linked List in Programming Data Structure

Explanation :

  1. Doubly Linked List is most useful type of Linked List.
  2. In DLL we have facility to access both next as well as previous data using “next link” and “previous link“.
  3. In the above diagram , suppose we are currently on 2nd node i.e at 4 then we can access next and previous data using –
To Access Next Data : curr_node->next->data
To Access Previous Data : curr_node->previous->data
  1. We have to always maintain start node , because it is the only node that is used to keep track of complete linked list.
Circular Linked List
  1. Circular Linked List is Divided into 2 Categories .
    • Singly Circular Linked List
    • Doubly Circular Linked List
  2. In Circular Linked List Address field of Last node contain address of “First Node“.
  3. In short First Node and Last Nodes are adjacent .
  4. Linked List is made circular by linking first and last node , so it looks like circular chain [ shown in Following diagram ].
  5. Two way access is possible only if we are using “Doubly Circular Linked List”
  6. Sequential  movement is possible
  7. No direct access is allowed.