Problem statement
Write a c program to store numbers using link list and then print them,the new number entered should be at the top of list
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
};
struct node* scan(int n);
void print(struct node *head);
int main()
{
struct node *head;
int n;
head=NULL;
printf("enter number of elements in the link list ");
scanf("%d",&n);
head=scan(n);
printf("the elements entered by you is ");
print(head);
return 0;
}
void print(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
return;
}
struct node* scan(int n)
{
int i,temp;
struct node *newnode,*head;
head=NULL; // this is a good practice to initialise pointer by NULL every tme
for(i=0;i<n;i++)
{
printf("enter element ");
scanf("%d",&temp);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=temp;
newnode->next=head;
head=newnode;
}
return head;
}
Write a c program to store numbers using link list and then print them,the new number entered should be at the top of list
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node * next;
};
struct node* scan(int n);
void print(struct node *head);
int main()
{
struct node *head;
int n;
head=NULL;
printf("enter number of elements in the link list ");
scanf("%d",&n);
head=scan(n);
printf("the elements entered by you is ");
print(head);
return 0;
}
void print(struct node *head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
return;
}
struct node* scan(int n)
{
int i,temp;
struct node *newnode,*head;
head=NULL; // this is a good practice to initialise pointer by NULL every tme
for(i=0;i<n;i++)
{
printf("enter element ");
scanf("%d",&temp);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=temp;
newnode->next=head;
head=newnode;
}
return head;
}
No comments:
Post a Comment