Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Delete First Node from Singly Linked List

Program :

void del_beg()
{
struct node *temp;
if (start==NULL)
Printf(" list is empty")

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);

DELETION AT THE END


void last_delete()  
{  
    struct node *ptr,*ptr1;  
    if(start == NULL)  
    {  
        printf("\nlist is empty");  
    }  
    else if(start -> next == NULL)  
    {  free(start);  
        start = NULL;  
        
        printf("\nOnly node of the list deleted ...\n");  
    }  
          
    else  
    {  
        ptr = start;   
        while(ptr->next != NULL)  
        {  
            ptr1 = ptr;  
            ptr = ptr ->next;  
        }  
        ptr1->next = NULL;  
        free(ptr);  
        printf("\nDeleted Node from the last ...\n");  
    }     
}  
DELETION AT THE MIDDLE

void random_delete()  
{  
    struct node *ptr,*ptr1;  
    int loc,i;    
    printf("\n Enter the location of the node after which you wan
t to perform deletion \n");  
    scanf("%d",&loc);  
    ptr=start;  
    for(i=1;i<=loc-1;i++)  
    {  
        ptr1 = ptr;       
        ptr = ptr->next;  
              
        if(ptr == NULL)  
        {  
            printf("\nCan't delete");  
            return;  
        }  
    }  
    ptr1 ->next = ptr ->next;  
    free(ptr);  
    printf("\nDeleted node %d ",loc);  
}  

Counting number of Nodes in Linked List :

We know the logic for traversing through the linked list in C Programming. [See Dry
Run]

Function for counting the singly linked nodes is very similar to display(), Only
difference is that instead of printing data we are incrementing length variable.
Program :

void count()
{
struct node *temp;
int count =0;
temp = start;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("nLength of Linked List : %d",length);
}

Searching and Traversing for Data in Singly Linked List


Way 1 : This Function only tells that whether data is present or not

 This function return 1 if data found successfully.


 Returns 0 if data is not present

void search(int num)


{
while(temp->data!=num)
{
temp=temp=>next;
if (temp==null)
{
printf(data not foung'');
break;
}
}
printf(data found");
temp->data = 10;
}

1. void display()  
2. {  
3.     struct node *ptr;  
4.     ptr = start;  
5.     if(start == NULL)  
6.     {  
7.         printf("Nothing to print");  
8.     }  
9.     else  
10.     {  
11.         printf("\nprinting values . . . . .\n");   
12.         while (ptr!=NULL)  
13.         {  
14.             printf("\n%d",ptr->data);  
15.
16.             ptr = ptr -> next;  
17.         }  
18.     }  
19. }     

You might also like