You are on page 1of 58

Part –A

1. Program to sort the given list using selection sort technique.


#include <stdio.h>

void selection(int a[], int n);

void printArr(int a[], int n);

int main()

int a[20],i,n;

printf("Enter how many elements you want:");

scanf("%d",&n);

printf("Enter the elements:");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Before sorting array elements are - \n");

printArr(a, n);

selection(a, n);

printf("\nAfter sorting array elements are - \n");

printArr(a, n);

return 0;

void selection(int a[], int n)

{
int i, j, small,temp;

for (i = 0; i < n-1; i++)

small = i;

for (j = i+1; j < n; j++)

if(a[j] < a[small])

small = j;

temp = a[small];

a[small] = a[i];

a[i] = temp;

void printArr(int a[], int n)

int i;

for (i = 0; i < n; i++)

printf("%d ", a[i]);

Output
Enter how many elements you want:5

Enter the elements:20 50 10 40 30


Before sorting array elements are -

20 50 10 40 30

After sorting array elements are -

10 20 30 40 50

2) Program to sort the given list using insertion sort technique.


#include <stdio.h>

void insert(int a[], int n)

int i, j, tmp;

for (i = 1; i < n; i++)

tmp = a[i];

j = i - 1;

while(j>=0 && tmp <= a[j])

a[j+1] = a[j];

j = j-1;

a[j+1] = tmp;

void printArr(int a[], int n)

int i;
for (i = 0; i < n; i++)

printf("%d ", a[i]);

void main()

{ int a[20],i,n;

printf("Enter how many elements you want:");

scanf("%d",&n);

printf("Enter the elements:");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Before applying insertion sort technique - \n");

printArr(a, n);

insert(a, n);

printf("\nAfter applying insertion sort technique - \n");

printArr(a, n);

Output
Enter how many elements you want:6

Enter the elements:3 4 2 5 1 6

Before applying insertion sort technique -

342516

After applying insertion sort technique -

123456
3) Program to sort the given list using bubble sort technique

#include<stdio.h>

int main()

int n, tmp, i, j, a[30];

printf("How many numbers you want to enter?: ");

scanf("%d",&n);

printf("Enter %d numbers: ",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Before sorted elements are:\n ");

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;

}
}

printf("\n After sorted elements are:\n ");

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

Before sorted elements are:

8 6 9 7 10

After sorted elements are:

6 7 8 9 10

=============================================================================

4) Program to search an element using linear search technique.


#include<stdio.h>

int main()

int a[10],n,ele,i,pos;

printf("How many numbers you want to enter?: ");

scanf("%d",&n);

printf("Enter %d numbers: ",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);
printf("Enter the search element: ");

scanf("%d",&ele);

pos=-1;

for(i=0; i<n ;i++)

if(ele == a[i])

pos = i;

break;

printf("The array elements are:");

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

printf("\n The element is not found.");

return 0;

Output
How many numbers you want to enter?: 5

Enter 5 numbers: 25 3 6 4 65
ôEnter the search element: 65

The array elements are: 25 3 6 4 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;

printf("How many numbers you want to enter?: ");

scanf("%d",&n);

printf("Enter %d numbers in ascending order: ",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter the search element: ");

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)

printf("The Position='%d' \n",pos+1);

else

printf("he element is not found.\n");

printf("And The array elements are:");

for(i=0;i<n;i++)

printf(" %d",a[i]);

return 0;

Output
How many numbers you want to enter?: 6

Enter 6 numbers in ascending order: 22 33 44 55 66 77


Enter the search element: 66

The Position='5'

And The array elements are: 22 33 44 55 66 77

=============================================================================

6) Program to implement Stack operations using arrays.


#include <stdio.h>

int stack[10],i,j,ch=0,n,top=-1;

void push ()

int val;

if (top == n )

printf("\n Overflow");

else

printf("Enter the value:");

scanf("%d",&val);

top = top +1;

stack[top] = val;

void pop ()

if(top == -1)
printf("Underflow");

else

top = top -1;

void show()

printf("The stack elements are:");

for (i=top;i>=0;i--)

printf("%d ",stack[i]);

if(top == -1)

printf("Stack is empty");

void main ()

printf("How many elements you want to enter in stack: ");

scanf("%d",&n);

printf("*********Stack operations using array*********");

printf("\n----------------------------------------------");

while(ch!= 4)

printf("\nChose one from the below options...");

printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");

printf("\n Enter your choice :\n");


scanf("%d",&ch);

switch(ch)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

show();

break;

case 4:

printf("Exiting....");

break;

default:
{

printf("Please Enter valid choice ");

Output
How many elements you want to enter in stack: 5

*********Stack operations using array*********

----------------------------------------------

Chose one from the below options...

1.Push

2.Pop

3.Show

4.Exit

Enter your choice :

Enter the value:25

Chose one from the below options...

1.Push

2.Pop

3.Show

4.Exit
Enter your choice :

Enter the value:35

Chose one from the below options...

1.Push

2.Pop

3.Show

4.Exit

Enter your choice :

The stack elements are:35 25

=============================================================================

7) Program to implement Queue operations using arrays


#include<stdio.h>

#include<stdlib.h>

#define max 5

void insert();

void removing();

void display();

int f = -1, r = -1;

int q[max];

void main ()

{
int ch;

while(ch!= 4)

printf("\n******************Queue Operations*********************\n");

printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit");

printf("\nEnter your choice ?");

scanf("%d",&ch);

switch(ch)

case 1: insert();

break;

case 2: removing();

break;

case 3: display();

break;

case 4: exit(0);

break;

default: printf("\nEnter valid choice??\n");

void insert()

{
int item;

printf("\nEnter the element:");

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;

printf("\nValue inserted ");

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;

printf("\nvalue deleted ");

void display()

int i;

if(r==-1)
{

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

for(i=f;i<=r;i++)

printf("\n%d\n",q[i]);

Output
******************Queue Operations*********************

1.insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice ?1

Enter the element:50

Value inserted

******************Queue Operations*********************
1.insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice ?1

Enter the element:100

Value inserted

******************Queue Operations*********************

1.insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice ?3

printing values .....

50

100

=============================================================================
8) Program to implement dynamic array. Find smallest and largest
element.
#include<stdio.h>

#include<stdlib.h>

int main() {

int n, *arr, max, min;

printf("\nEnter how many elements you want to enter: ");

scanf("%d", &n);

arr = (int*) calloc(n, sizeof(int));

printf("\nEnter %d elements: ",n);

for(int i = 0; i < n; i++) {

scanf("%d", (arr+i));

printf("\nThe array elements are: ");

for(int i = 0; i < n; i++)

(i != n - 1) ? printf(" %d", *(arr+i)) : printf(" %d", *(arr+i));

}
max = arr[0];

for(int i = 0; i < n; i++) {

if(max > arr[i]) {

max = arr[i];

min = arr[0];

for(int i = 0; i < n; i++) {

if(min < arr[i]) {

min = arr[i];

printf("\nMinimum number in the given array =%d",min);

printf("\nMaximum number in the given array =%d", max);

Output
Enter how many elements you want to enter: 5

Enter 5 elements: 3 6 4 7 2

The array elements are: 3 6 4 7 2

Minimum number in the given array =7

Maximum number in the given array =2


Part-B
1) Program to sort the given list using merge sort technique
#include <stdio.h>

void merge(int a[], int b, int m, int e)

int i, j, k;

int n1 = m - b + 1;

int n2 = e - m;

int LA[n1], RA[n2];

for (i = 0; i < n1; i++)

LA[i] = a[b + i];

for (j = 0; j < n2; j++)

RA[j] = a[m + 1 + j];

i = 0;

j = 0;

k = b;

while (i < n1 && j < n2)

if(LA[i] <= RA[j])

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++;

void mergeSort(int a[], int b, int e)

{
if (b<e)

int m = (b+e)/2;

mergeSort(a, b, m);

mergeSort(a, m + 1, e);

merge(a, b, m, e);

void printArray(int a[], int n)

int i;

for (i = 0; i < n; i++)

printf("%d ", a[i]);

printf("\n");

int main()

int a[20],n,i;

printf("How many numbers you want to enter?: ");

scanf("%d",&n);

printf("Enter %d numbers: ",n);

for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("Before sorting array elements are - \n");

printArray(a, n);

mergeSort(a, 0, n - 1);

printf("After sorting array elements are - \n");

printArray(a, n);

return 0;

Output
How many numbers you want to enter?: 7

Enter 7 numbers: 4 3 2 6 1 5 7

Before sorting array elements are -

4326157

After sorting array elements are -

1234567

=============================================================================
2)Program to implement circular queue using array
#include <stdio.h>

# define max 5

int q[max];

int f=-1;

int r=-1;

void enqueue(int ele)

{
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()

if((f==-1) && (r==-1))

printf("\nQueue is underflow..");

else if(f==r)

printf("\nThe dequeued element is %d", q[f]);

f=-1;
r=-1;

else

printf("\nThe dequeued element is %d", q[f]);

f=(f+1)%max;

void display()

int i=f;

if(f==-1 && r==-1)

printf("\n Queue is empty..");

else

printf("\nElements in a Queue are :");

while(i<=r)

printf("%d,", q[i]);

i=(i+1)%max;

}
}

int main()

int ch=1,x;

while(ch<4 && ch!=0)

printf("\n******Circular Queue *******");

printf("\nPress 1: Insert an element");

printf("\nPress 2: Delete an element");

printf("\nPress 3: Display the element");

printf("\nEnter your choice:");

scanf("%d", &ch);

switch(ch)

case 1: printf("Enter the element which is to be inserted:");

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 *******

Press 1: Insert an element

Press 2: Delete an element

Press 3: Display the element

Enter your choice:1

Enter the element which is to be inserted:45

******Circular Queue *******

Press 1: Insert an element

Press 2: Delete an element

Press 3: Display the element

Enter your choice:1

Enter the element which is to be inserted:50

******Circular Queue *******

Press 1: Insert an element

Press 2: Delete an element


Press 3: Display the element

Enter your choice:2

The dequeued element is 45

******Circular Queue *******

Press 1: Insert an element

Press 2: Delete an element

Press 3: Display the element

Enter your choice:3

Elements in a Queue are :50,

=============================================================================
3) Program to search an element using recursive binary search technique
#include <stdio.h>

int BST(int a[], int b, int e, int ele)

int m;

if (b > e)

return -1;

m=(b+e)/2;

if (a[m] == ele)

return m;

else if (a[m] > ele)


BST(a, b, m - 1, ele);

else if (a[m] < ele)

BST(a, m + 1, e,ele);

int main()

int a[20],n,i,pos=0,ele=0;

printf("How many numbers you want to enter?: ");

scanf("%d",&n);

printf("Enter %d numbers in ascending order: ",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter item to search: ");

scanf("%d", &ele);

pos= BST(a, 0, n, ele);

if (pos == -1)

printf("Item not found in array\n");

else

printf("Element found in the position= %d\n",pos+1);

return 0;

Output
How many numbers you want to enter?: 5
Enter 5 numbers in ascending order: 2 4 6 8 10

Enter item to search: 10

Element found in the position= 5

=============================================================================
4) Program to implement Stack operations using linked list.
#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

struct Node* top = NULL;

void push(int value)

struct Node *newNode;

newNode = (struct Node *)malloc(sizeof(struct Node));

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 {

struct Node *temp = top;

int temp_data = top->data;

top = top->next;

free(temp);

return temp_data;

void display() {

if (top == NULL) {

printf("\nStack Underflow\n");

} else {

printf("The stack is \n");

struct Node *temp = top;

while (temp->next != NULL) {

printf("%d--->", temp->data);

temp = temp->next;
}

printf("%d--->NULL\n\n", temp->data);

int main() {

int ch, value;

printf("****Implementaion of Stack using Linked List****\n");

while (1) {

printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");

printf("\nEnter your choice : ");

scanf("%d", &ch);

switch (ch)

case 1: printf("\nEnter the value to insert: ");

scanf("%d", &value);

push(value);

break;

case 2: printf("Popped element is :%d\n", pop());

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

Enter your choice : 1

Enter the value to insert: 20

Node is Inserted

1. Push

2. Pop

3. Display

4. Exit

Enter your choice : 1


Enter the value to insert: 30

Node is Inserted

1. Push

2. Pop

3. Display

4. Exit

Enter your choice : 3

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;

struct node *next;

};

struct node *f;

struct node *r;

void insert()

{
struct node *ptr;

int item;

ptr = (struct node *) malloc (sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW\n");

return;

else

printf("\nEnter value: ");

scanf("%d",&item);

ptr -> data = item;

if(f == NULL)

f= ptr;

r = ptr;

f -> next = NULL;

r -> next = NULL;

else

r -> next = ptr;


r = ptr;

r->next = NULL;

void delete ()

struct node *ptr;

if(f == NULL)

printf("\nUNDERFLOW\n");

return;

else

ptr = f;

f = f-> next;

free(ptr);

printf("Item deleted.\n");

void display()

struct node *ptr;


ptr = f;

if(f == NULL)

printf("\nEmpty queue\n");

else

{ printf("\nprinting values .....\n");

while(ptr != NULL)

printf("\n%d\n",ptr -> data);

ptr = ptr -> next;

void main ()

int ch;

while(ch != 4)

printf("\n*************************Main Menu*****************************");

printf("\n1.Insert an element\n2.Delete an element\n3.Display the queue\n4.Exit");

printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch)
{

case 1: insert();

break;

case 2: delete();

break;

case 3: display();

break;

case 4: exit(0);

break;

default: printf("\nSorry| Enter valid choice.\n");

Output
*************************Main Menu*****************************

1.Insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice:1

Enter value: 50
*************************Main Menu*****************************

1.Insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice:1

Enter value: 70

*************************Main Menu*****************************

1.Insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice:3

printing values .....

50

70

=============================================================================
6) Program to evaluate postfix expression.
#include<stdio.h>
#include<ctype.h>

int stack[20];

int top = -1;

void push(int x)

stack[++top] = x;

int pop()

return stack[top--];

int main()

char exp[20];

char *e;

int n1,n2,n3,num;

printf("Enter the expression :: ");

scanf("%s",exp);

e = exp;

while(*e != '\0')

{
if(isdigit(*e))

num = *e - 48;

push(num);

else

n1 = pop();

n2 = pop();

switch(*e)

case '+': n3 = n1 + n2;

break;

case '-': n3 = n2 - n1;

break;

case '*': n3 = n1 * n2;

break;

case '/': n3 = n2 / n1;

break;

push(n3);

e++;
}

printf("\nThe result of expression %s = %d\n\n",exp,pop());

return 0;

Output
Enter the expression :: 352*+

The result of expression 352*+ = 13

=============================================================================
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;

struct node *next;

};

struct node *start;

void insert_end()

struct node *p,*temp;

int value;

p=(struct node*)malloc(sizeof(struct node));


if(p==NULL)

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()

struct node *p,*p1;

int loc,i;

printf("Enter the location of the node(0 to n-1) :");

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);

printf("Deleted node %d ",loc+1);

}
void display()

struct node *p;

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)

printf("\n*****Single Linked List Operations*****");


printf("\n1.Insert at the begining\n2.Delete node at given position\n3.Show\n4.Exit\n");

printf("Enter your choice:");

scanf("%d",&ch);

switch(ch)

case 1: insert_end();

break;

case 2: Delete();

break;

case 3: display();

break;

case 4: exit(0);

break;

default: printf("Enter valid option");

Output
*****Single Linked List Operations*****

1.Insert at the begining

2.Delete node at given position

3.Show

4.Exit

Enter your choice:1


Enter value:100

*****Single Linked List Operations*****

1.Insert at the begining

2.Delete node at given position

3.Show

4.Exit

Enter your choice:1

Enter value:200

*****Single Linked List Operations*****

1.Insert at the begining

2.Delete node at given position

3.Show

4.Exit

Enter your choice:1

Enter value:300

*****Single Linked List Operations*****

1.Insert at the begining

2.Delete node at given position

3.Show

4.Exit

Enter your choice:3


printing values...

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;

struct BST *lchild;

struct BST *rchild;

};

typedef struct BST * NODE;

NODE create()

NODE temp;

temp = (NODE) malloc(sizeof(struct BST));

printf("Enter The value:");


scanf("%d", &temp->data);

temp->lchild = NULL;

temp->rchild = NULL;

return temp;

void insert(NODE root, NODE newnode);

void inorder(NODE root);

void preorder(NODE root);

void postorder(NODE root);

void search(NODE root);

void insert(NODE root, NODE newnode)

if (newnode->data < root->data)

if (root->lchild == NULL)

root->lchild = newnode;

else

insert(root->lchild, newnode);
}

if (newnode->data > root->data)

if (root->rchild == NULL)

root->rchild = newnode;

else

insert(root->rchild, newnode);

void search(NODE root)

int key;

NODE cur;

if(root == NULL)

printf("\nBST is empty.");

return;

printf("\nEnter Element to be searched: ");

scanf("%d", &key);

cur = root;

while (cur != NULL)


{

if (cur->data == key)

printf("\nKey element is present in BST");

return;

if (key < cur->data)

cur = cur->lchild;

else

cur = cur->rchild;

printf("\nKey element is not found in the BST");

void inorder(NODE root)

if(root != NULL)

inorder(root->lchild);

printf("%d ", root->data);

inorder(root->rchild);

}
void preorder(NODE root)

if (root != NULL)

printf("%d ", root->data);

preorder(root->lchild);

preorder(root->rchild);

void postorder(NODE root)

if (root != NULL)

postorder(root->lchild);

postorder(root->rchild);

printf("%d ", root->data);

void main()

int ch, key, val, i, n;


NODE root = NULL, newnode;

while(1)

printf("\n******BST MENU*******");

printf("\n1.Create a BST");

printf("\n2.BST Traversals: ");

printf("\n3.Search");

printf("\n4.Exit");

printf("\nEnter your choice: ");

scanf("%d", &ch);

switch(ch)

case 1: printf("\nEnter the number of elements: ");

scanf("%d", &n);

for(i=1;i<=n;i++)

newnode = create();

if (root == NULL)

root = newnode;

else

insert(root, newnode);

break;

case 2: if (root == NULL)


printf("\nTree Is Not Created");

else

printf("\nThe Preorder display : ");

preorder(root);

printf("\nThe Inorder display : ");

inorder(root);

printf("\nThe Postorder display : ");

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

Enter your choice: 1

Enter the number of elements: 5

Enter The value:3

Enter The value:6

Enter The value:5

Enter The value:7

Enter The value:4

******BST MENU*******

1.Create a BST

2.BST Traversals:

3.Search

4.Exit

Enter your choice: 2

The Preorder display : 3 6 5 4 7

The Inorder display : 3 4 5 6 7

The Postorder display : 4 5 7 6 3

============================================================================

Created by:

Mohan L S

1st BCA

You might also like