data structure diu All Lab Report0
data structure diu All Lab Report0
Problem 1:
Read 6 values that can be floating point numbers. After, print how many of them were positive. In the
next line, print the average of all positive values typed, with one digit after the decimal point.
Code:
#include<stdio.h>
int main(){
int i,total=0;
double x[5];
double sum=0,avg;
for(i=0;i<6;i++){
scanf("%lf",&x[i]);
}
for(i=0;i<6;i++){
if(x[i]>=0){
total++;
sum+=x[i];
}
avg=sum/total;
printf("%d Positive Numbers\nAverage %.1lf\n",total,avg);
Problem 2:
Make a program that reads five integer values. Count how many of these values are even and print
this information.
Code:
#include<stdio.h>
1
int main(){
int x[5];
int cnt=0,i;
for(i=0;i<5;i++){
scanf("%d",&x[i]);
}
for(i=0;i<5;i++){
if(x[i]%2==0){
cnt+=1;
}
}
printf("%d Even Numbers\n",cnt);
Function:
Problem 1:
Write a code to print the factorial of any positive integer.
Code:
#include<stdio.h>
int n;
scanf("%d",&n);
int result=fact(n);
printf("Factorial of %d = %d\n",n,result);
}
2
Problem 2:
Write a code to find out if a given number is prime or not.
Code:
#include<stdio.h>
}
3
Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
int main()
{
struct node *head,*second,*third;
head=(struct node*)malloc(sizeof(struct node));
head->data = 2;
//head->next =NULL;
head->next = second;
second->next = third;
third->next = NULL;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=lastnode;
scanf("%d",&pos);
printf("\n");
struct node *del;
del=head;
for(int i=1;i<pos-1 && del!= NULL;i++)
{
del=del->next;
}
struct node *hold;
hold=del->next->next;
free(del->next);
del->next=hold;
Problem 3:
Doubly Link List (all operation ) using Function:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *pre;
struct node *next;
};
temp->next->pre=newnode;
temp->next=newnode;
}
temp->next=hold;
hold->pre=temp;
}
Output:
9
Problem 4:
Stack using array and linked list.
Using array:
#include<stdio.h>
int array[5];
int top = -1;
array[++top] = data;
}
int pop()
{
if(top == -1){
printf("Stack is Empty.\n");
return -1;
}
array[top--];
}
int main()
{
push (5);
push (4);
push (2);
pop();
10
pop();
push(7);
Output:
Problem 5:
Using Linked list:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
head->data = data;
head -> link = NULL;
void pop()
{
if(top==NULL){
printf("Stack is Empty.\n");
return;
}
top= temp->link;
free(temp);
}
int main()
{
push(5);
push(7);
push(1);
pop();
temp = temp->link;
}
}
Output:
13
Problem 6:
Queue using array and link list.
#include<stdio.h>
#include<stdlib.h>
int q[100];
int f=-1,r=-1;
void dequeue(){
if(f==r && f!=-1){
f=-1,r=-1;
}
else if(f==-1 && r==-1){
printf("Array Underflow\n");
}
else{
f++;
}
}
void print(){
14
for(int i=f;i<=r;i++){
printf("%d\n",q[i]);
}
}
int main(){
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
dequeue();
print();
}
Output:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
new->data=data;
new->next=NULL;
if(f==NULL && r==NULL){
f=new,r=new;
}
else{
r->next=new;
r=new;
}
}
void dequeue(){
struct node *temp;
temp=f;
f=f->next;
free(temp);
}
void print(){
struct node *ptr;
ptr=f;
while(ptr!=NULL){
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
int main(){
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
dequeue();
print();
}
Output:
16
17
Problem 7:
BST with insert,search,update function.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left,*right;
};
}
};
inorder(node->left);
printf("%d ",node->data);
inorder(node->right);
}
}
printf("%d ",node->data);
preorder(node->left);
preorder(node->right);
}
}
postorder(node->left);
postorder(node->right);
printf("%d ",node->data);
}
}
int main(){
struct node *node=NULL;
node=insert(node,20);
node=insert(node,10);
node=insert(node,25);
node=insert(node,5);
19
node=insert(node,7);
update(node,25,24);
printf("Preorder : ");
preorder(node);
printf("\nPostorder : ");
postorder(node);
printf("\nInorder : ");
inorder(node);
Output:
Problem 8:
Stack using array and linked list.
Using array:
#include<stdio.h>
int array[5];
int top = -1;
printf("Stack is Full.\n");
return;
}
array[++top] = data;
}
int pop()
{
if(top == -1){
printf("Stack is Empty.\n");
return -1;
}
array[top--];
}
int main()
{
push (5);
push (4);
push (2);
pop();
pop();
push(7);
Output:
21
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
head->data = data;
head -> link = NULL;
void pop()
{
if(top==NULL){
printf("Stack is Empty.\n");
return;
}
top= temp->link;
free(temp);
}
int main()
{
push(5);
push(7);
push(1);
pop();
Output:
23
Problem 9:
Queue using array and link list.
#include<stdio.h>
#include<stdlib.h>
int q[100];
int f=-1,r=-1;
void dequeue(){
if(f==r && f!=-1){
f=-1,r=-1;
}
else if(f==-1 && r==-1){
printf("Array Underflow\n");
}
else{
f++;
}
}
void print(){
for(int i=f;i<=r;i++){
printf("%d\n",q[i]);
}
}
25
int main(){
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
dequeue();
print();
}
Output:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
else{
r->next=new;
r=new;
}
}
void dequeue(){
struct node *temp;
temp=f;
f=f->next;
free(temp);
}
void print(){
struct node *ptr;
ptr=f;
while(ptr!=NULL){
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
int main(){
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
dequeue();
print();
}
Output:
27