Inserting node at start in the SLL (Steps):
- Create New Node
- Fill Data into “Data Field“
- Make it’s “Pointer” or “Next Field” as NULL
- Attach This newly Created node to Start
- 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 :
Attention :
- If starting node is not available then “Start = NULL” then following part is executed
if(start==NULL) { start=new_node; current=new_node; }
- 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; }
No comments:
Post a Comment