23/06/2025
Single Linked List /****************************************************************************** Online C Compiler. Code, Compile, Run and Debug C program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ /* Include Files */ struct node { int data; struct node *link; }; int count_tot_no_nodes(struct node *address) { int count=0; if(address->link==NULL) { printf("Linked List is empty.\n\r"); return count; } else { while(address->link!=NULL) { count++; address = address->link; } return ++count; } } void printLinkedListData(struct node *Address) { int count=1; if(Address->link== NULL) { printf("\nNode %d - %x",count++, Address->data); } else { while(Address->link!= NULL) { printf("\nNode %d - %x",count++, Address->data); Address = Address->link; } printf("\nNode %d - %x",count++, Address->data); Address = Address->link; } } void initNode() { struct node *head; int b =sizeof(struct node); head = (struct node *)malloc(b); head->data = 0x55AA55AA; head->link = head; } void add_at_end_node(struct node *address, int data) { struct node *end_node = (struct node *)malloc(sizeof(struct node)); end_node->data = data; end_node->link = NULL; while(address->link != NULL) { address = address->link; } address->link = end_node; } void add_at_any_position(struct node *address, int data, int pos) { struct node *temp_node = (struct node *)malloc(sizeof(struct node)); temp_node->data = data; temp_node->link = NULL; while(pos--) { address=address->link; } temp_node->link= address->link; address->link = temp_node; } void delete_node(struct node *address,int pos) { struct node *previous; pos--; while(pos--) { previous=address; address=address->link; } struct node *temp = address; previous->link = address->link; free(temp); } void delete_entire_linkedList(struct node *address) { struct node *temp; while(address->link != NULL) { temp=address; address= address->link; free(temp); } free(address); } int main(void) { printf("\n Welcome to ArunEworld"); printf("\n Single Linked List Example"); struct node *head; int b =sizeof(struct node); head = (struct node *)malloc(b); head->data = 0xAAAAAAAA; head->link = head; struct node *current = (struct node *)malloc(b); current->data = 0xBBBBBBBB; current->link = NULL; head->link= current; current = (struct node *)malloc(b); current->data = 0xCCCCCCCC; current->link = NULL; head->link->link= current; printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); add_at_end_node(head,0xDDDDDDDD); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); add_at_end_node(head,0xEEEEEEEE); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); add_at_any_position(head, 0xFFFFFFFF, 2); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); add_at_any_position(head, 0xBBBBBBBB, 2); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); delete_node(head,2); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); delete_node(head,4); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); printLinkedListData(head); delete_entire_linkedList(head); printf("\nLinked List counting - %d",count_tot_no_nodes(head)); free(head); printLinkedListData(head); printf("\n____________________________________________"); return 0; }
Single Linked List