DS slip 3

Slip3_1 : program for implementing Linear Search method using function.

Program Code:

#include<stdio.h>
#include<conio.h>
void search(int a[],int n,int k)
{ int flag=0,i;
for(i=0;i<n;i++)
{ if(a[i]==k)
{ flag=1;
break;
}
} if(flag==1)
printf("\nElement is found at %d location ",i+1);
else
printf("\nElement is NOT found");
}
void main()
{ int a[20],sr,i,n;
printf("\nEnter how many elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\nEnter element : ");
scanf("%d",&a[i]);
}
printf("\nEnter element for search : ");
scanf("%d",&sr);
search(a,n,sr);
getch();
}

Output:


program output




Slip3_2 :Write a ‘C’ program to create linked list with given number in which data part of each node contains individual digit of the number. (Ex. Suppose the number is 584 then the nodes of linked list should contain 5, 8, 4.)

Program Code:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *head=NULL;
void create(int no)
{
NODE *s;
if(head==NULL)
{ head=(NODE *)malloc(sizeof(NODE));
head->data=no;
head->next=NULL;
}
else
{
for(s=head;s->next!=NULL;s=s->next);
s->next=(NODE*)malloc(sizeof(NODE));
s=s->next;
s->data=no;
s->next=NULL;
}
}
void display()
{
NODE *s;
for(s=head;s!=NULL;s=s->next)
{ printf("\t %d -> ",s->data);
}
}
void main()
{
int no,k;
int r=0;
printf("\nEnter The Number ");
scanf("%d", &no);
while(no>0)
{ k=no%10;
r=r*10+k;
no=no/10;
}
while(r>0)
{
k=r%10;
create(k);
r=r/10;
}
printf("\n link list is :");
display();
getch();
}

Output: 


DS slip 3 DS slip 3 Reviewed by Dinesh Varal on August 04, 2018 Rating: 5

No comments:

Powered by Blogger.