0% found this document useful (0 votes)
28 views62 pages

DS Lab Assignment

Uploaded by

mohdtaha22509
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views62 pages

DS Lab Assignment

Uploaded by

mohdtaha22509
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 62

Ques 1:Write a program that uses a function to perform following

operations on linear array i)Creation ii)Insertion iii)Deletion


iv)Traversal.
#include <stdio.h>
int main()
{

int arr[20];
printf("Enter 6 elements to the array \n");
for (int i = 0; i < 6; i++)
{
scanf("%d", &arr[i]);
}
printf("Array created\n");

int capacity = sizeof(arr) / sizeof(arr[0]);


int n = 6;
int i, key;
printf("\nInsert an element\n");

scanf("%d", &key);

printf("\n Before Insertion:\n");

for (i = 0; i < n; i++)


printf("%d ", arr[i]);

n = insertSorted(arr, n, key, capacity);

printf("\n After Insertion: \n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\nArray before deletion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);

n = deleteElement(arr, n, key);

printf("\nArray after deletion\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

return 0;
}
int insertSorted(int arr[], int n,int a,int c)
{
if (n >= c)
return n;
arr[n] = a;

return (n + 1);
}

int findElement(int arr[], int n,


int key);

int deleteElement(int arr[], int n,


int key)
{
int pos = findElement(arr, n, key);

if (pos == -1)
{
printf("Element not found");
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];

return n - 1;
}

int findElement(int arr[], int n, int key)


{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;

return -1;
}
Output of the program:
Ques 2: write a program to implement addition of 2d arrays.
#include<stdio.h>

int main ()
{
int A[3][3],B[3][3],C[3][3],i,j;
printf("Enter the elements of 2D Array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("Enter the elements of 2D Array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&B[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
C[i][j]=A[i][j]+B[i][j];
}

printf("After adding both the 2D Arrays we get\n");


for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t ",C[i][j]);
}
printf("\n");
}

return 0;
}
Output of the program:
Ques 3: Write a program to implement multiplication of 2D arrays.
#include <stdio.h>

int main()
{
int r, c, r2, c2, i, j, k, sum = 0;
int a[10][10], b[10][10], mul[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &r, &c);
printf("Enter the elements of first matrix\n");

for ( i = 0 ; i < r ; i++ )


for ( j = 0 ; j < c ; j++ )
scanf("%d", &a[i][j]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &r2, &c2);

if ( c != r2 )
printf("enter only square matrices for multiplication.\n");
else
{
printf("Enter the elements of second matrix\n");

for ( i = 0 ; i < r2 ;i++ )


for ( j = 0 ; j < c2 ; j++ )
scanf("%d", &b[i][j]);

for ( i = 0 ; i < r ; i++ )


{
for ( j = 0 ; j < c2 ; j++ )
{
for ( k = 0 ; k < r2 ; k++ )
{
sum = sum + a[i][k]*b[k][j];
}

mul[i][j] = sum;
sum = 0;
}
}

printf("The multiplication of entered matrices is :-\n");

for ( i = 0 ; i < r ; i++ )


{
for ( j = 0 ; j < c2 ; j++ )
printf("%d\t", mul[i][j]);
printf("\n");
}
}

return 0;
}

Output of the program:


Ques 4: write a program to perform transpose of a matrix.
#include <stdio.h>
int main() {
int A[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &A[i][j]);
}

// printing the matrix a[][]


printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", A[i][j]);
if (j == c - 1)
printf("\n");
}

// computing the transpose


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = A[i][j];
}

// printing the transpose


printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Output of the program:
Ques 5: Write a program to concatenate two strings.
#include <stdio.h>
int main()
{
char s1[100] = " My Name ", s2[] = "is Tasneem";
int l, j;

l = 0;
while (s1[l] != '\0')
{
++l;
}

for (j = 0; s2[j] != '\0'; ++j, ++l)


{
s1[l] = s2[j];
}
s1[l] = '\0';

printf("After String concatenation: ");


puts(s1);
return 0;
}

Output of the program:


Ques 6: Write a program to check string is palindrome or not.
#include <string.h>

int main()
{
char c[1000];
int i,n,x=0;

printf("Enter the string : ");


gets(c);
n=strlen(c);

for(i=0;i<n/2;i++)
{
if(c[i]==c[n-i-1])
x++;

}
if(x==i)
printf("The given string is palindrome");
else
printf("The given string is not palindrome");

return 0;
}

Output of the program:


Ques 7: Write a program that uses functions to perform the
following operations on singly linked list.

i)creation
ii)Insertion
iii)Deletion
iv)Traversal

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

void create();
void display();
void insert_pos();
void delete_pos();
struct node
{
int info;
struct node *next;
};
struct node *start = NULL;
int main()
{
int choice;
while (1)
{

printf("\n MENU \n");


printf("\n 1.Create ");
printf("\n 2.Display \n");
printf("\n 3.Insert at specified position ");
printf("\n 4.Delete from specified position ");
printf("\n 9.Exit ");
printf("\n--------------------------------------");
printf("\nEnter your choice:\t");
scanf("%d", &choice);
switch (choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_pos();
break;

case 4:
delete_pos();
break;

case 9:
exit(0);
break;

default:
printf("\n Wrong Choice:\n");
break;
}
}
return 0;
}
void create()
{
struct node *temp, *ptr;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\nOut of Memory Space:");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d", &temp->info);
temp->next = NULL;
if (start == NULL)
{
start = temp;
}
else
{
ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
void display()
{
struct node *ptr;
if (start == NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr = start;
printf("\nThe List elements are:\n");
while (ptr != NULL)
{
printf("%d\t", ptr->info);
ptr = ptr->next;
}
}
}

void insert_pos()
{
struct node *ptr, *temp;
int i, pos;
temp = (struct node *)malloc(sizeof(struct node));
if (temp == NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("nEnter the position for the new node to be inserted:\t");
scanf("%d", &pos);
printf("\nEnter the data value of the node:\t");
scanf("%d", &temp->info);

temp->next = NULL;
if (pos == 0)
{
temp->next = start;
start = temp;
}
else
{
for (i = 0, ptr = start; i < pos - 1; i++)
{
ptr = ptr->next;
if (ptr == NULL)
{
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->next = ptr->next;
ptr->next = temp;
}
}

void delete_pos()
{
int i, pos;
struct node *temp, *ptr;
if (start == NULL)
{
printf("nThe List is Empty:n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d", &pos);
if (pos == 0)
{
ptr = start;
start = start->next;
printf("\nThe deleted element is:%d\t", ptr->info);
free(ptr);
}
else
{
ptr = start;
for (i = 0; i < pos; i++)
{
temp = ptr;
ptr = ptr->next;
if (ptr == NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->next = ptr->next;
printf("\nThe deleted element is:%d\t", ptr->info);
free(ptr);
}
}
}
Output of the program:
Ques 8: Write a program that uses functions to perform the
following operations on Doubly linked list.
i) Creation
ii) Insertion
iii) Deletion
iv) Traversal
#include <stdio.h>
#include <stdlib.h>

struct node
{
struct node *prev;
int n;
struct node *next;
} * h, *temp, *temp1, *temp2, *temp4;

void insert1();
void insert2();
void traversebeg();
void delete ();
int count = 0;

void main()
{
int ch;

h = NULL;
temp = temp1 = NULL;

printf("\n 1 - Insert at beginning");


printf("\n 2 - Insert at end");
printf("\n 3 - Delete from specified location ");
printf("\n 4 - Display from beginning");
printf("\n 5 - Exit");

while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
delete ();
break;
case 4:
traversebeg();
break;
case 5:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}
/* TO create an empty node */
void create()
{
int data;

temp = (struct node *)malloc(1 * sizeof(struct node));


temp->prev = NULL;
temp->next = NULL;
printf("\n Enter value to node : ");
scanf("%d", &data);
temp->n = data;
count++;
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
/* To delete an element */
void delete ()
{
int i = 1, pos;

printf("\n Enter position to be deleted : ");


scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1))


{
printf("\n Error : Position out of range to delete");
return;
}
if (h == NULL)
{
printf("\n Error : Empty list no elements to delete");
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next;
if (i == 1)
h = temp2->next;
printf("\n Node deleted");
free(temp2);
}
count--;
}
/* Traverse from beginning */
void traversebeg()
{
temp2 = h;

if (temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");

while (temp2->next != NULL)


{
printf(" %d ", temp2->n);
temp2 = temp2->next;
}
printf(" %d ", temp2->n);
}

Output of program:
Ques 9: Write a program that uses functions to perform the
following operations on Circular linked list.
i) Creation
ii) Insertion
iii) Deletion
iv) Traversal
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert();
void lastinsert();
void begin_delete();
void last_delete();
void display();
void main()
{
int choice = 0;
while (choice != 7)
{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beg
inning\n4.display the list \n5.Exit.\n");
printf("\nEnter your choice?\n");
scanf("\n%d", &choice);
switch (choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
begin_delete();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
head = ptr;
ptr->next = head;
}
else
{
temp = head;
while (temp->next != head)
temp = temp->next;
ptr->next = head;
temp->next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
getch();
}
}
void lastinsert()
{
struct node *ptr, *temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d", &item);
ptr->data = item;
if (head == NULL)
{
head = ptr;
ptr->next = head;
}
else
{
temp = head;
while (temp->next != head)
{
temp = temp->next;
}
temp->next = ptr;
ptr->next = head;
}

printf("\nnode inserted\n");
getch();
}
}

void begin_delete()
{
struct node *ptr;
if (head == NULL)
{
printf("\nUNDERFLOW");
}
else if (head->next == head)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
getch();
}

else
{
ptr = head;
while (ptr->next != head)
ptr = ptr->next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");
getch();
}
}

void display()
{
struct node *ptr;
ptr = head;
if (head == NULL)
{
printf("\nnothing to print");
getch();
}
else
{
printf("\n printing values ... \n");

while (ptr->next != head)


{

printf("%d\n", ptr->data);
ptr = ptr->next;
}
printf("%d\n", ptr->data);
getch();
}
}

Output of the program:


Ques 10: Write a program that implement Stack and its Operations
(push and pop) using arrays.
#include<stdio.h>
#include<stdlib.h>

#define MAX 5

int top=-1,stack[MAX];
void push();
void pop();

void main()
{
int ch;
while(1)
{
printf("\n---Stack Menu---\n");
printf("\n\n1.Push\n2.Pop\n3.Exit");
printf("\n\tEnter your choice from above :");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: exit(0);
default: printf("\nWrong Choice!");
}
}
}

void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
printf("Node is Inserted\n");
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is empty!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}

Output of the program:


Ques 11: Write a program that implement Stack and its operations
using linked list (pointers).
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}
*top = NULL;
void push(int);
void pop();

int main()
{
int choice, value;
printf("\n---Menu---\n");
while(1){
printf("1. Push\n2. Pop\n3. Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;

case 2: pop();
break;

case 3: exit(0);
break;

default: printf("\n Wrong Choice\n");


}}}

void push(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("Node is Inserted\n");
}
void pop()
{
if(top == NULL)
printf("\nEmpty Stack !\n");
else{
struct Node *temp = top;
printf("\nPopped Element : %d", temp->data);
printf("\n");
top = temp->next;
free(temp);
}
}

Output of the program:


Ques 12: Write a program for conversion of infix to postfix using
stack.
#include<stdio.h>
#include<ctype.h>
char stack[50];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char expression[50];
char *e, x;
printf("Enter the expression : ");
scanf("%s",expression);
printf("\n");
e = expression;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}

Output of the program:


Ques 13: Write a program for checking balanced parenthesis in an
expression using Stacks.
#include<stdio.h>
int main()
{
char expression[50];
int x=0, i=0;
printf("\nEnter an expression to check whether it is balanced or not\
n");
scanf("%s", expression);
while(expression[i]!= '\0')
{
if(expression[i]=='(')
{
x++;
}
else if(expression[i]==')')
{
x--;
if(x<0)
break;
}
i++;
}
if(x==0)
{
printf("Expression is balanced");
}
else
{
printf("Expression is unbalanced");
}
return 0;
}
Output of the program:
Ques 14: Write a program to create Fibonacci series using recursive
function.
#include<stdio.h>
int fseries(int);

int main(void)
{
int a;
printf("Enter total no. of terms u want to print: ");
scanf("%d", &a);
for(int i = 0; i < a; i++)
{
printf("%d ", fseries(i));
}
return 0;
}
int fseries(int x)
{
if(x == 0 || x == 1)
{
return x;
}

else
{
return fseries(x-1) + fseries(x-2);
}

Output of the program:


Ques 15: Write a program to calculate factorial of a number using
recursive function.
#include <stdio.h>
int getFact(int N);
int main(){
int n;
printf("Enter a number to get factorial \n");
scanf("%d",&n);

printf("Factorial of %d is %d", n, getFact(n));

getch();
return 0;
}

int getFact(int n){

if(n <= 1){


return 1;
}
return n * getFact(n - 1);
}

Output of the program:


Ques 16: Write a program to implement Queue and its operations
using arrays.

#include <stdio.h>
#define MAX 10

void insert();
void delete();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int c;
while (1)
{
printf("\n---Queue Menu---\n");
printf("\n\n1.Insert element\n2.delete element\n3.Exit");
printf("\n\tEnter your choice from above :");
scanf("%d", &c);
switch (c)
{
case 1:
insert();
break;
case 2:
delete();
break;

case 3:
exit(1);
default:
printf("Wrong choice \n");
}
}
}
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Insert the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}

void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]
);
front = front + 1;
}
}

Output of the program:


Ques 17: Write a program that implement Queue and its operations
using Linked list (pointers).
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}
*rear,*front,*temp,*newNode;

void create()
{
front = rear = NULL;
}

void enqueue(int data)


{
newNode = (struct node*) malloc(sizeof(struct node));
newNode -> data = data;
newNode -> next = NULL;
if(rear != NULL)
{
rear -> next = newNode;
rear = newNode;
}
else
front = rear = newNode;
}
int dequeue()
{
int data;
data = front -> data;
temp = front;
front = front -> next;
free(temp);
if(front == NULL)
rear = NULL;
return data;
}

int empty()
{
if(front == NULL)
{
return 1;
}
else
return 0;
}

int main()
{
int num,choice;
while(1)
{
printf("\n--- Queue Menu ---");
printf("\n1. Enqueue\n2. Dequeue\n3. Exit\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("\n Insert the element in Queue : ");
scanf("%d",&num);
enqueue(num);
break;
case 2:
if(!(empty()))
printf("\nDequeued element : %d",dequeue());
break;
case 3:
exit(1);
break;
default:
printf("\nwrong choice\n");
}}
return 0;
}
Output of the program:
Ques 18: Write a program that implement circular Queue using
arrays.
# include<stdio.h>
# define MAX 10

int cqueue_arr[MAX];
int front = -1;
int rear = -1;

void insert(int item)


{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}

void del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}

void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}

int main()
{
int choice,item;
do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");

printf("Enter your choice : ");


scanf("%d",&choice);

switch(choice)
{
case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &item);

insert(item);
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=4);

return 0;
}

Output of the program:


Ques 19: Write a that implement the Binary search.
#include <stdio.h>
int main()
{
int i,l, h, m, n, a, arr[50];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d elements \n", n);
for(i = 0; i < n; i++)
scanf("%d",&arr[i]);
printf("Enter the value to be find\n");
scanf("%d", &a);
l = 0;
h = n - 1;
m = (l+h)/2;
while (l <= h)
{
if(arr[m] < a)
l = m + 1;
else if (arr[m] == a)
{
printf("%d found at location %d \n", a, m+1);
break;
}
else
h = m- 1;
m = (l + h)/2;
}
if(l > h)
printf("Element not found\n", a);
return 0;
}
Output of the program:
Ques 20: Write a program that implements the Linear Search.
#include<stdio.h>

int main()
{
int a[50],i,x,n;
printf("Enter the no. of elements\n");
scanf("%d",&n);

printf("Enter %d elements:\n",n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);

printf("\nEnter element to search: ");


scanf("%d",&x);

for(i=0;i<n;++i)
if(a[i]==x)
break;

if(i<n)
printf("%d is found at location %d",x,i+1);
else
printf("Element not found");

return 0;
}

Output of the program:


Ques 21: Write a program that implement bubble sort.
#include<stdio.h>
int main()
{
int arr[50], n, i, j, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d Numbers:\n", n);
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(i = 0 ; i < n - 1; i++)
{
for(j = 0 ; j < n-i-1; j++)
{
if(arr[j] > arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("Sorted Array is:");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

Output of the program:


Ques 22: Write a program that implements selection sort.

#include <stdio.h>
int main()
{
int a[50], n, i, j, pos, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d Numbers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
pos=i;
for(j = i + 1; j < n; j++)
{
if(a[pos] > a[j])
pos=j;
}
if(pos != i)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf("Sorted Array: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}

Output of the program:


Ques 23: Write a program that implement the Quick sort.
#include<stdio.h>
void quicksort(int a[50],int x,int y)
{
int i, j, pivot, temp;

if(x<y){
pivot=x;
i=x;
j=y;

while(i<j){
while(a[i]<=a[pivot] && i<y)
i++;
while(a[j]>a[pivot])
j--;
if(i<j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,x,j-1);
quicksort(a,j+1,y);

}
}

int main()
{
int i, n, a[50];

printf("Enter no.of elements:\n ");


scanf("%d",&n);

printf("Enter %d elements:\n ",n);


for(i=0;i<n;i++)
scanf("%d",&a[i]);

quicksort(a,0,n-1);

printf("Sorted Array: ");


for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
}

Output of the program:


Ques 24: Write a program that implements insertion sort.
#include<stdio.h>

int main() {
int i, j, n, temp, arr[50];

printf("Enter total elements:\n ");


scanf("%d", &n);

printf("Enter %d elements:\n ", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

for (i = 1; i < n; i++) {


temp = arr[i];
j = i - 1;
while ((temp < arr[j]) && (j >= 0)) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}

printf("After Sorting: ");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}

Output of the program:


Ques 25: Write a program that implements the merge sort.

#include<stdio.h>

void mergesort(int a[],int i,int j);


void merge(int a[],int i1,int j1,int i2,int j2);

int main()
{
int a[50],n,i;
printf("Enter no of elements:\n");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}

void mergesort(int a[],int i,int j)


{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];

for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

Output of the program:


Ques 26: Write a program that implements heap sort.
#include<stdio.h>

void create(int []);


void adjust_down(int [],int);

void main()
{
int heap[50],n,i,end,temp;
printf("Enter no. of elements:\n");
scanf("%d",&n);
printf("\nEnter elements:\n");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
heap[0]=n;
create(heap);
while(heap[0] > 1)
{
end=heap[0];
temp=heap[1];
heap[1]=heap[end];
heap[end]=temp;
heap[0]--;
adjust_down(heap,1);
}

printf("\nArray after sorting:\n");


for(i=1;i<=n;i++)
printf("%d ",heap[i]);
}

void create(int heap[])


{
int i,n;
n=heap[0];
for(i=n/2;i>=1;i--)
adjust_down(heap,i);
}

void adjust_down(int heap[],int i)


{
int j,temp,n,flag=1;
n=heap[0];
while(2*i<=n && flag==1)
{
j=2*i;
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}

Output of the program:


Ques 27: Write the program to perform following operations on
Binary Search Tree .
i) Insert an element.
ii) Delete an element
iii) Search for a key element.

Insert an Element
#include<stdio.h>
#include<stdlib.h>

void insert(int);
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
insert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void insert(int item)
{
struct node *ptr, *parentptr , *nodeptr;
ptr = (struct node *) malloc(sizeof (struct node));
if(ptr == NULL)
{
printf("can't insert elements");
}
else
{
ptr -> data = item;
ptr -> left = NULL;
ptr -> right = NULL;
if(root == NULL)
{
root = ptr;
root -> left = NULL;
root -> right = NULL;
}
else
{
parentptr = NULL;
nodeptr = root;
while(nodeptr != NULL)
{
parentptr = nodeptr;
if(item < nodeptr->data)
{
nodeptr = nodeptr -> left;
}
else
{
nodeptr = nodeptr -> right;
}
}
if(item < parentptr -> data)
{
parentptr -> left = ptr;
}
else
{
parentptr -> right = ptr;
}
}
printf("Node Inserted");
}
}

Output of the program:


Delete an Element
#include<stdio.h>
#include<stdlib.h>

/* Structure of the node */


struct node
{
int data;
struct node *left, *right;
}*root;

// Function to create a new node


struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}

/* Function to find the minimum value node */


struct node * minValueNode(struct node* node)
{
struct node* current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
current = current->left;
return current;
}

/* Function to delete the given node */


struct node* delete_node(struct node* root, int data)
{
if (root == NULL)
return root;
// If the key to be deleted is smaller than the root's key,
if (data < root->data)
root->left = delete_node(root->left, data);
// If the key to be deleted is greater than the root's key,
else if (data > root->data)
root->right = delete_node(root->right, data);
else
{
// node with only one child or no child
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
// node with two children:
struct node* temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->data = temp->data;
// Delete the inorder successor
root->right = delete_node(root->right, temp->data);
}
return root;
}

// Function to do inorder traversal of BST


void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

/* Function to insert a new node with given key in BST */


struct node* insert(struct node* node, int data)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(data);
/* Recur down the tree */
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
}

// Main Function
int main()
{
int n;
root = NULL;
printf("\nEnter the number of nodes : ");
scanf("%d", &n);
int i;
int data;
printf("\nInput the nodes of the binary search tree : ");
if(n > 0)
{
scanf("%d", &data);
root = insert(root, data);
}
for(i = 1; i < n; i++)
{
scanf("%d", &data);
insert(root, data);
}
printf("\nInorder traversal of the BST : ");
inorder(root);
printf("\n");
int del_ele;
printf("\nEnter the node to be deleted : ");
scanf("%d", &del_ele);
delete_node(root, del_ele);
printf("\nInorder traversal after deletion : ");
inorder(root);
printf("\n");
return 0;
}

Output of the Program:


Searching an element
#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
int data;
struct TreeNode *leftChildNode;
struct TreeNode *rightChildNode;
};

typedef struct TreeNode node;


node *rootNode = NULL;

void insertNode(int i, node **n) {


if (*n == NULL) {
(*n) = (node*)malloc(sizeof(node));
(*n)->leftChildNode = NULL;
(*n)->rightChildNode = NULL;
(*n)->data = i;
}
else if ((*n)->data == i)
printf("\nThis value already exists in the tree!");
else if (i > (*n)->data)
insertNode(i, &((*n)->rightChildNode));
else
insertNode(i, &((*n)->leftChildNode));
}
void searchNode(int i, node **n) {
if (*n == NULL)
printf("\nValue does not exist in tree!");
else if((*n)->data == i)
printf("\nValue found!");
else if(i > (*n)->data)
searchNode(i, &((*n)->rightChildNode));
else
searchNode(i, &((*n)->leftChildNode));
}
int main()
{
int ch, num, num1;
do {
printf("\nSelect a choice from the menu below.");
printf("\n1. Insert a node.");
printf("\n2. Search for a node.");
printf("\nChoice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("\nEnter an element: ");
scanf("%d", &num);
insertNode(num, &rootNode);
break;
case 2:
printf("\nEnter the element to be searched for: ");
scanf("%d", &num);
searchNode(num, &rootNode);
break;
default:
exit(0);
}
printf("\nIf you want to return to the menu, press 1.");
printf("\nChoice: ");
scanf("%d", &num);
} while(num == 1);
return 0;
}

Output of the program:


Ques 28: Write a program to implement tree traversal methods.

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return (node);
}
void printPostorder(struct node* node)
{
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
void printInorder(struct node* node)
{
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
void printPreorder(struct node* node)
{
if (node == NULL)
return;
printf("%d ", node->data);
printPreorder(node->left);
printPreorder(node->right);
}
int main()
{
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);

printf("\nPreorder traversal of binary tree is \n");


printPreorder(root);

printf("\nInorder traversal of binary tree is \n");


printInorder(root);

printf("\nPostorder traversal of binary tree is \n");


printPostorder(root);

getchar();
return 0;
}

Output of the program:

You might also like