# Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.

## Presentation on theme: "Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23."— Presentation transcript:

Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23

A program can insert a new Node in link list, by first declaring its memory in the RAM. We know, dynamic memory can be declared using malloc () or realloc ( ) functions. data link 10 head_ptr Inserting a First Node Allocate memory using malloc ( ) or realloc ( ) NULL

Link List Code struct Node { float Data; Node *link; }; Node *head_ptr; void main (void ) { Node *new_node; new_node = (struct Node *) malloc (sizeof (struct Node)); new_node->data = 10; new_node->link = NULL; if ( head_ptr == NULL ) head_ptr = new_node; } data link 10 NULL

Insertion Code in Function void main ( void ) { InsertNode (10); } void InsertNode (int data) { Node *new_node; new_node = (struct Node *) malloc (sizeof (struct Node)); new_node->data = data; new_node->link = NULL; if ( head_ptr == NULL ) head_ptr = new_node; }

First, declare a new memory of second node in RAM. data link 10 head_ptr Inserting of Second Node Allocate memory using malloc ( ) or realloc ( ) NULL data link 16 NULL

First, declare a new memory of second node in RAM. Points the last node->link to this new memory. data link 10 head_ptr Inserting of Second Node Allocate memory using malloc ( ) or realloc ( ) data link 16 NULL

First, declare a new memory of second node in RAM. Points the last node->link to this new memory. How we can points the last node->link to this new memory. Move from head_ptr, and traverse each existing link list node. If any node->link contains NULL, this will be our last node. Insertion cost O(total nodes of link list). data link 10 head_ptr Inserting of Second Node Allocate memory using malloc ( ) or realloc ( ) NULL data link 16 NULL

Reducing Insertion cost:- A program can also keep track of the last node by using a pointer variable such as tail_ptr in this example. Notice that tail_ptr itself is not a node -- it is a pointer to a node. data link 10 data link 15 data link 7 null head_ptr node * head_ptr; node * tail_ptr; tail_ptr Inserting of Second Node

Insertion of First Node Node *tail_ptr = NULL; void main ( void ) { InsertNode (10); } void InsertNode (int data) { Node *new_node; new_node = (struct Node *) malloc (sizeof (struct Node)); new_node->data = data; new_node->link = NULL; if ( head_ptr == NULL ) { head_ptr = new_node; } tail_ptr = new_node; } 10 head_ptr tail_ptr

Insertion of Second Node void main ( void ) { InsertNode (10); InsertNode (16); } void InsertNode (int data) { Node *new_node; new_node = (struct Node *) malloc (sizeof (struct Node)); new_node->data = data; new_node->link = NULL; if ( head_ptr == NULL ) head_ptr = new_node; tail_ptr = new_node; } 10 head_ptr tail_ptr 1616

Insertion of Second Node void main ( void ) { InsertNode (10); InsertNode (16); } void InsertNode (int data) { Node *new_node; new_node = (struct Node *) malloc (sizeof (struct Node)); new_node->data = data; new_node->link = NULL; if ( head_ptr == NULL ) head_ptr = new_node; else tail_ptr->link = new_node; tail_ptr = new_node; } 10 head_ptr tail_ptr 16

Insertion of Third Node void main ( void ) { InsertNode (10); InsertNode (16); InsertNode (20); } void InsertNode (int data) { Node *new_node; new_node = (struct Node *) malloc (sizeof (struct Node)); new_node->data = data; new_node->link = NULL; if ( head_ptr == NULL ) head_ptr = new_node; else tail_ptr->link = new_node; tail_ptr = new_node; } 10 head_ptr tail_ptr 1620

Printing Node Information void PrintNodeInformation ( void ) void main ( void ) { PrintNodeInformation (void); } void PrintNodeInformation (void) { Node *temp_node = head_ptr; while (1) { printf (“Data = %f”, temp_node->data ); temp_node = temp_node->link; if ( temp_node == NULL ) break; } 10 head_ptr tail_ptr 1620

Printing Node Information void DeleteNode ( ) void main ( void ) { DeleteNode ( ); } void PrintNodeInformation ( ) { Node *temp_node; temp_node = head_ptr; head_ptr = head_ptr->link; free (temp_node); } 10 head_ptr tail_ptr 1620

Printing Node Information void DeleteNode ( ) void main ( void ) { DeleteNode ( ); } void PrintNodeInformation ( ) { Node *temp_node; temp_node = head_ptr; head_ptr = head_ptr->link; free (temp_node); } 10 head_ptr tail_ptr 1620

Printing Node Information void DeleteNode ( ) void main ( void ) { DeleteNode ( ); } void PrintNodeInformation ( ) { Node *temp_node; temp_node = head_ptr; head_ptr = head_ptr->link; free (temp_node); } head_ptr tail_ptr 1620