Professional Documents
Culture Documents
UNIVERSITY
AKIDO COLLEGE OF
ENGINEERING PRACTICAL FILE
B Tech CSE
DATA STRUCTURE AND ALGORITHM
SUBMITTED BY:
AARYAVART
VIRAT
BHUSHAN
REGISTRATION NUMBER : 2113071058
2113071230
INDEX:
S.no. Topic Date Teacher’s
Sign
01 Write a menu driven program that 30-09-22
implements following operations (using
separate functions) on a linear array:
Insert a new element at end as well as
at a given position
Delete an element from a given
whose value is given or whose position
is given
To find the location of a given
element
To display the elements of the linear
array
02 Write a menu driven program that 07-10-22
maintains a linear linked list whose
elements are stored in on ascending
order and implements the following
operations (using separate functions):
Insert a new element
Delete an existing element
Search an element
Display all the element
03 Write a program to demonstrate the 14-10-22
use of stack (implemented using linear
array) in converting arithmetic
expression from infix notation to postfix
notation.
04 Program to demonstrate the use of 22-10-22
stack (implemented using linear linked
lists) in evaluating arithmetic expression
in postfix notation
05 Program to demonstration the 28-10-22
implementation of various operations
on a linear queue represented using a
linear array
Write a menu driven program that implements following operations (using separate functions) on a
linear array:
Delete an element from a given whose value is given or whose position is given
#include<stdio.h>
#include<stdlib.h>
int n = 0;
void create();
void display();
void insert();
void del();
int main()
int choice;
while(1)
printf("\n\n~~~~MENU~~~~");
Exit");
scanf("%d", &choice);
switch(choice)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert();
break ;
case 4: del();
break;
case 6: exit(1);
break;
}}
void create(){
int i;
scanf("%d", &n);
scanf("%d", &a[i]);
void display()
{ int i;
if(n == 0)
return;
for(i=0; i<n;i++)
if(n == 5)
return;
do{
scanf("%d", &pos);
scanf("%d", &elem);
a[i+1] = a[i];
a[pos] = elem;
n = n+1;
display();
void del()
Int I;
if(n == 0)
return;
scanf("%d", &pos);}
while(pos>=n);
elem = a[pos];
{ a[i] = a[i+1];
n = n-1;
display();
}
OUTPUT
COMPLEXITY
1- >Best case = if we get the element at the index 0 then is time complexity will be constant which is
order of 1 [O(1)].
2- >Worst case = if the element is in the last position then its time complexity will be order of n[O(n)],
Because in the linear search, the searching starts from the index 0 to n.
If we did not get the element in the array but the searching reached the end position still its time
complexity will be O(n).
3- >Average case = In the average case, we consider a mid value and the we will do (1+n)/2, if the
value of n reached infinity, then infinity/2 will be infinity only because we ignore the small value, but
still, there will be order on n[O(n)] in the average case.
PROGRAM 2
Write a menu driven program that maintains a linear linked list whose elements are stored in on
ascending order and implements the following operations (using separate functions):
Search an element
/*
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
/*
* Node Declaration
*/
struct node
int info;
}*start;
/*
* Class Declaration
*/
class single_llist
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void search();
void update();
void display();
single_llist()
start = NULL;
};
/*
*/
main()
single_llist sl;
start = NULL;
while (1)
cout<<endl<<" "<<endl;
cout<<"7.Search Element"<<endl;
cout<<"10.Exit "<<endl;
cin>>choice;
switch(choice)
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
sl.insert_last();
cout<<endl;
break;
case 3:
sl.insert_pos();
cout<<endl;
break;
case 4:
sl.sort();
cout<<endl;
break;
case 5:
sl.delete_pos();
break;
case 6:
sl.update();
cout<<endl;
break;
case 7:
sl.search();
cout<<endl;
break;
case 8:
sl.display();
cout<<endl;
break;
case 9:
sl.reverse();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
/*
* Creating Node
*/
if (temp == NULL)
return 0;
else
temp->info = value;
temp->next = NULL;
return temp;
}
/*
*/
void single_llist::insert_begin()
int value;
cin>>value;
temp = create_node(value);
if (start == NULL)
start = temp;
start->next = NULL;
else
p = start;
start = temp;
start->next = p;
/*
*/
void single_llist::insert_last()
int value;
cin>>value;
temp = create_node(value);
s = start;
while (s->next != NULL)
s = s->next;
temp->next = NULL;
s->next = temp;
/*
*/
void single_llist::insert_pos()
cin>>value;
temp = create_node(value);
cin>>pos;
int i;
s = start;
while (s != NULL)
s = s->next;
counter++;
if (pos == 1)
if (start == NULL)
start = temp;
start->next = NULL;
else
{
ptr = start;
start = temp;
start->next = ptr;
s = start;
ptr = s;
s = s->next;
ptr->next = temp;
temp->next = s;
else
/*
*/
void single_llist::delete_pos()
if (start == NULL)
cout<<"List is empty"<<endl;
return;
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
start = s->next;
else
while (s != NULL)
s = s->next;
counter++;
s = start;
ptr = s;
s = s->next;
ptr->next = s->next;
else
free(s);
cout<<"Element Deleted"<<endl;
/*
* Searching an element
*/
void single_llist::search()
{
int value, pos = 0;
if (start == NULL)
cout<<"List is empty"<<endl;
return;
cin>>value;
s = start;
while (s != NULL)
pos++;
if (s->info == value)
flag = true;
s = s->next;
if (!flag)
/*
*/
void single_llist::display()
if (start == NULL)
return;
temp = start;
cout<<"Elements of list are: "<<endl;
cout<<temp->info<<"->";
temp = temp->next;
cout<<"NULL"<<endl;
}
OUTPUT
COMPLEXITY
In a singly linked list, the time complexity for inserting and deleting an
element from the list is O(n).
PROGRAM 3
Write a program to demonstrate the use of stack (implemented using linear array) in
converting arithmetic expression from infix notation to postfix notation.
#include<stdio.h>
#include<ctype.h>
char stack[100];
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 exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
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
COMPLEXITY
// Stack type
struct Stack
{
int top;
unsigned capacity;
int* array;
};
// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
stack->top = -1;
stack->capacity = capacity;
stack->array = (int*) malloc(stack->capacity * sizeof(int));
return stack;
}
#include<stdio.h>
#define n 5
int main()
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
while(ch)
scanf("%d",&ch);
switch(ch)
case 1: if(rear==x)
else
scanf("%d",&queue[rear++]);
break;
case 2: if(front==rear)
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
break;
if(front==rear)
else
printf("%d",queue[i]);
printf("\n");
break;
default:
return 0;
}
OUTPUT
COMPLEXITY
#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
COMPLEXITY
Time complexity of enQueue(), deQueue() operation is O(1) as there is no
loop in any of the operation.
PROGRAM 7
#include
<stdlib.h>
struct node
int info;
}*front,*rear,*temp,*front1;
int frontelement();
void deq();
void empty();
void display();
void create();
void
queuesize();
int count = 0;
int main()
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue
size"); create();
while (1)
switch (ch)
case 1:
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
e); else
break;
case 4:
empty();
break;
case 5:
exit(0)
; case 6:
display()
; break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
void create()
void queuesize()
if (rear == NULL)
rear->ptr = NULL;
rear->info = data;
front = rear;
else
rear->ptr = temp;
temp->info =
data; temp->ptr =
NULL;
rear = temp;
count++;
void display()
front1 = front;
printf("Queue is empty");
return;
>ptr;
if (front1 == rear)
printf("%d", front1-
>info);
void deq()
front1 = front;
if (front1 == NULL)
queue"); return;
else
if (front1->ptr != NULL)
front1 = front1->ptr;
>info); free(front);
front = front1;
else
>info); free(front);
front =
NULL; rear
= NULL;
count--;
int frontelement()
NULL)) return(front->info);
else
return 0;
void empty()
else
#include <stdlib.h>
struct node {
int key;
};
// Create a node
temp->key = item;
temp->left = temp->right =
// Inorder Traversal
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
>key);
// Traverse right
inorder(root->right);
// Insert a node
struct node *insert(struct node *node, int key) {
newNode(key);
node->left = insert(node->left,
key); else
return node;
return current;
// Deleting a node
root;
free(root);
return temp;
>left; free(root);
return temp;
root->key = temp->key;
return root;
// Driver code
int main() {
NULL; root =
insert(root, 4);
printf("Inorder traversal: ");
inorder(root);
inorder(root);
}
OUTPUT
COMPLEXITY
The worst-case time complexity of search and insert operations is O(h)
where h is the height of the Binary Search Tree. In the worst case, we may
have to travel from root to the deepest leaf node. The height of a skewed
tree may become n and the time complexity of search and insert operation
may become O(n).
PROGRAM 9
Program to illustrate the traversal of graph using breadth-first search
#include<stdio.h>
void bfs(int v) {
if(a[v][i] && !
visited[i]) q[++r] = i;
if(f <= r) {
visited[q[f]] =
1;
bfs(q[f++]);
int main()
{ int v;
scanf("%d", &n);
for(i=1; i <= n; i+
+) { q[i] = 0;
visited[i] = 0;
for(j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
scanf("%d", &v);
bfs(v);
for(i=1; i <= n; i+
+) { if(visited[i])
printf("%d\t", i);
else {
reachable"); break;
}
OUTPUT
COMPLEXITY
void DFS(int);
int main()
int i,j;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
void DFS(int i)
int j; printf("\n
%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
}
OUTPUT
COMPLEXITY
O(V + E), where V is the number of vertices and E is the number of edges in
the graph.
PROGRAM 11
Program to sort an array of integers in ascending order using bubble sort
#include <stdio.h>
#define MAXSIZE 10
int main()
int array[MAXSIZE];
scanf("%d", &num);
scanf("%d", &array[i]);
printf("%d\n", array[i]);
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
printf("%d\n", array[i]);
}
OUTPUT
COMPLEXITY
#include <stdio.h>
*a = *b;
*b = temp;
min_idx = i;
swap(&array[min_idx], &array[step]);
t size) {
}
printf("\n");
// driver code
int main() {
selectionSort(data, size);
printArray(data, size);
}
OUTPUT
COMPLEXITY
Time Complexity: The time complexity of Selection Sort is O(N 2) as there
are two nested loops:
One loop to select an element of Array one by one = O(N)
Another loop to compare that element with every other Array element =
O(N)
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N 2)
PROGRAM 13
Program to sort an array of integers in ascending order using insertion sort.
#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()
{
int a[] = { 12, 31, 25, 8, 32, 17 };
printArr(a, n);
insert(a, n);
printArr(a, n);
return 0;
}
OUTPUT
COMPLEXITY
#include <stdio.h>
// Using counting sort to sort the elements in the basis of significant places
void countingSort(int array[], int size, int place) {
int output[size + 1];
int max = (array[0] / place) % 10;
// Driver code
int main() {
int array[] = {121, 432, 564, 23, 1, 45, 788};
int n = sizeof(array) / sizeof(array[0]);
radixsort(array, n);
printArray(array, n);
}
OUTPUT
COMPLEXITY
Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time
where b is the base for representing numbers, for example, for the decimal
system, b is 10. What is the value of d? If k is the maximum possible value,
then d would be O(logb(k)). So overall time complexity is O((n+b) * logb(k)).
Which looks more than the time complexity of comparison-based sorting
algorithms for a large k. Let us first limit k. Let k <= n c where c is a constant.
In that case, the complexity becomes O(nLogb(n)). But it still doesn’t beat
comparison-based sorting algorithms.
What if we make the value of b larger? What should be the value of b to
make the time complexity linear? If we set b as n, we get the time complexity
as O(n). In other words, we can sort an array of integers with a range from 1
to nc if the numbers are represented in base n (or every digit takes log2(n)
bits).
PROGRAM 15
Program to sort an array of integers in ascending order using merge sort.
#include <stdio.h>
#include <stdlib.h>
r)
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
subarray
arr[k] = L[i];
i++;
else {
arr[k] = R[j];
j++;
} k+
+;
are any */
arr[k] = L[i];
i++;
k++;
are any */
arr[k] = R[j];
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
COMPLEXITY
Time Complexity: O(N log(N)), Sorting arrays on different machines. Merge
Sort is a recursive algorithm and time complexity can be expressed as
following recurrence relation.
T(n) = 2T(n/2) + θ(n)
The above recurrence can be solved either using the Recurrence Tree
method or the Master method. It falls in case II of the Master Method and the
solution of the recurrence is θ(Nlog(N)). The time complexity of Merge Sort is
θ(Nlog(N) ) in all 3 cases (worst, average, and best) as merge sort always
divides the array into two halves and takes linear time to merge two halves.
PROGRAM 16
Program to sort an array of integers in ascending order using quick sort.
// Quick sort in C
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
i++;
swap(&array[i], &array[j]);
}
}
return (i + 1);
quickSort(array, pi + 1, high);
printf("\n");
// main function
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
printf("Unsorted Array\n");
printArray(data, n);
quickSort(data, 0, n - 1);
printArray(data, n);
}
OUTPUT
COMPLEXITY
Time taken by QuickSort, in general, can be written as follows.
T(n) = T(k) + T(n-k-1) + (n)
The first two terms are for two recursive calls, the last term is for the partition
process. k is the number of elements that are smaller than the pivot.
The time taken by QuickSort depends upon the input array and partition
strategy.
PROGRAM 17
Program to sort an array of integers in ascending order using heap sort.
#include <stdio.h>
index of root node in array a[], and 'n' is the size of heap. */
largest = left;
largest = right;
if (largest != i) {
a[i] = a[largest];
a[largest] =
temp;
heapify(a, n, largest);
heapify(a, n, i);
a[0] = a[i];
a[i] =
temp;
heapify(a, i, 0);
printf("%d", arr[i]);
printf(" ");
int main()
printArr(a, n);
heapSort(a, n);
printArr(a, n);
return 0;
}
OUTPUT
COMPLEXITY
/* store a[i] to the variable temp and make the ith position empty */
int j;
a[j] = temp;
return 0;
int i;
int main()
{
printArr(a, n);
shell(a, n);
printArr(a, n);
return 0;
}
OUTPUT
COMPLEXITY
Time complexity of the above implementation of Shell sort is O(n2). In the
above implementation, the gap is reduced by half in every iteration. There
are many other ways to reduce gaps which leads to better time complexity.
PROGRAM 19
Program to demonstrate the use of linear search to search a given element in an array.
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:\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("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
OUTPUT
COMPLEXITY
#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 isn't present in the list.\n", search);
return 0;
}
OUTPUT
COMPLEXITY