From 442e76d9169ecddc571e0e087259944bc34576a4 Mon Sep 17 00:00:00 2001 From: Jishu Sengupta <99582530+voiDXDops@users.noreply.github.com> Date: Sat, 29 Oct 2022 20:09:49 +0530 Subject: [PATCH] Added Code to insert node from the end. --- insert_end.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 insert_end.c diff --git a/insert_end.c b/insert_end.c new file mode 100644 index 0000000..31f5c8d --- /dev/null +++ b/insert_end.c @@ -0,0 +1,95 @@ +#include +#include +#include + +struct node +{ + int data; + struct node *next; +}; + +struct node *start = NULL; +struct node *create_ll(struct node *); +struct node *display(struct node *); +struct node *insert_end(struct node *); + +int main() +{ + int choice; + do + { + printf("\nEnter 1 to create list, \n2 to insert data from the end, \n3 to display \nand 4 to end!\n:-"); + scanf("%d",&choice); + switch(choice) + { + case 1:start= create_list(start); + printf("Linked List Created: "); + break; + + case 2:start= insert_end(start); + break; + + case 3:start= display(start) + break; + } + }while(choice !=4) + return 0; +} + +struct node *create_ll(struct node *start) +{ + struct node *new_node, *ptr; + int num; + printf("\nEnter -1 to end\nEnter the Data:"); + scanf("%d",&num); + while(num!=-1) + { + new_node=(struct node*)malloc(sizeof(struct node)); + new_node->data=num; + if(start=NULL) + { + new_node->next=NULL; + start=new_node; + } + else + { + ptr=start; + while(ptr->next!=NULL) + {ptr=ptr->next;} + ptr->next = new_node; + new_node->next=NULL; + + } + printf("\nEnter the data: "); + scanf("%d",&num); + } + return start; +} + +struct node *display(struct node *start) +{ + struct node *ptr; + ptr=start; + while(ptr!=NULL) + { + printf("\t %d", ptr->data); + ptr=ptr->next; + } + return start; +} + +struct node *insert_end(struct node *start) +{ + struct node *new_node, *ptr; + int num; + printf("\nEnter the Data to be inserted from the end:"); + scanf("%d",&num); + new_node=(struct node *)malloc(sizeof(struct node)); + new_node->data=num; + new_node->next=NULL; + ptr=start; + while(prt->next !=NULL) + {prt=ptr->next;} + ptr->next = new_node; + return start; +}