Creating a node C

The basic thing is to create a node. It can be done by using the malloc function.

start = (node*) malloc(sizeof(node))

This statement creates the starting node of list. A block of memory whose size is equal to the sizeof(node) is allocated to the node pointer start. Typecasting is used because otherwise malloc will return pointer to character.

code snippet to create a node.

code snippet to create a node.
Self-referential structure emp is defined and is named as node. Two pointers, ‘start’ and ‘temp’ are defined and initially assigned a NULL value.

code snippet to create a node.

A value for e_no is accepted. e_no entered as -1, is decided to be the terminating condition for creating nodes. It is checked if starting node is to be created or further nodes are to be created. If value of start pointer is NULL, it means starting node does not exist and is to be created. Memory is assigned to start pointer. Value of e_no is assigned to its e_no field and self-referential pointer assigned a value of NULL.

self-referential pointer assigned a value of NULL.
If value of start pointer is not null, it means start pointer exists and further node is to be created which is created in the same manner.

start pointer
As new nodes are created start pointer is kept fixed while temp pointer is moved.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics