Download presentation
Presentation is loading. Please wait.
Published byMarvin Chapman Modified over 9 years ago
1
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23
2
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
3
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
4
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; }
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
data link 10 data link 16 data link 20 null head_ptr tail_ptr Deleting first Node
15
data link 10 data link 16 data link 20 null head_ptr tail_ptr Deleting first Node
16
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
17
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
18
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
19
data link 10 data link 16 data link 20 null head_ptr tail_ptr Deleting user specific Node
20
Deleting User Specific Node void DeleteNode (float Data) { Node *temp_node = head_ptr, *last_node; while (1) { if ( temp_node->data == Data ) { last_node->link = temp_node->link; free (temp_node); break; } last_node = temp_node; temp_node = temp_node->link; if ( temp_node == NULL ) break; } 10 head_ptr tail_ptr 1620
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.