Length of a Linked ListCode CrazeMar 10, 20231 min read#include <stdio.h>struct node{ int data; struct node *next;};struct node *head,*newnode;int choice;int length=0;void main() { printf("\n----Insertion At Beginning----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=head; head=newnode; printf("\nEnter your choice : (0/1) "); scanf("%d",&choice); }while(choice); temp=head; printf("\n\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; length++; } printf("\n\nLength of linked list : %d",length); }
Linked List Insertion At Any PositionCode CrazeMar 9, 20231 min read#include <stdio.h>struct node{ int data; struct node *next;};struct node *head,*newnode;int choice;int pos, i=1;void main() { printf("\n----Insertion At Any position----"); struct node *temp; int count=0; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d", &newnode->data); printf("\nEnter position : "); scanf("%d", &pos); newnode->next=0; if(head==0) { head=temp=newnode; } else{ temp=head; while(i<pos) { temp=temp->next; } newnode->next=temp->next; temp->next=newnode; } printf("\nEnter your choice : (0/1) "); scanf("%d", &choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; count++; } printf("\nLength of linked list : %d",count);}
Linked List Insertion At EndCode CrazeMar 9, 20231 min read#include <stdio.h>struct node{ int data; struct node *next;};struct node *head,*newnode;int choice;void main() { printf("\n----Insertion At End----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d", &newnode->data); newnode->next=0; if(head==0) { head=temp=newnode; } else{ temp=head; while(temp->next!=0) { temp=temp->next; } temp->next=newnode; } printf("\nEnter your choice : (0/1) "); scanf("%d", &choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d", temp->data); temp=temp->next; }}