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;
- };
- struct head
- {
- struct node *head_ptr;
- int num;
- };
- int main()
- {
- struct head a;
- int i,data;
- a.head_ptr=NULL;
- printf("\n Enter the number of elements to be entered to the list :");
- scanf("%d",&a.num);
- for(i=1;i<=a.num;i++)
- {
- printf("\n Enter the elements onto the list ");
- scanf("%d",&data);
- a.head_ptr=addelement(a.head_ptr,data);
- }
- display(a.head_ptr);
- }
- 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;
- }
- }
- 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