Professional Documents
Culture Documents
8 Singly Linked List 20-03-2023
8 Singly Linked List 20-03-2023
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;
start = start->next;
Step 3 : Delete temp i.e Previous Starting Node as we have Updated Version of Start
Pointer
free(temp);
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);
}
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);
}
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. }