0% found this document useful (0 votes)
16 views28 pages

data structure diu All Lab Report0

The document contains multiple programming problems and their corresponding C code solutions. It covers topics such as reading values, calculating averages, factorials, prime number checks, linked lists, stacks, queues, and binary search trees. Each problem is accompanied by code snippets that demonstrate the implementation of the required functionality.

Uploaded by

6yzdahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views28 pages

data structure diu All Lab Report0

The document contains multiple programming problems and their corresponding C code solutions. It covers topics such as reading values, calculating averages, factorials, prime number checks, linked lists, stacks, queues, and binary search trees. Each problem is accompanied by code snippets that demonstrate the implementation of the required functionality.

Uploaded by

6yzdahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Array:

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 fact(int n){


if(n==1){return 1;}
return (n*fact(n-1));
}
int main(){

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>

int prime(int n){


for(int i=2;i*i<=n;i++){
if(n%i==0){
printf("%d is not a prime number.\n",n);
return 0;
}
}
printf("%d is a prime number.\n",n);
}
int main(){
int n;
scanf("%d",&n);
prime(n);

}
3

PROBLEM 2: Singly linked list (all) without function.

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;

second=(struct node*)malloc(sizeof(struct node));


second->data = 3;
//second->next = NULL;

third=(struct node*)malloc(sizeof(struct node));


third->data = 5;

head->next = second;
second->next = third;
third->next = NULL;

//insert First Node


struct node *newNode;
newNode=(struct node*)malloc(sizeof(struct node));
newNode->data = 1;
newNode->next= head;
head= newNode;

//Insert Last Node


struct node *lastnode;
lastnode=(struct node*)malloc(sizeof(struct node));
lastnode->data = 6;
lastnode->next = NULL;
struct node *temp;
temp= head;
4

while(temp->next!=NULL)
{
temp=temp->next;
}

temp->next=lastnode;

//Insert at any position


int n;
printf("Enter the position to insert the node:\n");
scanf("%d",&n);
struct node *midnode,*temp4=head;
midnode=(struct node*)malloc(sizeof(struct node));
midnode->data=4;
midnode->next=NULL;

for(int j=1;j<n-1 && temp4->next!=NULL;j++){


temp4=temp4->next;
}
midnode->next=temp4->next;
temp4->next=midnode;

//Delete First Node


struct node *temp1;
temp1=head;
head=head->next;
free(temp1);
temp1=NULL;

//Delete Last Node


struct node *temp2, *temp3;
temp2=head;
temp3=head;
while(temp2->next!=NULL)
{
temp3=temp2;
temp2=temp2->next;
}
free(temp2);
temp3->next=NULL;
temp2=NULL;
//Delete at any position
printf("Enter the position to delete the node: ");
int pos;
5

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;

struct node *Print;


Print = head;
while(Print!=NULL)
{
printf("%d\n", Print->data);
Print = Print->next;
}
return 0;
}
6

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;
};

struct node *creatnode(int data){


struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->pre=NULL;
newnode->next=NULL;
return newnode;
};

void insertfirst(struct node **head,int data){


struct node *newnode=creatnode(data);
struct node *temp=*head;
newnode->next=*head;
newnode->pre=NULL;
if(*head!=NULL){
temp->pre=newnode;
}
*head=newnode;
}
void insertmid(struct node **head,int data,int pos){
struct node *newnode=creatnode(data);
struct node *temp=*head;
for(int i=1;i<pos-1;i++){
temp=temp->next;
if(temp==NULL){
printf("Position is out of the range.\n");
return;
}
}
newnode->next=temp->next;
newnode->pre=temp;
7

temp->next->pre=newnode;
temp->next=newnode;
}

void insertlast(struct node **head,int data){


struct node *newnode=creatnode(data);
struct node *temp=*head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
newnode->pre=temp;
newnode->next=NULL;

void deletefirst(struct node **head){


if(*head==NULL){
printf("Nothing to delete.\n");
}
struct node *temp=*head;
*head=temp->next;
struct node *temp1=*head;
temp1->pre=NULL;
free(temp);
}

void deletelast(struct node **head){


struct node *temp1=*head;
while(temp1->next->next!=NULL){
temp1=temp1->next;
}
free(temp1->next);
temp1->next=NULL;
}

void deletemid(struct node **head,int data){


struct node *temp=*head;
struct node *hold;
while(temp->next->data!=data){
temp=temp->next;
}
hold=temp->next->next;
free(temp->next);
8

temp->next=hold;
hold->pre=temp;
}

void print(struct node *head){


struct node *ptr=head;
while(ptr!=NULL){
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
int main(){
struct node *head=NULL;
insertfirst(&head,5);
insertfirst(&head,3);
//insertlast(&head,5);
//insertmid(&head,4,3);
//deletefirst(&head);
//deletelast(&head);
insertlast(&head,7);
insertfirst(&head,10);
int dlt;
//printf("Enter data to delete.\n");
//scanf("%d",&dlt);
//deletemid(&head,dlt);
print(head);
}

Output:
9

Problem 4:
Stack using array and linked list.

Using array:
#include<stdio.h>

int array[5];
int top = -1;

void push(int data)


{
if(top==4){
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();
10

pop();
push(7);

for(int i=top; i>=0 ;i--){


printf("%d ",array[i]);
}

Output:

Problem 5:
Using Linked list:

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *link;
};

struct node *top = NULL;


11

void push(int data)


{
struct node *head;
head = (struct node*) malloc (sizeof(struct node));

head->data = data;
head -> link = NULL;

head -> link = top;


top = head;
}

void pop()
{
if(top==NULL){
printf("Stack is Empty.\n");
return;
}

struct node *temp;


temp=top;

top= temp->link;
free(temp);
}

int main()
{
push(5);
push(7);
push(1);

pop();

struct node *temp;


temp = top;
while(temp!=NULL){
printf("%d ",temp->data );
12

temp = temp->link;
}
}

Output:
13

Problem 6:
Queue using array and link list.

Queue using array:

#include<stdio.h>
#include<stdlib.h>

int q[100];
int f=-1,r=-1;

void enqueue(int data){


if(r==100-1){
printf("Array Overflow\n");
}
else{
if(f==-1 && r==-1){
f=0,r=0;
q[r]=data;
}
else{
r++;
q[r]=data;
}
}
}

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:

Queue using link list:

#include<stdio.h>
#include<stdlib.h>

struct node{
int data;
struct node *next;
};

struct node *f=NULL,*r=NULL;

void enqueue(int data){


struct node *new=(struct node *)malloc(sizeof(struct node));
15

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;
};

struct node *creatnode(int data){


struct node *newn=(struct node *)malloc(sizeof(struct node));
newn->data=data;
newn->left=NULL;
newn->right=NULL;
return newn;
}

struct node *insert(struct node *node,int data){


if(node==NULL){
return creatnode(data);
}
if(data<node->data){
node->left=insert(node->left,data);
}
else{
node->right=insert(node->right,data);
}
return node;
}

struct node *search(struct node *node,int target){


if (node->data==target){
return node;
}
if(node->data<target){
return search(node->right,target);
}
if(node->data>target){
return search(node->left, target);
}
if(node==NULL){
printf("%d is not in the tree.\n");
18

}
};

void update(struct node *node,int target,int value){


struct node *temp=(struct node *)malloc(sizeof(struct node));
temp=search(node,target);
temp->data=value;
}
void inorder(struct node *node){
if(node!=NULL){

inorder(node->left);
printf("%d ",node->data);
inorder(node->right);

}
}

void preorder(struct node *node){


if(node!=NULL){

printf("%d ",node->data);
preorder(node->left);
preorder(node->right);

}
}

void postorder(struct node *node){


if(node!=NULL){

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;

void push(int data)


{
if(top==4){
20

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);

for(int i=top; i>=0 ;i--){


printf("%d ",array[i]);
}

Output:
21

Using Linked list:

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *link;
};

struct node *top = NULL;

void push(int data)


{
struct node *head;
head = (struct node*) malloc (sizeof(struct node));

head->data = data;
head -> link = NULL;

head -> link = top;


top = head;
}
22

void pop()
{
if(top==NULL){
printf("Stack is Empty.\n");
return;
}

struct node *temp;


temp=top;

top= temp->link;
free(temp);
}

int main()
{
push(5);
push(7);
push(1);

pop();

struct node *temp;


temp = top;
while(temp!=NULL){
printf("%d ",temp->data );
temp = temp->link;
}
}

Output:
23

Problem 9:
Queue using array and link list.

Queue using array:


24

#include<stdio.h>
#include<stdlib.h>

int q[100];
int f=-1,r=-1;

void enqueue(int data){


if(r==100-1){
printf("Array Overflow\n");
}
else{
if(f==-1 && r==-1){
f=0,r=0;
q[r]=data;
}
else{
r++;
q[r]=data;
}
}
}

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:

Queue using link list:

#include<stdio.h>
#include<stdlib.h>

struct node{
int data;
struct node *next;
};

struct node *f=NULL,*r=NULL;

void enqueue(int data){


struct node *new=(struct node *)malloc(sizeof(struct node));
new->data=data;
new->next=NULL;
if(f==NULL && r==NULL){
f=new,r=new;
}
26

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

You might also like