Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Lab MANUAL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

NADAR SARASWATHI COLLEGE OF

ENGINEERING & TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

EC8381
FUNDAMENTALS OF DATA STRUCTURES IN C
LABORATARY

Prepared By
Dr.A.SOLAIRAJ, ASP/CSE

1
SYLLABUS

S. NO. NAME OF THE EXPERIMENT

1
Basic C Programs – looping, data manipulations, arrays

2
Programs using strings – string function implementation

3
Programs using structures and pointers

4
Programs involving dynamic memory allocations

5
Array implementation of stacks and queues

6
Linked list implementation of stacks and queues

7
Application of Stacks and Queues

8
Implementation of Trees, Tree Traversals

9
Implementation of Binary Search trees

10
Implementation of Linear search and binary search

11 Implementation Insertion sort, Bubble sort, Quick sort and Merge


Sort
12
Implementation Hash functions, collision resolution technique

2
EX.NO:1 Basic C Programs – looping, data manipulations, arrays
FIND GREATEST OR BIGGEST NUMBER

AIM

Develop a ‘C’ program to find the greatest of ’N’ numbers or biggest number stored in an array

PROGRAM

#include <stdio.h>
int main()
{
int i, n;
int arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
printf("Enter number%d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 1; i < n; ++i)
{
if (arr[0] < arr[i])
arr[0] = arr[i];
}

printf("Largest element = %d", arr[0]);

return 0;
}
SAMPLE OUTPUT

Enter the number of elements (1 to 100): 3


Enter number1: 34
Enter number2: 12
Enter number3: 67
Largest element = 67

3
CALCULATOR USING SWITCH CASE

AIM

Develop a ‘C’ program to implement a simple calculator using switch case statement

PROGRAM

#include <stdio.h>
int main()
{
char operator;
int first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%d %d", &first, &second);
switch (operator)
{
case '+':
printf("%d + %d = %d", first, second, first + second);
break;
case '-':
printf("%d + %d = %d", first, second, first - second);
break;
case '*':
printf("%d + %d = %d", first, second, first * second);
break;
case '/':
printf("%d + %d = %d", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
SAMPLE OUTPUT
Enter an operator (+, -, *,): +
Enter two operands: 2
6
2+6=8

4
SUM OF DIGITS USING WHILE

AIM

Develop a ‘C’ program to find the sum of the digits of a given number using while statement

PROGRAM

#include<conio.h>
void main()
{
intn,digit, n1,sum=0;
clrscr();
printf("Enter the Any Integer No: ");
scanf("%d",&n);
n1=n;
while(n!=0)
{
digit = n%10;
sum+= digit;
n/=10;
}
printf("number is = %d \n",n1);
printf("Sum of digit = %d ",sum);
getch();
}

SAMPLE OUTPUT
Enter the Any Integer No: 234
number is = 234
Sum of digit = 9

PROGRAM FOR FACTORIAL

AIM

Develop a ‘C’ program using function to compute the factorial of a given number

PROGRAM

#include<stdio.h>
int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %d", n, multiplyNumbers(n));
5
return 0;
}

int multiplyNumbers(int n)
{
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
SAMPLE OUTPUT

Enter a positive integer: 6

Factorial of 6 = 720

PROGRAM FOR CALL BY VALUE

AIM

Write a C Program to swap two numbers using call by value

PROGRAM

#include<stdio.h>
int main()
{
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, firstNumber = %d\n", first);
printf("After swapping, secondNumber = %d", second);
return 0;
}

SAMPLE OUTPUT

Enter first number: 12


Enter second number: 46
After swapping, firstNumber = 46
After swapping, secondNumber = 12

6
PROGRAM FOR ASCENDING ORDER

AIM
Write a program in C to sort the numbers in ascending order

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,t,a[10];
clrscr();
printf("enter the n values\n");
scanf("%d",&n);
printf("enter the %d number\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if (a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
printf("the ascending order is \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
SAMPLE OUTPUT
enter the max values limit
5
enter the 5 number
25634
the ascending order is
23456

7
ADDITION OF TWO MATRICES

AIM
Write a program in C to find the addition of two matrices

PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],sum[5][5],i,j,r,c;
clrscr();
printf("ENTER THE ROW SIZE\n");
scanf("%d",&r);
printf("ENTER THE COLOUMN SIZE\n");
scanf("%d",&c);
printf("ENTER THE VLAUES FOR %d X %d FIRST MATRIX \n",r,c);
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
scanf("%d",&a[i][j]);
}
printf("ENTER THE VLAUES FOR %d X %d SECOND MATRIX \n",r,c);
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
scanf("%d",&b[i][j]);
}
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
sum[i][j]=a[i][j]+b[i][j];
}
printf("THE RESULTANT %d X %d MATRIX ADDITION IS\n\n",r,c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%d\t",sum[i][j]);
}
printf("\n\n");
}
8
getch();
}
SAMPLE OUTPUT

ENTER THE ROW SIZE


2
ENTER THE COLOUMN SIZE
2
ENTER THE VLAUES FOR 2 X 2 FIRST MATRIX
1234
ENTER THE VLAUES FOR 2 X 2 SECOND MATRIX
1234
THE RESULTANT 2 X 2 MATRIX ADDITION IS

2 4

6 8
RESULT

Thus the C program for basic concepts isexecuted successfully and the output is verified.

9
EX.NO:2 Programs using strings – string function implementation

STRING PALINDROME

AIM

Write a program in C to check whether the given string is Palindrome or not

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[15],b[15];
clrscr();
printf("enter the string\n");
scanf("%s",&a);
strcpy(b,a);
strrev(a);
if(strcmp(a,b)==0)
{
printf("the given string is palindrome\n");
}
else
{
printf("the given string is not a palindrome\n");
}
getch();
}
SAMPLE OUTPUT

enter the string


adam
the given string is not a palindrome
RESULT

Thus the C program for string manipulation is executed successfully and the output is verified.

10
EX.NO:3 Programs using structures and pointers

ADD TWO COMPLEX NUMBERS

AIM

Write a program in C to add two complex numbers using structures

PROGRAM
#include<stdio.h>
struct complex
{
float real;
float imaginary;
};
int main()
{
struct complex cnum1, cnum2, sum;
printf("Enter real and imaginary part of first complex number:\n");
scanf("%f%f", &cnum1.real, &cnum1.imaginary);
printf("Enter real and imaginary part of second complex number:\n");
scanf("%f%f", &cnum2.real, &cnum2.imaginary);
sum.real = cnum1.real + cnum2.real;
sum.imaginary = cnum1.imaginary + cnum2.imaginary;

printf("SUM = %0.2f + i %0.2f", sum.real, sum.imaginary);

return 0;
}
SAMPLE OUTPUT

Enter real and imaginary part of first complex number:


2
4
Enter real and imaginary part of second complex number:
3
5
SUM = 5.00 + i 9.00

11
CALL BY REFERENCE
AIM
Write a program in C to swap two numbers using call by reference
PROGRAM
#include <stdio.h>
void swap(int *n1, int *n2);
int main()
{
int num1,num2;
clrscr();
printf("Enter the first number\n");
scanf("%d",&num1);
printf("Enter the second number\n");
scanf("%d",&num2);
printf("\n\nTHE NUMBERS LOCATION BEFORE SWAPPING\n\n");
printf("Number1 = %d\n", num1);
printf("Number2 = %d", num2);
swap(&num1, &num2);
printf("\n\nTHE NUMBERS LOCATION AFTER SWAPPING\n\n");
printf("Number1 = %d\n", num1);
printf("Number2 = %d", num2);
getch();
return 0;
}
void swap(int * n1, int * n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
SAMPLE OUTPUT
Enter the first number
25
Enter the second number
75
THE NUMBERS LOCATION BEFORE SWAPPING
Number1 = 25
Number2 = 75
THE NUMBERS LOCATION AFTER SWAPPING
Number1 = 75
Number2 = 25
RESULT
Thus the C program for structures and pointers are executed successfully and the output is
verified.

12
EX.NO:4 Programs involving dynamic memory allocations
FIND THE LARGEST NUMBER

AIM

Write a program in C to find the largest number using dynamic memory allocation.

PROGRAM

#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);
data = (float *)calloc(num, sizeof(float));
if (data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < num; ++i)
{
printf("Enter Number %d: ", i + 1);
scanf("%d", data + i);
}
for (int i = 1; i < num; ++i) {
if (*data < *(data + i))
*data = *(data + i);
}
printf("Largest number = %d", *data);
return 0;
}
SAMPLE OUTPUT

Enter the Total Number of Elements: 4


Enter Number 1: 34
Enter Number 2: 12
Enter Number 3: 56
Enter Number 4: 9
Largest Number = 56

13
FIND THE SMALLEST NUMBER

AIM

Write a program in C to find the smallest number using dynamic memory allocation

PROGRAM

#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
int *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);
data = (float *)calloc(num, sizeof(float));
if (data == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
for (int i = 0; i < num; ++i)
{
printf("Enter Number %d: ", i + 1);
scanf("%d", data + i);
}
for (int i = 1; i < num; ++i) {
if (*data > *(data + i))
*data = *(data + i);
}
printf("Smallest number = %d", *data);
return 0;
}
SAMPLE OUTPUT

Enter the total number of elements: 3


Enter Number 1: 34
Enter Number 2: 12
Enter Number 3: 56
Smallest number = 12

14
SUM OF ELEMENTS

AIM

Write a C program to read a one dimensional array, print sum of all elements along with input as array
elements using Dynamic Memory Allocation.

PROGRAM
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num, i, *ptr, sum = 0;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: \n");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
getch();
}

SAMPLE OUTPUT

Enter number of elements: 5


Enter elements of array:
12345
Sum = 15
RESULT

Thus the dynamic memory allocation is successfully executed and the output is verified

15
EX.NO:5 Array implementation of stacks and queues

ARRAY IMPLEMENTATION OF STACK

AIM

To write a C Program to implement array of stack

PROGRAM FOR STACK

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int Data;
struct Node *next;
}
*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
16
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main()//int argc, char *argv[])
{
int i=0;
top=NULL;
clrscr();
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Stack: ");
scanf("%d",&value); push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
17
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}

SAMPLE OUTPUT

Enter the Size of STACK [MAX=100]:4

STACK OPERATION USING ARRAY


----------------------------------------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter the Choice: 1


Enter a value to be pushed: 23

Enter the Choice: 1


Enter a value to be pushed: 12

Enter the Choice: 1


Enter a value to be pushed: 45

Enter the Choice: 3


The element in STACK
45
12
23
18
Press Next Choice:
Enter the Choice: 2
The popped element is 45

Enter the Choice: 3


The element in STACK
12
23
Enter the Choice: 4

ARRAY IMPLEMENTATION OF QUEUE

AIM
Develop a ‘C’ program for implementing a Queue using arrays. Illustrate the operations that can
be performed on the same.

PROGRAM
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
}
case 2:
{
if(front==rear)
19
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
}
case 3:
{
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
}
case 4:
exit(0);
default:
{
printf("Wrong Choice: please see the options");
}
}
}
}
getch();
}
SAMPLE OUTPUT

Queue using Array


1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:5
Enter the Choice:1
Enter no 2:8
Enter the Choice:1
Enter no 3:4
20
Enter the Choice:3
Queue Elements are:
5
8
4
Enter the Choice:4

21
EX.NO:6 Linked list implementation of stacks and queues
LINKED LIST IMPLEMENTATION OF STACK

AIM

Write a program in C to implement a stack using linked list.

PROGRAM

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int Data;
struct Node *next;
}
*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{

22
temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main()//int argc, char *argv[])
{
int i=0;
top=NULL;
clrscr();
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a value to push into Stack: ");
scanf("%d",&value); push(value);
display();
break;
23
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
SAMPLE OUTPUT

1. Push to stack
2. Pop from Stack
3. Display data of Stack
4. Exit

Choose Option: 1
Enter a value to push into Stack: 5
Elements are as:
5

Choose Option: 1
Enter a value to push into Stack: 3
24
Elements are as:
3
5

Choose Option: 1
Enter a value to push into Stack: 2
Elements are as:
2
3
5
Choose Option: 3
Elements are as:
2
3
5

Choose Option: 2
Elements are as:
3
5
Choose Option: 4

LINKED LIST IMPLEMENTATION OF QUEUE

AIM

Write a program in C to implement a Queue using linked list.

PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}
*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
25
}
else
printf("\nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements are as: ");
while(var!=NULL)
{
printf("\t%d",var->Data); var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty");
}
int main()
{
int i=0;
front=NULL;
clrscr();
26
printf(" \n1. Push to Queue");
printf(" \n2. Pop from Queue");
printf(" \n3. Display Data of Queue");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a value to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
SAMPLE OUTPUT
27
1. Push to Queue
2. Pop from Queue
3. Display Data of Queue
4. Exit

Choose Option: 1
Enter a value to push into Queue: 5
Elements are as: 5

Choose Option: 1
Enter a value to push into Queue: 3
Elements are as: 5 3

Choose Option: 1
Enter a value to push into Queue: 2
Elements are as: 5 3 2

Choose Option: 2
Elements are as: 3 2

Choose Option: 4

RESULT

Thus the C program for linked list implementation of stack and queue is executed successfully
and the output is verified.

28
EX.NO:7 APPLICATIONS OF STACK AND QUEUE

EVALUATE INFIX EXPRESSION


AIM
Write a program in C to evaluate infix expression using stack
PROGRAM
#include<stdio.h>
char expr[20];
char stack[20];
int precedence(char a,char b)
{
if(((a=='+')||(a=='-'))&&((b=='*')||(b=='/')))
return 0;
else
return 1;
}
int i;
int ctr;
int top=-1;
int topOper=-1;
int operate(int a,int b,char oper)
{
int res=0;
switch(oper)
{
case '+':res=a+b;break;
case '-':res=a-b;break;
case '*':res=a*b;break;
case '/':res=a/b;break;
}
return res;
}
void postfixConvert()
{
char topsymb,operatorStack[20];
ctr=0;
while(expr[ctr]!='\0')
{
if(expr[ctr]>='0'&&expr[ctr]<='9')
stack[++top]=expr[ctr];
else
{
while(topOper>=0&&precedence(operatorStack[topOper],expr[ctr]))
{
topsymb=operatorStack[topOper--];
29
stack[++top]=topsymb;
}
operatorStack[++topOper]=expr[ctr];
}
ctr++;
}
while(topOper>=0)
stack[++top]=operatorStack[topOper--];
printf("The Resulting Postfix expression for the given infix expression\n%s\n",stack);
}
int main()
{
printf("\t\tExpression Evaluator\n");
printf("This Program Evaluates Basic Expressions(without brackets) with arithmetic
operations(+,-,*,/) on single digit operand length below 20\n");
printf("Enter the Expression\n");
scanf("%s",expr);
postfixConvert();
char oper;
int operand1,operand2;
ctr=0;
int result[2];
int rTop=-1;
while(stack[ctr]!='\0')
{
oper=stack[ctr];
if(oper>='0'&&oper<='9')
result[++rTop]=(int)(oper-'0');
else
{
operand1=result[rTop--];
operand2=result[rTop--];
result[++rTop]=operate(operand2,operand1,oper);
}
ctr++;
}
printf("The result of the expression is\n%d\n",result[0]);
}
SAMPLE OUTPUT
Expression Evaluator
This Program Evaluates Basic Expressions (without brackets) with arithmetic operations (+,-,*,/)
on single digit operand length below 20
Enter the Expression
2+3*4
The Resulting Postfix expression for the given infix expression
234*+
The result of the expression is
14
30
EVALUATE POSTFIX EXPRESSION
AIM
Develop a ‘C’ program for evaluating postfix expression using array implementation of a stack.

PROGRAM

#include<stdio.h>
#include<conio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
void main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
clrscr();
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 '-':
{
31
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());
getch();
}

SAMPLE OUTPUT
Enter the expression: 2 3 4 + * 5 *
The result of expression 2 3 4 + * 5 * = 70
Enter the expression: 4 5 + 7 2 - *
The result of expression 4 5 + 7 2 - * = 45

32
EX.NO:8 IMPLEMENTATION OF TREES, TREE TRAVERSALS

INORDER TRAVERSAL

AIM

Write a program in C for in-order traversal of a Tree

PROGRAM

#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
struct node* newNode(int data)
{
struct node* node = (struct node*) malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}
void printInorder(struct node* node)
{
if (node == NULL)
return;
printInorder(node->left);
printf("%d ", node->data);
printInorder(node->right);
}
int main()
{
struct node *root = newNode(5);
root->left = newNode(2);
root->right = newNode(4);
root->left->left = newNode(3);
root->left->right = newNode(6);
root->right->left = newNode(8);
root->right->right= newNode(10);
clrscr();
33
printf("\nInorder traversal of binary tree is \n");
printInorder(root);

getch();
return 0;
}
SAMPLE OUTPUT

Inorder traversal of binary tree is


3 2 6 5 8 4 10

34
EX.NO:9 IMPLEMENTATION OF BINARY SEARCH TREES

AIM

Write a Program to implement Binary Search Tree

PROGRAM

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct treeNode
{
int data;
struct treeNode *left;
struct treeNode *right;

}treeNode;

treeNode* FindMin(treeNode *node)


{
if(node==NULL)
{

return NULL;
}
if(node->left)
return FindMin(node->left);
else
return node;
}
treeNode * Insert(treeNode *node,int data)
{
if(node==NULL)
{
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
temp -> data = data;
temp -> left = temp -> right = NULL;
return temp;
}

if(data >(node->data))
{
node->right = Insert(node->right,data);
}
35
else if(data < (node->data))
{
node->left = Insert(node->left,data);
}
return node;

treeNode * Delete(treeNode *node, int data)


{
treeNode *temp;
if(node==NULL)
{
printf("Element Not Found");
}
else if(data < node->data)
{
node->left = Delete(node->left, data);
}
else if(data > node->data)
{
node->right = Delete(node->right, data);
}
else
{
if(node->right && node->left)
{
temp = FindMin(node->right);
node -> data = temp->data;
node -> right = Delete(node->right,temp->data);
}
else
{
temp = node;
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp);
}
}
return node;

void PrintInorder(treeNode *node)


{
if(node==NULL)
36
{
return;
}
PrintInorder(node->left);
printf("%d-> ",node->data);
PrintInorder(node->right);
}

void main()
{

treeNode *root = NULL;


clrscr();
root = Insert(root, 5);
root = Insert(root, -1);
root = Insert(root, 3);
root = Insert(root, -14);
root = Insert(root, 8);
root = Insert(root, 10);
root = Insert(root, 9);
root = Insert(root, 6);
printf("\n Inorder value after insertion:\n");
PrintInorder(root);
printf("\n");
root = Delete(root,5);
root = Delete(root,-1);
printf("\nValue after deletion:\n");
PrintInorder(root);
printf("\n");
getch();
}

SAMPLE OUTPUT

Inorder value after insertion:

-14->-1->3->5->6->8->9->10->
Value after deletion:
-14->3->6->8->9->10->

37
IMPLEMENTATION OF LINEAR SEARCH
EX.NO:10
AND BINARY SEARCH

LINEAR SEARCH

AIM

Develop a ‘C’ program for implementing linear search on a sorted array of ‘N’ numbers

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,x,n;
clrscr();
printf("How many elements?");
scanf("%d",&n);
printf("Enter array elements:\n");
for(i=1;i<=n;++i)
scanf("%d",&a[i]);
printf("\nEnter element to search:");
scanf("%d",&x);
for(i=1;i<=n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
getch ();
}
SAMPLE OUTPUT

How many elements? 4


Enter array elements:
12
34
67
10
Enter element to search: 34
Element found at index 2

38
BINARY SEARCH

AIM

Develop a program to implement Binary search

PROGRAM

#include<stdio.h>

#include<conio.h>
void main()
{
int arr[50],i,n,x,flag=0,first,last,mid;
clrscr();
printf("Enter size of array:");
scanf("%d",&n);
printf("\nEnter array element(ascending order)\n");

for(i=0;i<n;++i)
{
scanf("%d",&arr[i]);
}
printf("\nEnter the element to search:");
scanf("%d",&x);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;

if(x==arr[mid]){
flag=1;
break;
}
else
if(x>arr[mid])
first=mid+1;
else
last=mid-1;
}
if(flag==1)
printf("\nElement found at position %d",mid+1);
else
printf("\nElement not found");

getch();
}
39
SAMPLE OUTPUT

Enter size of array: 4

Enter array element (ascending order)


12
23
45
67
Enter the element to search: 45
Element found at position 3

40
EX.NO:11 IMPLEMENTATION OF SORTING

INSERTION SORT

AIM

Develop a ‘C’ program to sort an array of ‘N’ numbers using insertion sort.

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,temp,a[30];
clrscr();
printf("Enter the max number of elements: ");
scanf("%d",&n);
printf("\nEnter the %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("\nSorted list is as follows\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}
SAMPLE OUTPUT

Enter the max number of elements: 4


Enter the 4 elements
12
45
41
23
9

Sorted list is as follows


9
12
23
45

BUBBLE SORT
AIM
Develop a ‘C’ program to sort an array of ‘N’ numbers using bubble sort

PROGRAM

#include<stdio.h>
#include<conio.h>
void bubble_sort(int [], int);
void main()
{
int array[30], n, c, d, swap;
clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
bubble_sort(array, n);
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();
}
void bubble_sort(int list[], int n)
{
int c, d, t;
for (c = 0 ; c < n - 1; c++)
{ for (d = 0 ; d < n - c - 1; d++)
{ if (list[d] > list[d+1])
{ t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
SAMPLE OUTPUT

42
Enter the number of elements: 4
Enter 4 integers
34
12
67
8
Sorted list in ascending order:
8
12
34
67

QUICK SORT
AIM
Develop a ‘C’ program to sort an array of ‘N’ numbers using quick sort
PROGRAM
#include<stdio.h>
#include<conio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
void main()
{
int a[50],n,i;
clrscr();
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void quick_sort(int a[],int p,int u)
{
int j;
if(p<u)
{
j=partition(a,p,u);
quick_sort(a,p,j-1);
quick_sort(a,j+1,u);
}
}
int partition(int a[],int p,int u)
{
int v,i,j,temp;
43
v=a[p];
i=p;
j=u+1;
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
while(i<j);
a[p]=a[j];
a[j]=v;
return(j);
}
SAMPLE OUTPUT

How many elements? 5


Enter array elements:
12
56
3
27
89
Array after sorting:
3
12
27
56
89

MERGE SORT

AIM
Develop a ‘C’ program to sort an array of ‘N’ numbers using merge sort.

PROGRAM
#include<stdio.h>
#include<conio.h>
void mergesort(int a[],int i,int j);
44
void merge(int a[],int i1,int j1,int i2,int j2);
void main()
{
int a[30],n,i;
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
45
SAMPLE OUTPUT

Enter no of elements: 4
Enter array elements:
12
89
45
37
Sorted array is: 12 37 45 89

46
IMPLEMENTATION OF HASH FUNCTIONS AND COLLISION
EX.NO:12
RESOLUTION TECHNIQUE

IMPLEMENTATION OF HASH FUNCTIONS


#include<stdio.h>
#include<limits.h>
void insert(int ary[],int hFn, int size)
{
int element,pos,n=0;
printf("Enter key element to insert\n");
scanf("%d",&element);
pos = element%hFn;
while(ary[pos]!= INT_MIN)
{
if(ary[pos]== INT_MAX)
break;
pos = (pos+1)%hFn;
n++;
if(n==size)
break;
}
if(n==size)
printf("Hash table was full of elements\nNo Place to insert this element\n\n");
else
ary[pos] = element;
}
void delete(int ary[],int hFn,int size)
{
int element,n=0,pos;
printf("Enter element to delete\n");
scanf("%d",&element);
pos = element%hFn;
while(n++ != size){
if(ary[pos]==INT_MIN){
printf("Element not found in hash table\n");
break;
}
else if(ary[pos]==element)
{
ary[pos]=INT_MAX;
printf("Element deleted\n\n");
break;
}
else
{
pos = (pos+1) % hFn;
47
}
}
if(--n==size)
printf("Element not found in hash table\n");
}

void search(int ary[],int hFn,int size)


{
int element,pos,n=0;
printf("Enter element you want to search\n");
scanf("%d",&element);
pos = element%hFn;
while(n++ != size){
if(ary[pos]==element){
printf("Element found at index %d\n",pos);
break;
}
else
if(ary[pos]==INT_MAX ||ary[pos]!=INT_MIN)
pos = (pos+1) %hFn;
}
if(--n==size) printf("Element not found in hash table\n");
}
void display(int ary[],int size){
int i;
printf("Index\tValue\n");
for(i=0;i<size;i++)
printf("%d\t%d\n",i,ary[i]);
}
int main()
{
int ary[100],size,hFn,i,choice;
clrscr();
printf("Enter size of hash table\n");
scanf("%d",&size);
printf("Enter hash function [if mod 10 enter 10]\n");
scanf("%d",&hFn);
for(i=0;i<size;i++)
ary[i]=INT_MIN;
printf(" 1-> Insert\n 2-> Delete\n 3-> Display\n 4-> Searching\n 5-> Exit\n");
do{
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert(ary,hFn,size);
break;
48
case 2:
delete(ary,hFn,size);
break;
case 3:
display(ary,size);
break;
case 4:
search(ary,hFn,size);
break;
case 5:
exit(0);
default:
printf("Enter correct choice\n");
break;
}
}
while(choice);
getch();
return 0;
}
SAMPLE OUTPUT

Enter size of hash table: 4

Enter hash function [if mod 10 enter 10]


10

1-> Insert
2-> Delete
3-> Display
4-> Searching
5-> Exit

Enter your choice: 1


Enter key element to insert: 10

Enter your choice: 1


Enter key element to insert: 21

Enter your choice: 1


Enter key element to insert: 32

Enter your choice: 3


Index Value
0 10
1 21
2 32
3 -32768
49
Enter your choice: 2
Enter element to delete: 10
Element Deleted

Enter your choice: 3


Index Value
0 32767
1 21
2 32
3 -32768

Enter your choice: 4


Enter element to delete: 21
Element found at index 1

Implementation of Collision Resolution Technique Linear Probing


#include <stdio.h>
#include <conio.h>
int tsize;

int hasht(int key)


{
int i ;
i = key%tsize ;
return i;
}

//-------LINEAR PROBING-------
int rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}
void main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
clrscr() ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);

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


scanf ("%d",&n);

for (i=0;i<tsize;i++)
hash[i]=-1 ;

50
printf ("Enter Elements: ");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

for (i=0;i<tsize;i++)
hash[i]=-1 ;

for(k=0;k<n;k++)
{
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashl(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}

getch() ;
}

SAMPLE OUTPUT

Enter the size of the hash table: 10

Enter the number of elements: 8


Enter Elements: 72 27 36 24 63 81 92 101

The elements in the array are:


Element at position 0: -1
Element at position 1: 81
Element at position 2: 72
Element at position 3: 63
Element at position 4: 24
Element at position 5: 92
Element at position 6: 36
Element at position 7: 27
Element at position 8: 101
Element at position 9: -1

51

You might also like