Pydah College of Engineering
Pydah College of Engineering
Department
of
Computer Science & Engineering
SYLLABUS
EXERCISE-01 (Searching)
a) Write a C program that use both recursive and Non-recursive functions to perform linear
search for a key value in a given list.
b)Write a C program that use both recursive and Non-recursive functions to perform binary
search for a key value in a given list .
EXERCISE-02(Sorting-1)
a) Write a C program that implement bubble sort ,to sort a given list of integers in ascending
order.
b) Write a C program that implement quick sort, to sort a given list of integers in ascending
order.
c) Write a C program that implement insertion sort, to sort a given list of integers in ascending
order.
EXERCISE-03(Sorting-2)
a) Write a C program that implement a radix sort, to sort the given list of integers in ascending
order.
b) Write a C program that implement a merge sort, to sort a given list of integers in ascending
order.
b) Write a C program that uses functions to perform insertion operation on a singly linked list.
c) Write a C program that uses functions to perform deletion operation on a singly linked list.
EXERCISE-05(queue)
a) Write a C program that implement queues (its operations) using array.
b) Write a C program that implement queues (its operations) using linked list.
EXERCISE-06(stack)
a) Write a C program that implements stack(its operations)using array.
c) Write a C program that uses stack operations to evaluate the postfix operation.
EXERCISE-07(binary tree)
Write a recursive program for traversing a binary tree in preorder, inorder and postorder.
EXERCISE:01
Aim :- Write a C program that use both recursive and Non-recursive functions to perform a
linear search for a key value for a given list.
SOURCE CODE:
##include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, n, key, low, high, mid;
clrscr();
printf(“Enter the array elements in ascending order”);
for(i = 0; i < n; i++)
{
scanf(“%d”, &a[i]);
}
printf(“Enter the key element\n”);
scanf(“%d”, &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf(“The key element is found at location %d”, mid + 1);
else
printf(“the key element is not found”);
getch();
}
Output:
Enter the size of the array 7
Enter the array elements in ascending order 23 45 68 90 100 789 890
Enter the key element 789
The key Element is found at location 6
PROGRAM:02
Aim:- Write a c program that use both recursive and non recursive binary search for a key value
to a given list.
Source code:
#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element){
while (start_index <= end_index){
int middle = start_index + (end_index- start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1;
}
return -1;
}
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = iterativeBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
return 0;
}
Output:
EXERCISE:02(Sorting-I)
Aim:- Write a c program that implement bubble sort,to sort a given list of integers in
ascending order.
SOURCE CODE:
#include <stdio.h>
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Sorted array: 11 12 22 25 34 64 90
PROGRAM:02
Aim:- Write a c program that implement quick sort,to sort a given list of integers in ascending
order.
SOURCE CODE:
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
}
// Function to print the array
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = { 12, 17, 6, 25, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:-
Sorted array:
1 5 6 12 17 25
PROGRAM-03
Aim:- Write a C program that implement insertion sort to sort the given list of integers in
ascending order.
SOURCE CODE:
#include <stdio.h>
void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one pos
tion ahead from their current position*/
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
int main()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
insert(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Output:
12 31 25 8 32 17
8 12 17 25 31 31
EXERCISE-03(SORTING-2)
PROGRAM-01
Aim:- Write a C program to implement radix sort to sort a given list of integers in ascending
order.
SOURCE CODE:
#include <stdio.h>
int getMax(int a[], int n) {
int max = a[0];
for(int i = 1; i<n; i++) {
if(a[i] > max)
max = a[i];
}
return max; //maximum element from the array
}
void countingSort(int a[], int n, int place) // function to implement counting sort
{
int output[n + 1];
int count[10] = {0};
Output:
Before sorting array elements:
PROGRAM:02
Aim:- Write a C program that implement merge sort to sort a given list of elements in
ascending order.
SOURCE CODE:
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
Output:
Before sorting array elements:
32 12 84 01 63
01 12 32 63 84
EXERCISE:04
PROGRAM:01
Aim:- Write a C program that uses functions to create a singly linked list.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
// Singly Linked List structure
struct Node {
int data;
struct Node* next;
};
int main() {
// Insert elements into the list
insert(10);
insert(20);
insert(30);
insert(40);
return 0;
}
Output:
List: 10 20 30 40
PROGRAM:02
Aim:- Write a C program that uses functions to perform insertion operation on a singly linked
list.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beginsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void beginsert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
Output
Node inserted
Node inserted
PROGRAM:03
Aim:- Write a C program that uses deletion operation on a singly linked list.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
void create(int);
void begdelete();
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{
printf("\n1.Append List\n2.Delete node\n3.Exit\n4.Enter your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the item\n");
scanf("%d",&item);
create(item);
break;
case 2:
begdelete();
break;
case 3:
exit(0);
break;
default:
printf("\nPlease enter valid choice\n");
}
}while(choice != 3);
}
void create(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");
}
}
void begdelete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\n Node deleted from the begining ...");
}
}
Output
1.Append List
2.Delete node
3.Exit
4.Enter your choice?1
Node inserted
1.Append List
2.Delete node
3.Exit
4.Enter your choice?2
PROGRAM:04
Aim:- Write a C program to reverse element of a singly linked list.
SOURCE CODE:
#include <stdio.h>
//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}
if(head == NULL) {
printf("List is empty\n");
return;
}
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);
return 0;
}
Output:
Original List:
1234
Reversed List:
4321
EXERCISE:05
PROGRAM:01
SOURCE CODE:
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
int choice;
while (1)
printf("4.Quit \n");
scanf("%d", &choice);
switch (choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
} /* End of switch */
} /* End of while */
} /* End of main() */
void insert()
. int add_item;
if (rear == MAX - 1)
else
if (front == - 1)
front = 0;
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
} /* End of insert() */
void delete()
return ;
else
front = front + 1;
} /* End of delete() */
void display()
int i;
if (front == - 1)
else
printf("Queue is : \n");
printf("\n");
} /* End of display() */
Output:
1.Insert element to queue
4.Quit
4.Quit
4.Quit
4.Quit
4.Quit
4.Quit
Queue is :
15 20 30
4.Quit
PROGRAM:02
Aim:-Write a C program that implement queue using linked list.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n")
;
printf("\n==========================================================
======\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
Output:
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter value?
123
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter value?
90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
123
90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
90
***********Main Menu**********
==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Exercise:06
Program:01
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Output:
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7
Enter choice : 8
Enter choice : 4
Stack is empty
Enter choice : 5
PROGRAM:02
Aim:-Write a C program that uses stack operation to evaluate using array.
SOURCE CODE:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
top=-1;
scanf("%d",&n);
printf("\n\t--------------------------------");
do
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
while(choice!=4);
return 0;
void push()
if(top>=n-1)
else
scanf("%d",&x);
top++;
stack[top]=x;
void pop()
if(top<=-1)
else
top--;
void display()
if(top>=0)
printf("\n%d",stack[i]);
else
OUTPUT :
1.push
2.pop
3.show
4.end
PROGRAM-03
Aim:- Write a C program that uses stack operations to evaluate postfix expressions.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
// Stack implementation
int stack[MAX_SIZE];
int top = -1;
void push(int item) {
if (top >= MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
top++;
stack[top] = item;
}
int pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1;
}
int item = stack[top];
top--;
return item;
}
int is_operator(char symbol) {
if (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/') {
return 1;
}
return 0;
}
int evaluate(char* expression) {
int i = 0;
char symbol = expression[i];
int operand1, operand2, result;
while (symbol != '\0') {
if (symbol >= '0' && symbol <= '9') {
int num = symbol - '0';
push(num);
}
else if (is_operator(symbol)) {
operand2 = pop();
operand1 = pop();
switch(symbol) {
case '+': result = operand1 + operand2; break;
case '-': result = operand1 - operand2; break;
case '*': result = operand1 * operand2; break;
case '/': result = operand1 / operand2; break;
}
push(result);
}
i++;
symbol = expression[i];
}
result = pop();
return result;
}
int main() {
char expression[] = "5 6 7 + * 8 -";
int result = evaluate(expression);
printf("Result= %d\n", result);
return 0;
}
OUTPUT:
Result: 57
EXERCISE-07
PROGRAM:01
Aim:- Write a c program for traversing a binary tree in preorder, post order, in order.
PREFIX:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
/*To create a new node*/
struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
/*function to traverse the nodes of binary tree in preorder*/
int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);
Output:
The preorder is :
9 7 5 8 14 11 16
POST ORDER:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
s
struct node* left;
struct node* right;
};
return;
traversePostorder(root->left);
traversePostorder(root->right);
printf(" %d ", root->element);
}
int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);
Output:
15264
52164
INORDER:
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left;
struct node *right;
};
//return a new node with the given
struct node *getNode(int val)
{
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->key = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
//inserts nodes in the binary search tree
struct node *insertNode(struct node *root, int val)
{
if(root == NULL)
return getNode(val);
return root;
}
/* Do while loop to display various options to select from to decide the input */
do
{
printf("\nSelect one of the operations::");
printf("\n1. To insert a new node in the Binary Tree");
printf("\n2. To display the nodes of the Binary Tree(via Inorder Traversal).\n");
int choice;
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf("\nEnter the value to be inserted\n");
scanf("%d",&data);
root = insertNode(root,data);
break;
case 2 :
printf("\nInorder Traversal of the Binary Tree::\n");
inorder(root);
break;
default :
printf("Wrong Entry\n");
break;
}
return 0;
}
Output:
425136
EXERCISE:08
PROGRAM:01
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *right_child;
struct node *left_child;
};
struct node* new_node(int x){
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data = x;
temp->left_child = NULL;
temp->right_child = NULL;
return temp;
}
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
root = delete(root, 1);
root = delete(root, 40);
root = delete(root, 45);
root = delete(root, 9);
inorder(root);
printf("\n");
return 0;
}
Output :-
1 5 7 9 12 15 20 25 30 40 42 45
5 7 12 15 20 25 30 42
PROGRAM:02
Aim:-Write a C program to insert a node into a BST.
SOURCE CODE:
#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");
}
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
Enter the item which you want to insert?
12
Node Inserted
23
Node Inserted
PROGRAM:03
Aim:-Write a C program to delete a node from a BST.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node {
int key;
};
temp->key = item;
return temp;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
if (node == NULL)
return newNode(key);
else
return node;
/* Given a non-empty binary search tree, return the node with minimum
key value found in that tree. Note that the entire tree does not
need to be searched. */
current = current->left;
return current;
/* Given a binary search tree and a key, this function deletes the key
// base case
if (root == NULL)
return root;
// to be deleted
else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
int main() {
50
/ \
30 70
/ \ / \
20 40 60 80 */
inorder(root);
printf("\nDelete 20\n");
inorder(root);
printf("\nDelete 30\n");
inorder(root);
printf("\nDelete 50\n");
inorder(root);
return 0;}
Output:
$ gcc DeleteBST.c
$ ./a.out
20 30 40 50 60 70 80
Delete 20
30 40 50 60 70 80
Delete 30
40 50 60 70 80
Delete 50
40 60 70 80