Skip to content

Singly linked list insertion and traversal #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions LinkedList/singly_linked_list_and_insertion.c
Original file line number Diff line number Diff line change
@@ -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<stdio.h>
#include<stdlib.h>
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;
}