0% found this document useful (0 votes)
28 views4 pages

Link 0

The document describes functions for implementing operations on a singly linked list data structure in C including creating and deleting nodes, inserting and removing nodes from the front and end of the list, reversing the list, finding the middle and nth node from the end, and dumping the list. It provides function prototypes and implementations for these operations and uses them in a main function to create a sample linked list, perform some operations on it like reversing and finding nodes, and output the results.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Link 0

The document describes functions for implementing operations on a singly linked list data structure in C including creating and deleting nodes, inserting and removing nodes from the front and end of the list, reversing the list, finding the middle and nth node from the end, and dumping the list. It provides function prototypes and implementations for these operations and uses them in a main function to create a sample linked list, perform some operations on it like reversing and finding nodes, and output the results.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h> struct node { int data; struct node* next; }; // Functions for allocating/deallocating memory struct node* create_node(int data); // Utility functions that can be used in reverse_list struct node* insert_at_front(struct node* head, struct node* new_node); struct node* insert_at_end(struct node* head, struct node* new_node); struct node* remove_from_front(struct node* head); struct node* reverse_list(struct node* head); // Delete a node without having the head pointer.. void delete_this_node(struct node* cur_node); // Find nth node from tail in single traversal.. struct node* find_nth_from_end(struct node* head, int n); // Find middle node of the list in single traversal.. struct node* find_middle_node(struct node* head); // Dump the list/node.. void dump_list(struct node* head); void dump_node(struct node* pNode); void copy_node(struct node* src, struct node* dest); // Main function int main(void) { struct node *list = NULL, *temp_node = NULL; int i = 0; // Create list with 10 members... for(i = 0; i <= 10; i++) { temp_node = create_node(i); list = insert_at_end(list, temp_node); } // Add 5 more in front... for(i = 14; i >= 11; i--) { temp_node = create_node(i); list = insert_at_front(list, temp_node); } dump_list(list); // To reverse a single linked list in single traversal....... /* list = reverse_list(list); */ // Delete a particular node without having the head pointer /* temp_node = list; for(i = 0; i <= 15; i++) { if(i == 4) { delete_this_node(temp_node); break; } temp_node = temp_node->next; } dump_list(list);

*/

// To find nth node from the end of list in single traversal.... /*

temp_node = find_nth_from_end(list, 10); */ dump_node(temp_node);

// To find the middle node of list in single traversal...... /* temp_node = find_middle_node(list); dump_node(temp_node); list = reverse_list(list); dump_list(list); temp_node = find_middle_node(list); */ } dump_node(temp_node); return 0;

// Allocate memory and store the data struct node* create_node(int data) { struct node *new_node = NULL; new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = data; new_node->next = NULL; return new_node; } // Deallocate the memory void free_this_node(struct node* pNode) { free(pNode); pNode = NULL; return; } // Insert a node at front end struct node* insert_at_front(struct node* head, struct node* new_node) { if(new_node) { new_node->next = head; return new_node; } else return NULL; } // Insert a node at tail end struct node* insert_at_end(struct node* head, struct node* new_node) { struct node *temp = NULL; if(head) { temp = head; while(temp->next) temp = temp->next; temp->next = new_node; } else { head = new_node; } return head; } // Remove a node from front end.. struct node* remove_from_front(struct node* head) { if(head) { head = head->next; return head; } else return NULL; }

// Reverse the list in single traversal.. struct node* reverse_list(struct node* head) { struct node *result = NULL, *temp_head = NULL, *temp = NULL; temp_head = head; while(temp_head) { temp = temp_head; temp_head = remove_from_front(temp_head); temp->next = NULL; result = insert_at_front(result, temp); } return result; } // Delete a node without the help of head node.. void delete_this_node(struct node* cur_node) { struct node *temp = NULL; if(cur_node && cur_node->next) { copy_node(cur_node, cur_node->next); temp = cur_node->next; cur_node->next = cur_node->next->next; free_this_node(temp); } else if(cur_node) { free_this_node(cur_node); } else return; } // Find n-th node from tail end in single traversal... struct node* find_nth_from_end(struct node *head, int n) { struct node *temp = NULL, *result = NULL, *temp_head = NULL; int i = 0; result = temp_head = temp = head; for(i = 0; i < n; i++) { if(!temp_head) { printf("Out of Range\n"); return NULL; } temp_head = temp_head->next; temp = temp->next; } while(temp_head) { temp_head = temp_head->next; temp = temp->next; result = result->next; } return result; } // Find middle node of list in single traversal... struct node* find_middle_node(struct node* head) { struct node *temp_head = NULL, *temp = NULL, *result = NULL; temp_head = temp = result = head; while(temp && temp->next) { if(temp->next->next) { temp = temp->next->next; result = result->next; } else temp = temp->next; } return result; } // Dump the list... void dump_list(struct node* head) { struct node *temp = NULL; printf("\n"); if(head) { temp = head; while(temp) { printf("%d ", temp->data);

} // Copy data in b/w two nodes... void copy_node(struct node* src, struct node* dest) { src->data = dest->data; return; } // Dump a node void dump_node(struct node* pNode) { if(pNode) printf("\nNode details : Addr : %x, data : %d\n", pNode, pNode->data); else printf("\n Node is NULL \n"); return; } //reverse PNODE revLinkList(PNODE pNode,PNODE prevNode) { PNODE pTop = NULL; if( pNode->next) pTop = revLinkList(pNode->next,pNode); else pTop = pNode; pNode->next=prevNode; return pTop; } one more question how to reverse block in singly linked list like 1->2->3->4->5->6->7->8->9->10->11->12 let us take block size as 3 so reversed list should be like this 3->2->1->6->5->4->9->8->7->12->11->10

} else { printf("Empty List"); } printf("\n");

temp = temp->next;

You might also like