Kuldeep D Slab
Kuldeep D Slab
Roll: 17115037
--------Menu-----------
1.Traverse
2.Insert
3.Delete
4.Exit
-----------------------
Enter your choice: 2
--------Menu-----------
1.Traverse
2.Insert
3.Delete
4.Exit
-----------------------
Enter your choice: 2
--------Menu-----------
1.Traverse
2.Insert
3.Delete
4.Exit
-----------------------
Enter your choice: 2
#include<stdio.h>
int a[20],b[20],c[40];
int m,n,p,val,i,j,key,pos,temp;
void traverse();
void insert();
void del();
int main()
{
int choice;
do{
printf("\n\n--------Menu-----------\n");
printf("1.Traverse\n");
printf("2.Insert\n");
printf("3.Delete\n");
printf("4.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
Name: KULDEEP NARAYAN MINJ
Roll: 17115037
--------Menu--------
1.Traverse
2.Insert
3.Delete
4.Exit
-----------------------
Enter your choice: 1
--------Menu-----------
1.Traverse
2.Insert
3.Delete
4.Exit
-----------------------
Enter your choice: 3
--------Menu-----------
1.Traverse
2.Insert
3.Delete
4.Exit
-----------------------
Enter your choice: 1
--------Menu-----------
1.Traverse
2.Insert
3.Delete
4.Exit
-----------------------
Enter your choice: 4
Program aborted!
Process returned 0 (0x0) execution time : 36.984 s
traverse();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
printf("rogram aborted!");
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=4);
return 0;
}
void traverse()
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
}
void insert()
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}
void del()
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
return 0;
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
Enter number of elements
10
Enter 10 integers
2 4 6 8 9 12 14 20 33 36
Enter value to find
6
6 found at location 3.
Theory: Binary Search can only be used for sorted arrays, but it's
fast as compared to linear search. If you wish to use binary search
on an array which isn't sorted, then you must sort it using some
sorting technique say merge sort and then use the binary search
algorithm to find the desired element in the list. If the element to be
searched is found then its position is printed.
Applications:
Swift implementation
Searching for prefixes
Autocompletion
Program Code-
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
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;
return 0;}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
Enter size of array:4
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:1
Enter element:20
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:1
Enter element:3
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:1
Enter element:45
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:1
Enter element:55
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:1
OVERFLOW!
Experiment-8
Aim- Write a program to implement stack and perform push and
pop operation.
Syntax Parsing: Many compilers use a stack for parsing the syntax of
expressions, program blocks etc. before translating into low level
code.
Program Code-
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
#include <stdio.h>
int a[100],top,c,n;
void push()
{
if(top==n)
{
printf("OVERFLOW!\n");
}else
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:3
Stack arrangement is displayed below.
55
45
3
20
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:2
Popped element is 0.
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:2
Popped element is 55.
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:3
Stack arrangement is displayed below.
3
20
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:2
Popped element is 45.
Operation successful.
{
printf("\nEnter element:");
scanf("%d",&a[top]);
top++;
printf("Operation successful.\n");
}
}
void pop()
{
if(top==0)
{
printf("UNDERFLOW!\n");
}
else
{
printf("Popped element is %d.",a[top]);
printf("\nOperation successful.\n");
top--;
}
}
void display()
{
int i=top-1;
printf("Stack arrangement is displayed
below.\n"); for(;i>=0;i--)
{
printf("\t%d\n",a[i]);
}
printf("Operation successful.\n");
}
int main()
{
printf("Enter size of array:");
scanf("%d",&n);
do
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\nEnter operation
detail:"); scanf("%d",&c);
switch(c)
{
case 1: push(); break;
case 2: pop(); break;
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:2
Popped element is 3.
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:2
UNDERFLOW!
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:3
Stack arrangement is displayed below.
Operation successful.
1.Push
2.Pop
3.Display
4.Exit
Enter operation detail:4
123
046
004
Check
1. Upper
2. Lower
3. Tridiagonal
4. Exit
1
Upper triangular!
Check
1. Upper
2. Lower
3. Tridiagonal
4. Exit
2
Not Lower triangular!
Check
1. Upper
2. Lower
3. Tridiagonal
4. Exit
4
Program aborted!
Process returned 0 (0x0) execution time : 19.907 s
Press any key to continue.
Experiment-7
Aim- Write a program to check whether input matrix is
upper triangular, lower triangular or tridiagonal matrix.
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
120
456
038
Check
1. Upper
2. Lower
3. Tridiagonal
4. Exit
3
Tridiagonal!
Check
1. Upper
2. Lower
3. Tridiagonal
4. Exit
2
Not Lower triangular!
Check
1. Upper
2. Lower
3. Tridiagonal
4. Exit
4
Program aborted!
Process returned 0 (0x0) execution time : 21.502 s
Press any key to continue.
}
if(flg==0)
printf("Upper triangular!");
else
printf("Not Upper triangular!");
}
void lower()
{ int c,flg=0,i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ if(i<j)
{
if(arr[i][j]==0)
c+=1;
else
flg+=1;
}
}
}
if(flg==0)
printf("Lower triangular!");
else
printf("Not Lower triangular!");
}
void tridiagonal()
{ inti,j,c=0,flg=0;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
if (i-j==1 || j-i==1 || i-j==0)
c+=1;
else
{
if(arr[i][j]!=0)
flg+=1;
}
}
if(flg==0)
printf("Tridiagonal!");
else
printf("Non Tridiagonal!");
}
int main()
{
int i,j,ch;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
ch=0;
while(ch!=4)
{ printf("\n\nCheck\n1. Upper\n2. Lower\n3. Tridiagonal\n4. Exit\n\n");
scanf("%d",&ch);
if(ch==1)
upper();
else
if(ch==2) lower();
else if(ch==3)
tridiagonal();
else if(ch==4)
printf("Program aborted!");
else
printf("Enter valid choice.\n");
}
return 0;
}
abcd^e-fgh*+^*+i-
Process returned 0 (0x0) execution time : 0.001 s
Press ENTER to continue.
Experiment-10
Aim- Write a program to convert infix expression to
postfix expression.
Theory:
Infix Notation
struct Stack
{
int top;
unsigned capacity;
int* array;
};
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
if (!stack->array)
return NULL;
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
}
char peek(struct Stack* stack)
{
return stack->array[stack->top];
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
}
while (!isEmpty(stack))
exp[++k] = pop(stack );
exp[++k] = '\0';
printf( "%s", exp );
}
int main()
{
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1.Addition
2. Deletion
3. Display
4. Exit
1
23
1.Addition
2. Deletion
3. Display
4. Exit
1
24
1.Addition
2. Deletion
3. Display
4. Exit
1
25
1.Addition
2. Deletion
3. Display
4. Exit
3
23,24,25,
1.Addition
2. Deletion
3. Display
4. Exit
1
26
1.Addition
2. Deletion
3. Display
4. Exit
3
23,24,25,26,
Experiment-13
Aim- Write a program to implement insertion and deletion in a
linear queue.
void addition()
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1.Addition
2. Deletion
3. Display
4. Exit
2
1.Addition
2. Deletion
3. Display
4. Exit
3
24,25,26,
1.Addition
2. Deletion
3. Display
4. Exit
2
1.Addition
2. Deletion
3. Display
4. Exit
3
25,26,
1.Addition
2. Deletion
3. Display
4. Exit
4
void deletion()
{
if(r==f)
{
printf("UNDERFLOW!\n");
}
else
{
r++;
}
}
void display()
{
int d,b;
d=f-1;
b=r;
for(b=r;b<=d;b++)
{
printf("%d,",a[b]);
}
printf("\n");
}
int main()
{
int c;
do{
printf("1.Addition\n2. Deletion\n3. Display\n4. Exit\n");
scanf("%d",&c);
switch(c)
{
case 1: addition();break;
case 2: deletion();break;
case 3: display();break;
case 4: break;
default: printf("Invalid Input!");
}
}while(c!=4);
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1. Insert
2. Delete
3. Display
4. Exit
Enter the choice: 1
1. Insert
2. Delete
3. Display
4. Exit
Enter the choice: 1
1. Insert
2. Delete
3. Display
4. Exit
Enter the choice: 1
1. Insert
2. Delete
3. Display
4. Exit
Enter the choice: 1
1. Insert
2. Delete
3. Display
4. Exit
Enter the choice: 3
void insert()
{
if(count == MAX)
printf("\nQueue is Full");
else
{
rear = (rear + 1) % MAX;
q[rear]=item;
count++;
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1. Insert
2. Delete
3. Display
4. Exit
Enter the choice: 2
{
if(count == 0)
printf("\nQueue is Empty");
else
{
if(front > rear && rear==MAX-1)
{
front=0;
rear=-1;
count=0;
}
else
{
item=q[front];
printf("\nDeleted item is: %c",item);
front = (front + 1) % MAX;
count--;
}
}
}
void display()
{
int i, f=front, r=rear;
if(count == 0)
printf("\nQueue is Empty");
else
{
printf("\nContents of Queue is:\n");
for(i=f; i<=r; i++)
{
printf("%c\t", q[i]);
f = (f + 1) % MAX;
}
}
}
int main()
{
while(1)
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nEnter the character item to be inserted: ");
scanf(" %c",&item);
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
return 0;
break;
}
}
return 0;
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
10
23
44
56
76
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
Element deleted is 76
Experiment-16
Aim- Write a program to implement insertion and deletion in a
double-ended queue.
10
23
44
56
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
Element deleted is 10
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
23
44
56
void print(dequeue *p);
void main()
{
int i,x,op,n;
dequeue q;
initialize(&q);
do
{
printf("\n1.Create\n2.Insert(rear)\n3.Insert(front)\n4.Delete(rear)\n5.Delete(front)"
);
printf("\n6.Print\n7.Exit\n\nEnter your
choice:"); scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter number of elements:");
scanf("%d",&n);
initialize(&q);
printf("\nEnter the data:");
for(i=0;i<n;i++)
{
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
}
break;
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
23
44
56
77
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
if(full(&q))
{
printf("\nQueue is full!!");
exit(0);
}
enqueueF(&q,x);
break;
case 4: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}
x=dequeueR(&q);
printf("\nElement deleted is %d\n",x);
break;
case 5: if(empty(&q))
{
printf("\nQueue is empty!!");
exit(0);
}
x=dequeueF(&q);
printf("\nElement deleted is %d\n",x);
break;
case 6: print(&q);
break;
default: break;
}
}while(op!=7);
}
99
23
44
56
77
1.Create
2.Insert(rear)
3.Insert(front)
4.Delete(rear)
5.Delete(front)
6.Print
7.Exit
return(0);
}
return(0);
}
x=P->data[P->front];
return(x);
}
x=P->data[P->rear];
if(P->rear==P->front)
initialize(P);
else
P->rear=(P->rear-1+MAX)%MAX;
return(x);
}
int i;
i=P->front;
while(i!=P->rear)
{
printf("\n%d",P->data[i]);
i=(i+1)%MAX;
}
printf("\n%d\n",P->data[P->rear]);
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
Key 50 inserted
Key 80 inserted
Key 30 inserted
Key 40 inserted
Key 20 inserted
Key 50 found
#include<stdlib.h>
int key;
}Node;
temp->key = key;
temp->left = NULL;
temp->right = NULL;
return temp;
if(!p){
return nwNode;
insert(p->right, nwNode);
if(!(p->right))
p->right = nwNode;
else{
insert(p->left, nwNode);
if(!(p->left))
p->left = nwNode;
return p;
if(!p){
if(p->key == searchKey){
search(p->right, searchKey);
else
search(p->left, searchKey);
while(p->left != NULL)
return p;
return p;
else{
if(p->left == NULL){
temp = p->right;
free(p);
return temp;
temp = p->left;
free(p);
return temp;
temp = getInSuccessor(p->right);
p->key = temp->key;
return p;
}
void traversePreorder(Node *p){
if(!p)
return ;
traversePreorder(p->left);
traversePreorder(p->right);
if(!p)
return ;
traverseInorder(p->left);
traverseInorder(p->right);
if(!p)
return ;
traversePostorder(p->left);
traversePostorder(p->right);
int main(void){
insert(root, newNode(80));
insert(root, newNode(30));
insert(root, newNode(40));
insert(root, newNode(20));
insert(root, newNode(100));
search(root, 50);
search(root, 10);
if(newRoot){
search(root, 50);
traversePreorder(root);
traverseInorder(root);
traversePostorder(root);
return 0;
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
In this first two elements of the array are checked and swapped if
they are in wrong order. Then first and third elements are
checked and swapped if they are in wrong order. This continues
till the lat element is checked and swapped if necessary.
Insertion Sort - In this, the second element is compared with the first
element and is swapped if it is not in order. Similarly, we take the
third element in the next iteration and place it at the right place in
the subarray of the first and second elements (as the subarray
containing the first and second elements is already sorted). We
repeat this step with the fourth element of the array in the next
iteration and place it at the right position in the subarray containing
the first, second and the third elements. We repeat this process until
our array gets sorted.
Applications:
Bubble Sort: Bubble sort is used in programming TV remote to
sort channels on the basis of longer viewing time..
Insertion sort: Offers low constant factors and is good for sorting
small lists, also appears as base cases to mergesort and quicksort
when the size of a subcase gets small.
int main()
{
int count=0;
int choice=0,ch=0;
int check=0;
int i=0;
printf("Enter the size of the list: ");
scanf("%d",&count);
int list[count];
for(i=0;i<count;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&list[i]);
}
do{
printf("Menu:\n\n");
printf("1.Bubble sort\n2.Selection Sort\n3.Inserton sort\n4.Exit\nYour choice: ");
scanf("%d",&choice);
switch(choice)
void print(dequeue *p);
case 1: //performs bubble sort
bubble(list,count);
break;
case 4: return 0;
}
printf("Do you want to continue(press 1 to continue any other number to exit): ");
scanf("%d",&ch);
} while(ch==1);
return 0;
}
void bubble(int *list,int n)
{
int i,j;
int c;
for(i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (list[j] > list[j+1])
{
c=list[j];
list[j]=list[j+1];
list[j+1]=c;
}
}
}
min=list[j];
k=j;
for(i=j+1;i<n;i++)
{
if(list[i]<min)
{
min=list[i];
k=i;
}
list[k]=list[j];
list[j]=min;
}
printf("Sorted list is:");
for(i=0;i<n;i++)
{
printf("%d,",list[i]);
}
printf("\n");
}
void insertion(int *list,int n)
{
int temp;
int i=0,j=0;
for(i=1;i<n;i++)
{
temp=list[i];
j=i-1;
while((j>=0)&&(list[j]>temp))
{
list[j+1]=list[j];
j--;
}
list[j+1]=temp;
}
printf("Sorted list is: ");
for(i=0;i<n;i++)
{
printf("%d,",list[i]);
}
printf("\n");
}
Name: KULDEEP NARAYAN MINJ
Roll: 17115037
Enter no of elements :6
void main()
scanf("%d", &no);
scanf("%d",
&heap[i]);
for(i=0;i<no;i++)
c = i;
do
root = (c - 1) / 2;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
c = root;
} while (c != 0);
temp = heap[0];
root = 0;
do
c++;
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
root = c;
printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
How many elements?6
Program Code-
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
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);
}
Name: KULDEEP
NARAYAN MINJ
Roll: 17115037
Simple Merge Sort Example - Functions and Array
Your Data : 33 4 11 54 6
Sorted Data : 4 6 11 33 54
Process returned 0 (0x0) execution time : 10.737 s
Press any key to continue.
Experiment
Aim- Write a program to implement merge sort.
#include<conio.h>
#define MAX_SIZE 5
int arr_sort[MAX_SIZE];
int main() {
int i;
scanf("%d", &arr_sort[i]);
printf("\t%d", arr_sort[i]);
getch();
int m;
if (i< j) {
m = (i + j) / 2;
merge_sort(i, m);
merge_sort(m + 1, j);
int t[50];
int i = a, j = c, k = 0;
if (arr_sort[i] <arr_sort[j])
t[k++] = arr_sort[i++];
else
t[k++] = arr_sort[j++];
while (i<= b)
t[k++] = arr_sort[i++];
while (j <= d)
t[k++] = arr_sort[j++];
arr_sort[i] = t[j];