Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
V SEMESTER
SUBMITTED
BY
NAME : ………………………………
REG.NO : ……………….………………
September 2019
CERTIFICATE
This is to certify that the bonafide record work is done by
_______________________________________ in Fifth semester of B.Sc (Computer Science)
course in DATA STRUCTURES AND ALGORITHMS LAB (BCS17R372), at
Department of Computer Science and Information Technology, Kalasalingam Academy of
Research And Education, Krishnankoil during June 2019 – September 2019.
S.
Date Title Page Marks Signature
No.
4 19
5/8/19 INFIX-POSTFIX OPERATIONS
5 26
19/8/19 BUBBLE SORT
6 29
26/8/19 SELECTION SORT
7 32
9/9/19 INSERTION SORT
8 35
16/9/19 QUICK SORT
9 39
16/9/19 HEAP SORT
10 43
23/9/19 BINARY SEARCH
11 46
30/9/19 LINEAR SEARCH
Ex. No : 1
STACK OPERATIONS USING ARRAYS
Date:18-07-19
AIM
ALGORITHM
Step 7: Using select statement we can go with the user choice of operations.
CODING
#include<stdio.h>
#define max 10
int s[max];
int top=0;
void push();
void pop();
void display();
1
void main()
char ch;
int choice;
clrscr();
do
printf("1.push()\n2.pop()\n3.display()\n");
scanf("%d",&choice);
printf("\n");
switch(choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
default:
printf("Invalid option\n");
2
printf("Do u want to continue? (y/n) : ");
fflush(stdin);
scanf("%c",&ch);
printf("\n");
}while(ch=='Y'||ch=='y');
void push()
int item;
if(top>=max)
printf("stack is full\n");
else
scanf("%d",&item);
top++;
s[top]=item;
void pop()
int item;
3
if(top==0)
printf("stack is empty\n");
else
item=s[top];
top--;
void display()
int item;
int i;
if(top==0)
else
printf("-----------------------------------\n");
for(i=1;i<=top;i++)
printf("%d\n",s[i]);
printf("-----------------------------------\n");
4
OUTPUT
5
RESULT
6
Ex. No : 2
QUEUE OPERATIONS USING ARRAYS
Date:18-07-19
AIM
ALGORITHM
Step 7: Using select statement we can go with the user choice of operations.
CODING
#include<stdio.h>
#include<conio.h>
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();
int main()
7
{
int option;
clrscr();
do
printf("\n 4.Exit");
scanf("%d",&option);
switch(option)
case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: return 0;
}while(option!=4);
return 0;
8
}
void insert_element()
int num;
scanf("%d",&num);
else if(front==-1&&rear==-1)
front=rear=0;
queue[rear]=num;
rear=0;
queue[rear]=num;
else
rear++;
queue[rear]=num;
9
}
void delete_element()
int element;
if(front==-1)
printf("\n Underflow");
element=queue[front];
if(front==rear)
front=rear=-1;
else
if(front==MAX-1)
front=0;
else
front++;
void display_queue()
int i;
if(front==-1)
10
printf("\n No elements to display");
else
for(i=front;i<=rear;i++)
printf("\t %d",queue[i]);
} } }
OUTPUT
11
RESULT
12
Ex. No : 3
CIRCULAR QUEUE OPERATIONS USING ARRAYS
Date:25-07-19
AIM
To write a program using C language to implement circular queue operations using arrays
ALGORITHM
Step 7: Using select statement we can go with the user choice of operations.
CODING
#include<stdio.h>
#define max 3
int q[10],front=0,rear=-1;
void main()
int ch;
void insert();
void delet();
13
void display();
clrscr();
while(1)
{ printf("1.insert\n2.delete\n3.display\n4.exit\n");
scanf("%d",&ch);
switch(ch)
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");
void insert()
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
14
printf("Queue is overflow\n");
else
scanf("%d",&x);
if(rear==max-1&&front>0)
rear=0;
q[rear]=x;
else
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
void delet()
int a;
if((front==0)&&(rear==-1))
printf("Queue is underflow\n");
getch();
exit();
15
}
if(front==rear)
a=q[front];
rear=-1;
front=0;
else
if(front==max-1)
a=q[front];
front=0;
else a=q[front++];
void display()
int i,j;
if(front==0&&rear==-1)
printf("Queue is underflow\n");
getch();
exit();
16
}
if(front>rear)
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
else
for(i=front;i<=rear;i++)
printf("\t%d",q[i]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
printf("\n");
17
OUTPUT
RESULT
18
Ex. No : 4
INFIX-POSTFIX OPERATIONS
Date:05-08-19
AIM
ALGORITHM
Step 7 : Pop function is used to delete the value from the stack
Step 9 : Push the values into stack one by one after checking the precedence
CODING
#include<stdio.h>
#include<string.h>
enum{False,True};
#define add 1
#define sub 1
#define mul 2
#define div 2
19
#define exp 3
void push(char[],char);
char pop(char[]);
int stack_empty();
void main()
char s[100];
void i2p(char[]);
clrscr();
scanf("%s",s);
i2p(s);
getch();
switch(symbol)
case '(':
return 1;
case ')':
return 2;
case '+':
20
case '-':
case '*':
case '/':
case '^':
return 4;
default:
return 3;
switch(symbol)
case '+':
return add;
case '-':
return sub;
case '*':
return mul;
case'/':
return div;
case'^':
return exp;
case '(':
21
return 0;
default:
return 1;
int i=0,p=0,len,type,prec;
char next,stack[20],postfix[20];
int get_type(char);
int get_precedence(char);
len=strlen(infix);
while(i<len)
type=get_type(infix[i]);
switch(type)
case 1:
push(stack,infix[i]);
break;
case 2:
while((next=pop(stack))!='(')
22
postfix[p++]=next;
break;
case 3:
postfix[p++]=infix[i];
break;
case 4:
prec=get_precedence(infix[i]);
while(stack_empty()==False)
if(prec<=get_precedence(stack[top-1]))
postfix[p++]=pop(stack);
else
break;
push(stack,infix[i]);
break;
i++;
while(stack_empty()==False)
postfix[p++]=pop(stack);
postfix[p]=' ';
printf("%s\n",postfix);
23
void push(char stack[],char data)
stack[top]=data;
top++;
char data;
top--;
data=stack[top];
return data;
int stack_empty()
if(top==0)
return True;
else
return False;
24
OUTPUT
RESULT
25
Ex. No : 5
BUBBLE SORT
Date:19-08-19
AIM
ALGORITHM
Step 4: If the first elements greater than second then interchange else keep the values as it
CODING
#include<stdio.h>
void main()
int a[10],i,j,temp,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
26
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(i=0;i<n;i++)
printf("%d\t",a[i]);
} getch();
27
OUTPUT
RESULT
28
Ex. No : 6
SELECTION SORT
Date:26-08-19
AIM
ALGORITHM
Step 4: Consider the first element as minimum element store its index value in a variable
Step 6: Compare the minimum with rest of all elements to find minimum value and interchange the
minimum value with the first element
CODING
#include<stdio.h>
void main()
int a[10],i,j,temp,n;
int min,loc;
clrscr();
scanf("%d",&n);
29
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
min=a[i];
loc=i;
for(j=i+1;j<=n;j++)
if(min>a[j])
min=a[j];
loc=j;
if(loc!=i)
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
for(i=0;i<n;i++)
30
printf("%d\t",a[i]);
getch();
OUTPUT
RESULT
31
Ex. No : 7
INSERTION SORT
Date:09-09-19
AIM
ALGORITHM
Step 4: Compare the first two elements of a list if first element is greater than second interchange it
else keep the list as it is.
Step 5: Now take three elements from the list and sort them as follows
CODING
#include <stdio.h>
void main()
clrscr();
scanf("%d", &n);
scanf("%d", &a[i]);
32
for (i = 1 ; i <= n - 1; i++)
j = i;
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
j--;
printf("%d\t", a[i]);
getch();
33
OUTPUT
RESULT
34
Ex. No : 8
QUICK SORT
Date:16-09-19
AIM
ALGORITHM
Step 5: Keep the first element as 'pivot' and correct its position in the list
CODING
#include<stdio.h>
int t = *a;
*a = *b;
*b = t;
35
int partition (int arr[], int low, int high)
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
quicksort(arr, pi + 1, high);
36
}
int i;
printf("\n");
void main()
int a[10],i,low,high,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
low=0;
high=n-1;
37
quicksort(a,low,high);
display(a,n);
getch();
OUTPUT
RESULT
38
Ex. No : 9
HEAP SORT
Date:16-09-19
AIM
ALGORITHM
Step 5: Re arrange the remaining elements from a heap this process is continued till we get sorted
list
CODING
#include <stdio.h>
void main()
clrscr();
scanf("%d", &no);
scanf("%d", &heap[i]);
39
{
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];
heap[0] = heap[j];
heap[j] = temp;
root = 0;
do
c = 2 * root + 1;
40
c++;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
root = c;
getch();
41
OUTPUT
RESULT
42
Ex. No : 10
BINARY SEARCH
Date:23-09-19
AIM
ALGORITHM
Step 6: If the middle value is equal to the target then we declare victory and stop.
Step 7: If the middle item is less than the target, then we set the new list to be the upper half of the
old list and we repeat from step 2 using the new list.
Step 8: If the middle value is greater than the target, then we set the new list to be the bottom half
of the list, and we repeat from step 2 with the new list.
CODING
#include <stdio.h>
void main()
clrscr();
43
scanf("%d",&n);
scanf("%d",&array[c]);
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
first = middle + 1;
break;
else
last = middle - 1;
getch();
44
OUTPUT
RESULT
45
Ex. No : 11
PROGRAM TO IMPLEMENT LINEAR SEARCH
Date:30-09-19
AIM
ALGORITHM
Step 6: If the current value matches the target then we declare victory and stop.
Step 7: If the current value is less than the target then set the current item to be the next item and
repeat from 2.
CODING
#include<stdio.h>
void main()
int list[10],key,found,num,i;
clrscr();
scanf("%d",&num);
printf("Enter %d elements\n",num);
46
for(i=0;i<num;i++)
scanf("%d",&list[i]);
scanf("%d",&key);
for(i=0;i<num;i++)
if(key==list[i])
found=1;
if(found!=1)
printf("search is unsucessful");
getch();
47
OUTPUT
RESULT
48