ARRAY OPERATIONS
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define SIZE 100
void display(int arr[],int n)
int i;
if(n==0)
Printf(“Array is empty!\n”);
return;
printf(“Array:”);
for(i=0;i<n;i++)
printf(“%d”,arr[i]);
printf(“\n”);
int insert(int arr[],int n,int pos,int val)
int i;
if(n==SIZE)
printf(“Array is full!\n”);
return n;
}
for(i=n;i>pos;i--)
arr[i]=arr[i-1];
arr[pos]=val;
return n+1;
int delete(int arr[ ],int n,int pos)
int i;
if(n==0)
printf(“Array is empty!\n”);
return n;
if(pos<0||pos>=n)
printf(“Invalid position!\n”);
return n;
for(i=pos;i<n1;i++)
arr[i]= arr[i+1];
return n-1;
int main()
int arr[SIZE],n=0,choice,pos,val;
while(1)
printf(“\n1.Insert\n2.Delete\n3.Display\n4.Exit\n”);
printf(“Enter your choice:”);
scanf(“%d”,&choice);switch(choice)
case1:
printf(“Enter position(0to%d)and value:”,n);
scanf(“%d %d”, &pos, &val);
n=insert(arr,n,pos,val); break;
case2:
printf(“Enter position(0to%d)to delete:”,n-1)
scanf(“%d”, &pos);
n=delete(arr,n,pos);
break;
case3:
display(arr,n);
break;
case4:
return 0;
default:
printf(“Invalid choice!\n”);
getch();
return 0;
}
OUTPUT:
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:1
Enter position(0to0)and value:01
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:1
Enter position(0to1)and value:12
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:1
Enter position(0to2)and value:23
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:1
Enter position(0to3)and value:34
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:3
Array: 1234
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:2
Enter position(0to3)to delete:1
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:3
Array : 134
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:4
RUNNING TIME COMPLEXITY
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<time.h>
void algorithm(int n)
int i, j;
clrscr();
for(i=0;i<n;i++)
printf(“%d“,i);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf(“%d“,i*j);
int main()
const int n=5;
clock_tstart,end;
double executiontime;
start= clock();
algorithm(n);
end=clock();
execution time=(double)(end-start)/CLOCKS_PER_SEC;
printf(“\nexecutiontime:%f seconds\n”, executiontime);
printf(“time complexity: o(n^2)\n”);
getch();
return 0;
}
OUTPUT:
012340000001234024680369120481216
Executiontime:0.000000seconds
timecomplexity: o(n^2)
INFIX TO POSTFIX USING STACK
PROGRAM:
#include<stdio.h>
#include<conio.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’||x==’/’)
return 2;
return 0;
int main( )
{
char exp[100];
char *e,x;
printf(“Enter the expression:”);
scanf(“%s”,exp);
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);
else
while(priority(stack[top])>=priority(*e))
printf(“%c”,pop());
push(*e);
} e++;
}
while(top!= -1) printf(“%c”,pop());
getch();
return 0;
OUTPUT:
Enter the expression:a+b*c-
d abc*+d-
CIRCULAR QUEUE OPERATIONS
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
void enqueue(int value)
clrscr();
if(rear = = MAX-1)
printf(“Queue is full\n”);
else if(front==-1)
front = 0;
rear=0;
queue[rear]=value;
else
rear=(rear+1)%MAX;
queue[rear]=value;
void enqueue( )
{
if (front == -1)
printf(“Queue is empty\n”);
else
printf(“Dequeued:%d\n”,queue[front]);
front = (front + 1) % Max;
void printQueue( )
if(front==-1)
printf(“Queue is empty\n”);
else
int temp=front;
while(temp!=rear){
printf(“%d”, queue[temp]);
temp=(temp+1) % MAX;
printf(“d\n”,queue[rear]);
}
int main( )
enqueue(1);
enqueue(2);
enqueue(3);
printQueue();
dequeue();
printQueue();
getch();
return 0;
}
OUTPUT:
123
Dequeued:1
23
POLYNOMIAL MANIPULATION USING SINGLE LINKED LIST
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
struct Node
int coeff,exp;
Node* next;
Node*nullptr;
Node(intc,inte):coeff(c),exp(e),next(nullptr){}
};
void insert(Node*&head,intc,inte){
clrscr();
Node* newNode=new Node(c,e);
if(!head || head -> exp<e) {
newNode->next=head;
head=newNodee;
else
Node*temp=head;
while(temp ->next && temp->next-> exp>e)temp=temp->next;
if(temp->exp==e)
temp->coeff+=c;
else
newNode->next=temp>next;
temp->next = newNode;
Node*nullptr
;
Node*next;
Node*addPoly(Node*p1,Node*p2)
Node*result=nullptr;
while(p1 || p2)
if(!p2||(p1&&p1->exp>p2->exp))
insert(result,p1->coeff,p1->exp),p1=p1=next;
else if(!p1 || (p2 && p2 ->exp>p1->exp)){
insert(result,p2->coeff,p2->exp),p2=p2->next;
else
insert(result,p1->coeff+p2->coeff,p1->exp);
p1 = p>next, p2=p2->next;
return result;
void printpoly(Node*head)
{
while(head) {
cout<<head->coeff<<”x”<<head->exp;
if(head->next)cout<<”+”;
head=head->next;
cout<<endl;
int main()
Node*poly1=nullptr,*poly2=nullptr;
insert(poly1,3,2),insert(poly1,5,1),insert(poly1,4,3);
insert(poly2,2,2),insert(poly2,3,1),insert(poly2,3,3);
Node*result=addPoly(poly1,poly2);
cout<<”Resultant Polynomial:”);
printPoly(result);
getch();
return 0;
}
OUTPUT:
Resultant Polynomial:7x3+5x2+8x1
VARIOUS OPERATIONS IN DOUBLE LINKED LIST
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
type def struct Node;
int data;
struct Node*prev,*next;
Node;
Node*createNode(int data)
Node* newNode=(Node*)malloc(sizeof(Node));
clrscr();
newNode->data=data;
newNode->prev=newNode->next=NULL;
return newNode;
void insertFront(Node**head, int data)
Node*newNode createNode(data);
newNode->next=*head; if(*head)
(*head)->prev=newNode;
*head=newNode;
void insertEnd(Node** head, int data)
{
Node*newNode=createNode(data);
Node* temp;
if(!*head)
*head=newNode;
return ;
temp=*head;
while(temp->next) temp= temp->next; temp->next =newNode;
newNode->prev=temp;
void deleteNode(Node**head, int key)
Node* temp= *head;
while(temp&&temp->data!=key)temp=temp->next;
if(! temp) return;
if(temp->prev) temp->prev->next = temp->next;
else *head= temp->next;
if(temp->next)temp->next->prev=temp->prev;
free(temp);
void display(Node*head)
while(head)
printf(“%d”, head->data);
head= head->next;
printf(“\n”);
}
int main( )
Node* head = NULL;
insertFront(&head,30);
insertFront(&head,20);
insertFront(&head,10);
insertEnd(&head,40);
display(head);
deleteNode(&head,20);
display(head);
getch();
return 0;
}
OUTPUT:
10203040
103040
PERFORM BINARY SEARCH
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main( )
int n, i, search,f=0,low,high,mid,a[20];
clrscr();
printf(“Enter the n value:”);
scanf(“%d”, &n);
printf(“Enter the numbers in ascending order:\n”);
for(i=0; i<n; i++)
printf(“a[%d]:“,i);
scanf(“%d”,&a[i]);
printf(“Enter the search element:”);
scanf(“%d”, &search);
low=0;
high=n1;
while(low<=high)
mid=(low+high)/2;
if(search<a[mid])
high=mid-1;
else if(search>a[mid])
{
Low=mid+1;
else
f=1;
printf(“Element found at position:%d\n”,mid+1);
break;
if(f==0)
printf(“Element not present in the array.\n”);
getch( );
}
OUTPUT:
Enter the n value:5
Enter the numbers in ascending order:
A[0]: 2
A[1]:4
A[2]:6
A[3]:9
A[4]:12
Enter the search element : 4
Element found at position:2
COMPARE AMONG BUBBLE SORT,SELECTION SORT AND INSERTIONSORT
PROGRAM:
#include<stdio.h>
#include<conio.h>
Void bubblesort(int arrr[],int n)
int i,j,temp;
clrscr();
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;
void selectionsort(int arr[],int n)
int i, j, temp;
clrscr();
for(i=0;i<n-1;i++)
{
int minIndex = i;
for(j=i+1; j<n; j++)
if(arr[j]<arr[minIndex])
minIndex=j;
temp=arr[minIndex];
arr[minIndex]=arr[i];
void insertionsort(int arr[],int n)
int i, j, key;
clrscr();
for(i=1;i<n;i++)
key=arr[i];
j=i-1;
while(j>=0&&arr[j]>key)
arr[j+1]=arr[j];
j--;
arr[j+1]=key;
}
void printarray(int arr[],int n)
int i;
for(i=0; i<n; i++)
printf(“\n%d\n”,arr[i]);
printf(“\n”);
int main( )
int n,i,choice;
int arr[100];
printf(“Enter the number of elements:”);
scanf(“%d”, &n);
printf(“Enter the elements:\n”);
for(i=0; i<n; i++)
scanf(“%d”,&arr[i]);
printf(“choose sorting algorithm1.bubble2.insertion3.selection”);
scanf(“%d”, &choice);
if(choice ==1)
bubblesort(arr, n);
else if(choice==2)
insertionsort(arr,n);
else if(choice==3)
selectionsort(arr, n);
else
printf(“Invalid choice!\n”);
return 1;
}
printf(“Sorted array:”);
print array(arr, n);
getch( );
return 0;
}
OUTPUT:
Enter the number of elements:
Choose sorting algorithm1.bubble2.insertion3.selection
Sorted array:
1
6
DELETION IN BINARY SEARCH TREE
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
int data;
struct Node*left,*right;
};
struct Node*newNode(int data){
struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
clrscr();
temp->data=data;
temp->left=temp>right=NULL;
return temp;
struct Node*findMin(struct Node*root)
while(root>left)root=root->left;
return root;
struct Node*deleteNode(struct Node*root,int key)
if(!root)
return NULL;
if(key<root->data)
root->left=deleteNode((root->left,key);
else if(key>root->data)
root->right=deleteNode(root->right, key);
else
struct Node*temp;
if(!root->left)
return root->right;
if(!root->right)
return root->left;
temp=findMin(root->right);
root->data=temp->data;
root->right=deleteNode(root->right,temp->data);
return root;
void inorder(struct Node*root)
if(root)
inorder(root->left);
printf(“%d “,root->data);
inorder(root->right);
struct Node*insert(struct Node*root,int data)
if(root)
return newNode(data);
if(data<root->data)
root->left=insert(root->left,data);
else
root->right=insert(root->right,data);
return root;
int main( )
struct Node* root=NULL;
root=insert(root,50);
root=insert(root,30);
root=insert(root,70);
printf(“Before Deletion:”);
inorder(root);
printf(“\n”);
root=deleteNode(root,50);
printf(“After Deletion:”);
inorder(root);
printf(“\n”);
getch();
return 0;
}
OUTPUT:
BeforeDeletion:305070
AfterDeletion:3070
GRAPH TRAVERSALS
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
int graph[MAX][MAX],
visited[MAX],n; int i;
void dfs(int v)
printf(“%d ”,v);
visited[v]=1;
for(i=0;i<n;i++)
if(graph[v][i] &&!visited[i])
dfs(i);
void bfs(int start)
int front=0;rear=0;
int i;
visited[start]=1;
queue[rear++]=start;
while(front<rear)
{
int v==queue[front++];
printf(“%d ”,v);
for(i=0; i<n; i++)
if(graph[v][i]&&!visited[i])
visited[i]=1;
queue[rear++]=i;
}
int main()
{
int edges,v1,v2,start;
int i;
scanf(“%d%d”,&n,&edges);
for(I=0; I<edges;I++)
scanf(“%d %d”, &v1, &v2);
graph[v1][v2]=graph[v2][v1]=1;
scanf((“%d”,&start);
printf(“DFS:”);
dfs(start);
for(i=0;i<n;i++)visited[I]=0;
printf(“\nBFS: “);
bfs(start);
getch();
return 0;
}
OUTPUT:
56
01
02
13
14
23
34
DFS:0132
BFS:01234