Sei sulla pagina 1di 51

BACHELOR OF SCIENCE IN COMPUTER SCIENCE

V SEMESTER

DATA STRUCTURES AND ALGORITHMS LAB


(BCS17R372)

SUBMITTED
BY

NAME : ………………………………

REG.NO : ……………….………………

DEPARTMENT OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY

KALASALINGAM ACADEMY OF RESEARCH AND EDUCATION


(Under Section 3 of the UGC Act 1956)
Anand Nagar, Krishnankoil - 626126 Tamil Nadu, India

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.

HEAD OF THE DEPARTMENT STAFF-IN-CHARGE

Submitted for the Practical examination held on………………at the


Department of Computer Science and Information Technology, Kalasalingam Academy of
Research And Education, Krishnankoil.

INTERNAL EXAMINER EXTERNAL EXAMINER


LIST OF PROGRAMS

S.
Date Title Page Marks Signature
No.

1 STACK OPERATIONS USING 1


18/7/19
ARRAYS

2 QUEUE OPERATIONS USING 7


18/7/19
ARRAYS

3 CIRCULAR QUEUE OPERATIONS 13


25/7/19 USING ARRAY

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

To write a program using C language to implement Stack operations using arrays

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Declare global variables (Size of the stack)

Step 4: Declare the functions globally

Step 5: Open main function

Step 6: Do…while loop is used to repeatedly perform the stack operations

Step 7: Using select statement we can go with the user choice of operations.

Step 8: Define the function

Step 9: Stop the program

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("enter choice of operation\n");

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

printf("enter any item : ");

scanf("%d",&item);

top++;

s[top]=item;

void pop()

int item;

3
if(top==0)

printf("stack is empty\n");

else

item=s[top];

printf("the related elemnt is %d\n",item);

top--;

void display()

int item;

int i;

if(top==0)

printf("\nstack is empty no element is displayed\n\n");

else

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

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

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

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

4
OUTPUT

5
RESULT

Thus, the program is executed successfully and the output is verified

6
Ex. No : 2
QUEUE OPERATIONS USING ARRAYS
Date:18-07-19

AIM

To write a program using C language to implement queue operations using arrays

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Declare global variables (Size of the queue)

Step 4: Declare the functions globally

Step 5: Open main function

Step 6: Do…while loop is used to repeatedly perform the stack operations

Step 7: Using select statement we can go with the user choice of operations.

Step 8: Define the function

Step 9: Stop the program

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

printf("c program to implement queue operations ");

do

printf("\n\n 1.Insert an element");

printf("\n 2.Delete an element");

printf("\n 3.Display queue");

printf("\n 4.Exit");

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

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;

printf("\n Enter the number to be inserted: ");

scanf("%d",&num);

if(front==0 && rear==MAX-1)

printf("\n Queue OverFlow Occured");

else if(front==-1&&rear==-1)

front=rear=0;

queue[rear]=num;

else if(rear==MAX-1 && front!=0)

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

printf("\n The deleted element is: %d",element);

void display_queue()

int i;

if(front==-1)

10
printf("\n No elements to display");

else

printf("\n The queue elements are:\n ");

for(i=front;i<=rear;i++)

printf("\t %d",queue[i]);

} } }

OUTPUT

11
RESULT

Thus, the program is executed successfully and the output is verified

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 1: Start the program

Step 2: Declare the header files

Step 3: Declare global variables (Size of the stack)

Step 4: Declare the functions globally

Step 5: Open main function

Step 6: Do…while loop is used to repeatedly perform the stack operations

Step 7: Using select statement we can go with the user choice of operations.

Step 8: Define the function

Step 9: Stop the program

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

printf("\nCircular Queue operations\n");

while(1)

{ printf("1.insert\n2.delete\n3.display\n4.exit\n");

printf("Enter your choice:");

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

printf("Enter element to be insert:");

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

printf("Deleted element is:%d\n",a);

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

Thus, the program is executed successfully and the output is verified

18
Ex. No : 4
INFIX-POSTFIX OPERATIONS
Date:05-08-19

AIM

To write a program using C language to implement Infix-postfix operations

ALGORITHM

Step 1 : Start the program

Step 2 : Declare the header files

Step 3 : Declare global variables (operator precedence)

Step 4 : Declare the functions globally

Step 5 : Open main function

Step 6 : Push function is used to insert the value into stack

Step 7 : Pop function is used to delete the value from the stack

Step 8 : Get the expression from the user

Step 9 : Push the values into stack one by one after checking the precedence

Step 10 : Stop the program

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

int top =0;

void push(char[],char);

char pop(char[]);

int stack_empty();

void main()

char s[100];

void i2p(char[]);

clrscr();

printf("Enter the Infix expression: ");

scanf("%s",s);

printf("\n\nThe Postfix expression is: ");

i2p(s);

getch();

int get_type(char symbol)

switch(symbol)

case '(':

return 1;

case ')':

return 2;

case '+':

20
case '-':

case '*':

case '/':

case '^':

return 4;

default:

return 3;

int get_precedence(char symbol)

switch(symbol)

case '+':

return add;

case '-':

return sub;

case '*':

return mul;

case'/':

return div;

case'^':

return exp;

case '(':

21
return 0;

default:

return 1;

void i2p(char infix[])

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 pop(char stack[])

char data;

top--;

data=stack[top];

return data;

int stack_empty()

if(top==0)

return True;

else

return False;

24
OUTPUT

RESULT

Thus, the program is executed successfully and the output is verified

25
Ex. No : 5
BUBBLE SORT
Date:19-08-19

AIM

To write a program using C language to implement bubble sort technique

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Take first two elements of a list and compare them

Step 4: If the first elements greater than second then interchange else keep the values as it

Step 5: Repeat the step 4 until last comparison takes place

Step 6: Repeat step 3 to 5 until the list is sorted

Step 7: Stop the program

CODING

#include<stdio.h>

void main()

int a[10],i,j,temp,n;

clrscr();

printf("\n enter the max no.of elements u wanna sort \n");

scanf("%d",&n);

printf("\n enter the elements u want to sort \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;

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

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

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

} getch();

27
OUTPUT

RESULT

Thus, the program is executed successfully and the output is verified

28
Ex. No : 6
SELECTION SORT
Date:26-08-19

AIM

To write a program using C language to implement selection sort technique

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Take a list of unsorted values at first

Step 4: Consider the first element as minimum element store its index value in a variable

Step 5: Repeat the step 3 until last comparison takes place

Step 6: Compare the minimum with rest of all elements to find minimum value and interchange the
minimum value with the first element

Step 7: Repeat step 5 to 6 until the list is sorted

Step 8: Stop the program

CODING

#include<stdio.h>

void main()

int a[10],i,j,temp,n;

int min,loc;

clrscr();

printf("\n enter the max no.of elements u wanna sort \n");

scanf("%d",&n);

printf("\n enter the elements u want to sort \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;

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

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

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

getch();

OUTPUT

RESULT

Thus, the program is executed successfully and the output is verified

31
Ex. No : 7
INSERTION SORT
Date:09-09-19

AIM

To write a program using C language to implement insertion sort technique

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Take a list of values

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

Step 6: Repeat step 4 to 5 until the list is sorted

Step 7: Stop the program

CODING

#include <stdio.h>

void main()

int n, a[100], i, j, temp;

clrscr();

printf("\n enter the max no.of elements u wanna sort \n");

scanf("%d", &n);

printf("\n enter the elements u want to sort \n");

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

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

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

j = i;

while ( j > 0 && a[j-1] > a[j]) {

temp = a[j];

a[j] = a[j-1];

a[j-1] = temp;

j--;

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

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

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

getch();

33
OUTPUT

RESULT

Thus, the program is executed successfully and the output is verified

34
Ex. No : 8
QUICK SORT
Date:16-09-19

AIM

To write a program using C language to implement quick sort technique

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Take first a list of unsorted values

Step 4: Take first element as 'pivot'

Step 5: Keep the first element as 'pivot' and correct its position in the list

Step 6: Divide the list into two based on first element

Step 7: Combine the list

Step 8: Stop the program

CODING

#include<stdio.h>

void swap(int* a, int* b)

int t = *a;

*a = *b;

*b = t;

35
int partition (int arr[], int low, int high)

int pivot = arr[high];

int i = (low - 1),j;

for (j = low; j <= high- 1; j++)

if (arr[j] < pivot)

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return (i + 1);

void quicksort(int arr[], int low, int high)

if (low < high)

int pi = partition(arr, low, high);

quicksort(arr, low, pi - 1);

quicksort(arr, pi + 1, high);

36
}

void display(int arr[], int size)

int i;

printf("\nThe sorted elements are\n");

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

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

printf("\n");

void main()

int a[10],i,low,high,n;

clrscr();

printf("\n enter the max no.of elements u wanna sort \n");

scanf("%d",&n);

printf("\n enter the elements u want to sort \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

Thus,, the program is executed successfully and the output is verified

38
Ex. No : 9
HEAP SORT
Date:16-09-19

AIM

To write a program using C language to implement heap sort technique

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Arrange elements of a list in correct form of a binary tree

Step 4: Remove top most elements of the heap

Step 5: Re arrange the remaining elements from a heap this process is continued till we get sorted
list

Step 6: Stop the program

CODING

#include <stdio.h>

void main()

int heap[10], no, i, j, c, root, temp;

clrscr();

printf("\n enter the max no.of elements u wanna sort \n");

scanf("%d", &no);

printf("\n enter the elements u want to sort \n");

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

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

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

39
{

c = i;

do

root = (c - 1) / 2;

if (heap[root] < heap[c])

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

c = root;

} while (c != 0);

for (j = no - 1; j >= 0; j--)

temp = heap[0];

heap[0] = heap[j];

heap[j] = temp;

root = 0;

do

c = 2 * root + 1;

if ((heap[c] < heap[c + 1]) && c < j-1)

40
c++;

if (heap[root]<heap[c] && c<j)

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

root = c;

} while (c < j);

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

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

printf("\t %d", heap[i]);

getch();

41
OUTPUT

RESULT

Thus, the program is executed successfully and the output is verified

42
Ex. No : 10
BINARY SEARCH
Date:23-09-19

AIM

To write a program using C language to implement binary search technique

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Declare the variables

Step 4: Set the list to be the whole list

Step 5: Find the middle value of the list

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.

Step 9: Print the result

Step 10: Stop the program

CODING

#include <stdio.h>

void main()

int c, first, last, middle, n, search, array[100];

clrscr();

printf("Enter number of elements\n");

43
scanf("%d",&n);

printf("\nEnter %d integers\n", n);

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

scanf("%d",&array[c]);

printf("\nEnter value to find\n");

scanf("%d", &search);

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("\n%d found at location %d.\n", search, middle+1);

break;

else

last = middle - 1;

middle = (first + last)/2;

if (first > last)

printf("\nNot found! %d isn't present in the list.\n", search);

getch();

44
OUTPUT

RESULT

Thus, the program is executed successfully and the output is verified

45
Ex. No : 11
PROGRAM TO IMPLEMENT LINEAR SEARCH
Date:30-09-19

AIM

To write a program using C language to implement linear search technique

ALGORITHM

Step 1: Start the program

Step 2: Declare the header files

Step 3: Declare the variable

Step 4: Start with the first item in the list.

Step 5: Compare the current item to the target

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.

Step 8: Print the result

Step 9: Stop the program

CODING

#include<stdio.h>

void main()

int list[10],key,found,num,i;

clrscr();

printf("Enter no. of elements : ");

scanf("%d",&num);

printf("Enter %d elements\n",num);

46
for(i=0;i<num;i++)

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

printf("\n enter the value to be seached \n");

scanf("%d",&key);

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

if(key==list[i])

printf("\n %d element is found at location %d",list[i],i+1);

found=1;

if(found!=1)

printf("search is unsucessful");

getch();

47
OUTPUT

RESULT

Thus, the program is executed successfully and the output is verified

48

Potrebbero piacerti anche