diff --git a/LinkedList/singly_linked_list_and_insertion.c b/LinkedList/singly_linked_list_and_insertion.c new file mode 100644 index 0000000..e2257fb --- /dev/null +++ b/LinkedList/singly_linked_list_and_insertion.c @@ -0,0 +1,66 @@ +//A Program in C to insert numbers in linked list and print the numbers by traversing the linked list +//user: namanvats +#include +#include +struct list{ + int data; + struct list *next; +}; +typedef struct list node; +node *head=NULL; +node *create() + { + node *temp; + temp=(node*)malloc(sizeof(node)); + temp->next=NULL; + return temp; + } +node* add(int value) + { + node *temp,*p; + temp=create(); + temp->data=value; + if(head==NULL) + { + head=temp; + } + else + { + p=head; + while(p->next!=NULL) + { + p=p->next; + } + p->next=temp; + } + } +void display() + { + node *p; + p=head; + while(p->next!=NULL) + { + printf("%d->",p->data); + p=p->next; + } + printf("%d",p->data); + } +int main() +{ + printf("type -1 to print and terminate else enter any number to add to linked list \n"); + while(1) + { + int value; + scanf("%d",&value); + if(value==-1) + { + display(); + break; + } + else + { + add(value); + } + } + return 0; +}