Linear Data Structure 503
Enter a number 3
Continue (y/n): y
Enter a number. 8
Continue (y/n): y
Enter a number 9
Continue (y/n): n
The list elements are: 5 3 8 9
Explanation In the above program, the structure node is declared. The structure node contains
integer variable number and pointer to the same structure *p. The structure pointer variables *h, *f
and *t are also declared. In function main (), variable of character type c is defined. The pointer f is
initialized to NULL.
In the do-while loop, using m alloc () function a memory of size equal to that of the structure
node (4 byte) is allocated to pointer h. The entered number is stored in the number variable. The
i f () statement checks the pointer/. If it is NULL, h is assigned to other two pointers, that is, f and
t. Here,/is the first node, h is the header and t is used for temporary storage. The user has to enter
'y' to enter the following elements. First time, the value of f is NULL and hence, e ls e () block is
executed. The purpose of assignment of h to f and t is as follows.
1) In i f (), t is used to assign the address of the newly created next node to the current, that is, h.
2) Only for the first time else block is executed and in which address of h (when the first node is
created) is assigned to f. f contains the address of the first node.
When a user finishes entering a number, t - >p=NULL; statement assigns NULL to pointer p and
link ends here. The address of the first node (f) is assigned to t. Using while loop, the pointer p is
accessed, and elements are accessed. When NULL pointer is encountered, the while loop terminates.
Fig.14.18 shows the detail about the pointers in the linked list.
Fig. 14.18 Fields of node
14.20 INSERTION
Insertion of an element in the linked list leads to several operations. The following steps are involved
in inserting an element.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.164.141