Professional Documents
Culture Documents
int main()
int a[20],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printArr(a, n);
selection(a, n);
printArr(a, n);
return 0;
{
int i, j, small,temp;
small = i;
small = j;
temp = a[small];
a[small] = a[i];
a[i] = temp;
int i;
Output
Enter how many elements you want:5
20 50 10 40 30
10 20 30 40 50
int i, j, tmp;
tmp = a[i];
j = i - 1;
a[j+1] = a[j];
j = j-1;
a[j+1] = tmp;
int i;
for (i = 0; i < n; i++)
void main()
{ int a[20],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printArr(a, n);
insert(a, n);
printArr(a, n);
Output
Enter how many elements you want:6
342516
123456
3) Program to sort the given list using bubble sort technique
#include<stdio.h>
int main()
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
printf(" %d",a[i]);
for(i=n-2;i>=0;i--)
for(j=0;j<=i;j++)
if(a[j]>a[j+1])
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
Output
How many numbers you want to enter?: 5
Enter 5 numbers: 8 6 9 7 10
8 6 9 7 10
6 7 8 9 10
=============================================================================
int main()
int a[10],n,ele,i,pos;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the search element: ");
scanf("%d",&ele);
pos=-1;
if(ele == a[i])
pos = i;
break;
for(i=0;i<n;i++)
printf(" %d",a[i]);
if(pos>= 0)
printf("\n And The element %d present in the position '%d' in the given array.\n ",
ele,pos+1);
else
return 0;
Output
How many numbers you want to enter?: 5
Enter 5 numbers: 25 3 6 4 65
ôEnter the search element: 65
And The element 65 present in the position '5' in the given array.
=============================================================================
5) Program to search an element using binary search technique.
#include<stdio.h>
int main()
int a[10],n,ele,i,pos,b,e,m;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&ele);
pos=-1;
b=0;
e=n-1;
while(b<=e)
m=(b+e)/2;
if(ele==a[m])
{
pos=m;
break;
else
if(ele<a[m])
e=m-1;
else
b=m+1;
if(pos>=0)
else
for(i=0;i<n;i++)
printf(" %d",a[i]);
return 0;
Output
How many numbers you want to enter?: 6
The Position='5'
=============================================================================
int stack[10],i,j,ch=0,n,top=-1;
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
scanf("%d",&val);
stack[top] = val;
void pop ()
if(top == -1)
printf("Underflow");
else
void show()
for (i=top;i>=0;i--)
printf("%d ",stack[i]);
if(top == -1)
printf("Stack is empty");
void main ()
scanf("%d",&n);
printf("\n----------------------------------------------");
while(ch!= 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
switch(ch)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
printf("Exiting....");
break;
default:
{
Output
How many elements you want to enter in stack: 5
----------------------------------------------
1.Push
2.Pop
3.Show
4.Exit
1.Push
2.Pop
3.Show
4.Exit
Enter your choice :
1.Push
2.Pop
3.Show
4.Exit
=============================================================================
#include<stdlib.h>
#define max 5
void insert();
void removing();
void display();
int q[max];
void main ()
{
int ch;
while(ch!= 4)
printf("\n******************Queue Operations*********************\n");
scanf("%d",&ch);
switch(ch)
case 1: insert();
break;
case 2: removing();
break;
case 3: display();
break;
case 4: exit(0);
break;
void insert()
{
int item;
scanf("%d",&item);
if(r==max-1)
printf("\nOVERFLOW\n");
return;
if(f ==-1&&r==-1)
f= 0;
r= 0;
else
r= r+1;
q[r] = item;
void removing()
int item;
if (f==-1 || f>r)
{
printf("\nUNDERFLOW\n");
return;
else
item = q[f];
if(f == r)
f= -1;
r= -1 ;
else
f= f+1;
void display()
int i;
if(r==-1)
{
printf("\nEmpty queue\n");
else
for(i=f;i<=r;i++)
printf("\n%d\n",q[i]);
Output
******************Queue Operations*********************
1.insert an element
2.Delete an element
4.Exit
Value inserted
******************Queue Operations*********************
1.insert an element
2.Delete an element
4.Exit
Value inserted
******************Queue Operations*********************
1.insert an element
2.Delete an element
4.Exit
50
100
=============================================================================
8) Program to implement dynamic array. Find smallest and largest
element.
#include<stdio.h>
#include<stdlib.h>
int main() {
scanf("%d", &n);
scanf("%d", (arr+i));
}
max = arr[0];
max = arr[i];
min = arr[0];
min = arr[i];
Output
Enter how many elements you want to enter: 5
Enter 5 elements: 3 6 4 7 2
int i, j, k;
int n1 = m - b + 1;
int n2 = e - m;
i = 0;
j = 0;
k = b;
a[k] = LA[i];
i++;
}
else
a[k] = RA[j];
j++;
k++;
while (i<n1)
a[k] = LA[i];
i++;
k++;
while (j<n2)
a[k] = RA[j];
j++;
k++;
{
if (b<e)
int m = (b+e)/2;
mergeSort(a, b, m);
mergeSort(a, m + 1, e);
merge(a, b, m, e);
int i;
printf("\n");
int main()
int a[20],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printArray(a, n);
mergeSort(a, 0, n - 1);
printArray(a, n);
return 0;
Output
How many numbers you want to enter?: 7
Enter 7 numbers: 4 3 2 6 1 5 7
4326157
1234567
=============================================================================
2)Program to implement circular queue using array
#include <stdio.h>
# define max 5
int q[max];
int f=-1;
int r=-1;
{
if(f==-1 && r==-1)
f=0;
r=0;
q[r]=ele;
else if((r+1)%max==f)
printf("Queue is overflow..");
else
r=(r+1)%max;
q[r]=ele;
int dequeue()
printf("\nQueue is underflow..");
else if(f==r)
f=-1;
r=-1;
else
f=(f+1)%max;
void display()
int i=f;
else
while(i<=r)
printf("%d,", q[i]);
i=(i+1)%max;
}
}
int main()
int ch=1,x;
scanf("%d", &ch);
switch(ch)
scanf("%d", &x);
enqueue(x);
break;
case 2: dequeue();
break;
case 3: display();
break;
default: printf("Enter correct choice next time.");
return 0;
Output
******Circular Queue *******
=============================================================================
3) Program to search an element using recursive binary search technique
#include <stdio.h>
int m;
if (b > e)
return -1;
m=(b+e)/2;
if (a[m] == ele)
return m;
BST(a, m + 1, e,ele);
int main()
int a[20],n,i,pos=0,ele=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d", &ele);
if (pos == -1)
else
return 0;
Output
How many numbers you want to enter?: 5
Enter 5 numbers in ascending order: 2 4 6 8 10
=============================================================================
4) Program to implement Stack operations using linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
if (top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("Node is Inserted\n\n");
}
int pop()
if (top == NULL)
printf("\nStack Underflow\n");
else {
top = top->next;
free(temp);
return temp_data;
void display() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
int main() {
while (1) {
scanf("%d", &ch);
switch (ch)
scanf("%d", &value);
push(value);
break;
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nWrong Choice\n");
Output
****Implementaion of Stack using Linked List****
1. Push
2. Pop
3. Display
4. Exit
Node is Inserted
1. Push
2. Pop
3. Display
4. Exit
Node is Inserted
1. Push
2. Pop
3. Display
4. Exit
The stack is
30--->20--->NULL
=============================================================================
5)Program to implement Queue operations using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insert()
{
struct node *ptr;
int item;
if(ptr == NULL)
printf("\nOVERFLOW\n");
return;
else
scanf("%d",&item);
if(f == NULL)
f= ptr;
r = ptr;
else
r->next = NULL;
void delete ()
if(f == NULL)
printf("\nUNDERFLOW\n");
return;
else
ptr = f;
f = f-> next;
free(ptr);
printf("Item deleted.\n");
void display()
if(f == NULL)
printf("\nEmpty queue\n");
else
while(ptr != NULL)
void main ()
int ch;
while(ch != 4)
printf("\n*************************Main Menu*****************************");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
break;
Output
*************************Main Menu*****************************
1.Insert an element
2.Delete an element
4.Exit
Enter value: 50
*************************Main Menu*****************************
1.Insert an element
2.Delete an element
4.Exit
Enter value: 70
*************************Main Menu*****************************
1.Insert an element
2.Delete an element
4.Exit
50
70
=============================================================================
6) Program to evaluate postfix expression.
#include<stdio.h>
#include<ctype.h>
int stack[20];
void push(int x)
stack[++top] = x;
int pop()
return stack[top--];
int main()
char exp[20];
char *e;
int n1,n2,n3,num;
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
num = *e - 48;
push(num);
else
n1 = pop();
n2 = pop();
switch(*e)
break;
break;
break;
break;
push(n3);
e++;
}
return 0;
Output
Enter the expression :: 352*+
=============================================================================
7) Program to perform insert node at the end, delete a given node and
display contents of singly linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insert_end()
int value;
printf("\nOVERFLOW");
else
printf("Enter value:");
scanf("%d",&value);
p->data=value;
if(start==NULL)
p->next=NULL;
start=p;
else
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=p;
p->next=NULL;
}
}
void Delete()
int loc,i;
scanf("%d",&loc);
p=start;
for(i=0;i<loc;i++)
p1=p;
p=p->next;
if(p==NULL)
printf("Can't delete");
return;
p1->next=p->next;
free(p);
}
void display()
p=start;
if(p==NULL)
printf("Nothing to print");
else
printf("printing values...");
while (p!=NULL)
printf("\n%d",p->data);
p=p->next;
void main ()
int ch=0;
while(ch!=4)
scanf("%d",&ch);
switch(ch)
case 1: insert_end();
break;
case 2: Delete();
break;
case 3: display();
break;
case 4: exit(0);
break;
Output
*****Single Linked List Operations*****
3.Show
4.Exit
3.Show
4.Exit
Enter value:200
3.Show
4.Exit
Enter value:300
3.Show
4.Exit
100
200
300
=============================================================================
8) Menu driven program for the following operations on Binary Search
Tree (BST) of Integers
(a) Create a BST of N Integers
(b) Traverse the BST in Inorder, Preorder and Post Order
#include<stdio.h>
#include<stdlib.h>
struct BST
int data;
};
NODE create()
NODE temp;
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
if (root->lchild == NULL)
root->lchild = newnode;
else
insert(root->lchild, newnode);
}
if (root->rchild == NULL)
root->rchild = newnode;
else
insert(root->rchild, newnode);
int key;
NODE cur;
if(root == NULL)
printf("\nBST is empty.");
return;
scanf("%d", &key);
cur = root;
if (cur->data == key)
return;
cur = cur->lchild;
else
cur = cur->rchild;
if(root != NULL)
inorder(root->lchild);
inorder(root->rchild);
}
void preorder(NODE root)
if (root != NULL)
preorder(root->lchild);
preorder(root->rchild);
if (root != NULL)
postorder(root->lchild);
postorder(root->rchild);
void main()
while(1)
printf("\n******BST MENU*******");
printf("\n1.Create a BST");
printf("\n3.Search");
printf("\n4.Exit");
scanf("%d", &ch);
switch(ch)
scanf("%d", &n);
for(i=1;i<=n;i++)
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
break;
else
preorder(root);
inorder(root);
postorder(root);
break;
case 3: search(root);
break;
case 4: exit(0);
break;
Output
******BST MENU*******
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
******BST MENU*******
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
============================================================================
Created by:
Mohan L S
1st BCA