Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct node * addelement(struct node * ,int);
- void display(struct node *);
- struct node
- {
- int data;
- struct node *ptr;
- };
- int main()
- {
- int a,b;
- struct node *head;
- printf(" \n Enter the elements to be added to the linked list ");
- scanf("%d %d",&a,&b);
- head=NULL;
- head=addelement(head,a);
- head=addelement(head,b);
- display(head);
- }
- struct node * addelement(struct node *head,int info)
- {
- struct node *newnode;
- newnode=(struct node *)malloc(sizeof(struct node));
- newnode->data=info;
- if(head==NULL)
- {
- newnode->ptr=NULL;
- return newnode;
- }
- else
- {
- newnode->ptr=head;
- return newnode;
- }
- }
- void display(struct node *head)
- {
- while(head!=NULL)
- {
- printf("%d\t",head->data);
- head=head->ptr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement