DS File Sagar
DS File Sagar
CSIT- 124
Practical File
#include <stdio.h>
void main()
int num;
scanf("%d", &num);
int array[num];
scanf("%d", &array[i]);
scanf("%d", &keynum);
if (keynum == array[i] )
found = 1;
break;
if (found == 1)
else
Output:
Code:
#include<stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}
Output:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
return 0;
}
Output:
#include <stdio.h>
int main() {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
position = i;
position = j;
if (position != i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
printf("%d\t", arr[i]);
return 0;
Output:
5. Write a program in c to sort an array implementing insertion sort
Code:
#include <stdio.h>
void insert(int a[], int n) /* function to sort an aay with insertion sort */
int i, j, temp;
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp to one position ahead
from their current position*/
a[j+1] = a[j];
j = j-1;
a[j+1] = temp;
int i;
}
int main()
printArr(a, n);
insert(a, n);
printArr(a, n);
return 0;
Output:
Code:
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
int a[50],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
int j;
if(l<u)
j=partition(a,l,u);
quick_sort(a,l,j-1);
quick_sort(a,j+1,u);
}
int partition(int a[],int l,int u)
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
temp=a[i];
a[i]=a[j];
a[j]=temp;
while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
Output:
7. Write a program to sort the given array implementing merge sort
Code:
#include <stdio.h>
#include <stdlib.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
arr[k] = L[i];
i++;
else {
arr[k] = R[j];
j++;
k++;
are any */
arr[k] = L[i];
i++;
k++;
are any */
j++;
k++;
if (l < r) {
// large l and h
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
/* UTILITY FUNCTIONS */
{
int i;
printf("\n");
/* Driver code */
int main()
printArray(arr, arr_size);
printArray(arr, arr_size);
return 0;
Output:
8. Write a program to insert a new element in the given unsorted array at
kth position .
Code:
#include<stdio.h>
int main(){
int student[40],pos,i,size,value;
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%d",&student[i]);
scanf("%d",&pos);
scanf("%d",&value);
for(i=size-1;i>=pos-1;i--)
student[i+1]=student[i];
student[pos-1]= value;
for(i=0;i<=size;i++)
printf("%d\n",student[i]);
return 0;
Output
9. Write a program to delete an element from given sorted array
Code:
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Resultant array:\n");
return 0;
}
Output:
#include <stdio.h>
Int main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
scanf("%d", &m);
scanf("%d", &array1[i]);
scanf("%d", &n);
scanf("%d", &array2[i]);
i = 0;
j = 0;
{
array3[k] = array1[i];
i++;
else
array3[k] = array2[j];
j++;
k++;
if (i >= m)
while (j < n)
array3[k] = array2[j];
j++;
k++;
if (j >= n)
while (i < m)
array3[k] = array1[i];
i++;
k++;
printf("\n%d", array3[i]);
return 0 ;
Output:
11. Write a program to implement stack using array . also show overflow
and underflow in respective to push and pop operation
Code:
#include<stdio.h>
int stack[20],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:
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");
}
Output:
13. Write a program to implement circular queue using array which showa
insertion and deletion operations.
Code:
#include <stdio.h>
# define max 6
int front=-1;
int rear=-1;
front=0;
rear=0;
queue[rear]=element;
printf("Queue is overflow..");
else
int dequeue()
printf("\nQueue is underflow..");
else if(front==rear)
{
front=-1;
rear=-1;
else
front=(front+1)%max;
void display()
int i=front;
else
while(i<=rear)
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
int main()
scanf("%d", &choice);
switch(choice)
case 1:
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
Output:
14. Write a program to implement linear linked list showing all operations
like creation, insertion, deletion , and searching
Code:
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
};
void createList()
if (start == NULL) {
int n;
scanf("%d", &n);
if (n != 0) {
int data;
start = newnode;
temp = start;
scanf("%d", &data);
start->info = data;
temp->link = newnode;
printf("\nEnter number to"
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
else
void traverse()
// List is empty
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
void insertAtFront()
int data;
scanf("%d", &data);
temp->info = data;
// assigned to start
temp->link = start;
start = temp;
void insertAtEnd()
{
int data;
scanf("%d", &data);
// Changes links
temp->link = 0;
temp->info = data;
head = start;
head = head->link;
head->link = temp;
void insertAtPosition()
// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
temp = temp->link;
i++;
newnode->link = temp->link;
temp->link = newnode;
void deleteFirst()
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->link;
free(temp);
}
void deleteEnd()
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0) {
prevnode = temp;
temp = temp->link;
free(temp);
prevnode->link = 0;
void deletePosition()
int i = 1, pos;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
// Position to be deleted
scanf("%d", &pos);
temp = start;
temp = temp->link;
i++;
// Change Links
position = temp->link;
temp->link = position->link;
// Free memory
free(position);
}
}
void reverseLL()
t1 = t2 = NULL;
// If LL is empty
if (start == NULL)
printf("List is empty\n");
// Else
else {
// Traverse the LL
// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
start = t1;
temp = start;
printf("Reversed linked "
"list is : ");
// Print the LL
temp = temp->link;
void search()
int item,i=0,flag;
ptr = start;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
while (ptr!=NULL)
if(ptr->info == item)
{
printf("item found at location %d ",i+1);
flag=0;
else
flag=1;
i++;
if(flag==1)
void display()
ptr = start;
if(ptr == NULL)
printf("Nothing to print");
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
printf("\n%d",ptr->info);
// Driver Code
int main()
int choice;
while (1) {
" starting\n");
" end\n");
"any position\n");
"first element\n");
"last element\n");
printf("\t7 For deletion of "
"linked list\n");
printf("\t11 To exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
reverseLL();
break;
case 9:
search();
break;
case 10:
display();
break;
case 11:
exit(1);
break;
default:
printf("Incorrect Choice\n");
return 0;
Output:
15. Write a program to implement stack using linked list. Implement push
,pop and display operations.
Code:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
};
void main ()
int choice=0;
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
}
case 3:
display();
break;
case 4:
printf("Exiting....");
break;
default:
};
void push ()
int val;
if(ptr == NULL)
else
{
scanf("%d",&val);
if(head==NULL)
ptr->val = val;
head=ptr;
else
ptr->val = val;
ptr->next = head;
head=ptr;
printf("Item pushed");
void pop()
int item;
if (head == NULL)
{
printf("Underflow");
else
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
void display()
int i;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
else
while(ptr!=NULL)
printf("%d\n",ptr->val);
ptr = ptr->next;
Output:
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
void insert();
void delete();
void display();
void main ()
int choice;
while(choice != 4)
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
scanf("%d",& choice);
switch(choice)
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
void insert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
return;
else
printf("\nEnter value?\n");
scanf("%d",&item);
if(front == NULL)
front = ptr;
rear = ptr;
else
rear = ptr;
rear->next = NULL;
void delete ()
if(front == NULL)
printf("\nUNDERFLOW\n");
return;
else
ptr = front;
free(ptr);
}
void display()
ptr = front;
if(front == NULL)
printf("\nEmpty queue\n");
else
while(ptr != NULL)
Output:
17. Write a program to count the number of times an element is present in
a linked list
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
int info;
};
int n;
start=create_list(start);
display(start);
scanf("%d",&n);
return 0;
}/*End of main()*/
int k=0;
while(ptr!=NULL)
if(ptr->info == n)
k++;
ptr=ptr->link;
return k;
}/*End of countOccurrences()*/
struct node *create_list(struct node *start)
int i,n,data;
scanf("%d",&n);
start=NULL;
if(n==0)
return start;
scanf("%d",&data);
start=addatbeg(start,data);
for(i=2;i<=n;i++)
scanf("%d",&data);
start=addatend(start,data);
return start;
}/*End of create_list()*/
printf("\nList is empty\n");
return;
p=start;
printf("\nList is :\n");
while(p!=NULL)
printf("%d ",p->info);
p=p->link;
printf("\n\n");
}/*End of display() */
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}/*End of addatbeg()*/
{
struct node *p,*tmp;
tmp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp;
tmp->link=NULL;
return start;
Output:
18. Write a program to increment the data part of every node present in a
linked list by 10. Display the data both before incrimination and after.
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
};
void createList()
if (start == NULL) {
int n;
scanf("%d", &n);
if (n != 0) {
int data;
start = newnode;
temp = start;
scanf("%d", &data);
start->info = data;
temp->link = newnode;
printf("\nEnter number to"
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
else
void traverse()
// List is empty
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
void insertAtFront()
int data;
scanf("%d", &data);
temp->info = data;
// assigned to start
temp->link = start;
start = temp;
void increment()
ptr = start;
if(ptr == NULL)
printf("Nothing to print");
else
while (ptr!=NULL)
printf("\n%d",ptr->info );
int main()
int choice;
while (1) {
" starting\n");
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
increment();
break;
default:
printf("Incorrect Choice\n");
return 0;
}Output:
19. Write a program to implement Doubly Linked List, showing all the
operations, like creation, display, insertion, deletion and searching.
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
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.Insert at any random location\
n4.Delete from Beginning\n
5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\
n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}
Output:
Menu:
Insert in starting: Insert at last:
20. Write a program to create a Binary Search Tree and display its
contents using recursive preorder, postorder and inorder traversal
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
if (node == NULL)
return;
printInorder(node->left);
printInorder(node->right);
if (node == NULL)
return;
printPreorder(node->left);
printPreorder(node->right);
}
/* Driver program to test above functions*/
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printPreorder(root);
printInorder(root);
printPostorder(root);
getchar();
return 0;
Output:
21. Write a program to implement deletion of a node in binary search tree.
• no specific data or no child
• only left child
• only right child
• node has two children
// C program to demonstrate
// delete operation in binary
// search tree
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
/* A utility function to
insert a new node with given key in
* BST */
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);
return current;
}
// Driver Code
int main()
{
/* Let us create following BST
50
/ \
30 70
/ \ / \
20 40 60 80 */
struct node* root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
printf("\nDelete 20\n");
root = deleteNode(root, 20);
printf("Inorder traversal of the modified tree \n");
inorder(root);
printf("\nDelete 30\n");
root = deleteNode(root, 30);
printf("Inorder traversal of the modified tree \n");
inorder(root);
printf("\nDelete 50\n");
root = deleteNode(root, 50);
printf("Inorder traversal of the modified tree \n");
inorder(root);
return 0;
}
Output:
// operation in binary
// search tree.
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
};
temp->key = item;
return temp;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
* BST */
if (node == NULL)
return newNode(key);
return node;
// Driver Code
int main()
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
Output:
23. Write a program to implement Binary tree and display the contents
using non-recursive preorder, postorder and inorder traversal techniques.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int value;
// root node
root->value = data;
return root;
node->value = data;
return node;
if (!temp)
return;
free_tree(temp->left);
free_tree(temp->right);
free(temp);
return;
if (!root)
return;
switch(traversal) {
case (PREORDER):
// Do a Preorder Traversal
print_tree(traversal, root->left);
print_tree(traversal, root->right);
break;
case (INORDER):
// Do an Inorder Traversal
print_tree(traversal, root->left);
print_tree(traversal, root->right);
break;
case (POSTORDER):
// Do a postorder Traversal
print_tree(traversal, root->left);
print_tree(traversal, root->right);
break;
int main() {
root->left = create_node(20);
root->right = create_node(30);
root->left->left = create_node(40);
root->left->right = create_node(50);
root->right->left = create_node(60);
root->right->right = create_node(70);
printf("----Preorder Traversal:----\n");
print_tree(PREORDER, root);
printf("\n\n");
printf("----Inorder Traversal:----\n");
print_tree(INORDER, root);
printf("\n\n");
printf("----Postorder Traversal:----\n");
print_tree(POSTORDER, root);
printf("\n\n");
free_tree(root);
return 0;
Output:
24. Write a program to sort elements in an array using heap sort
#include <stdio.h>
/* function to heapify a subtree. Here 'i' is the
index of root node in array a[], and 'n' is the size of heap. */
if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, n, largest);
}
}
heapify(a, i, 0);
}
}
int main()
{
int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}
Output: