0% found this document useful (0 votes)
26 views95 pages

DS Rec Final

Sri Manakula Vinayagar Engineering College for Data Structure given to IT DPT students

Uploaded by

btechit240937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views95 pages

DS Rec Final

Sri Manakula Vinayagar Engineering College for Data Structure given to IT DPT students

Uploaded by

btechit240937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

Ex. No. Title of the experiment

1 a) IMPLEMENTATION OF NON RECURSIVE LINEAR SEARCH

1 b) IMPLEMENTATION OF NON RECURSIVE BINARY SEARCH

1 c) IMPLEMENTATION OF RECURSIVE LINEAR SEARCH

1d) IMPLEMENTATION OF RECURSIVE BINARY SEARCH

2 a) IMPLEMENTATION OF BUBBLE SORT

2 b) IMPLEMENTATION OF SELECTION SORT

2 c) IMPLEMENTATION OF INSERTION SORT

2 d) IMPLEMENTATION OF SHELL SORT

2 e) IMPLEMENTATION OF HEAP SORT

3 a) ARRAY IMPLEMENTATION OF STACK ADT

3 b) ARRAY IMPLEMENTATION OF QUEUE ADT

4 IMPLEMENTATION OF LIST ADT

5 a) SINGLY LINKED LIST IMPLEMENTATION OF STACK ADT

5 b) SINGLY LINKED LIST IMPLEMENTATION OF QUEUE ADT

6 a) IMPLEMENTATION OF DOUBLY LINKED LIST

6 b) ARRAY IMPLEMENTATION OF DOUBLE ENDED QUEUE ADT

7 BINARY SEARCH TREE OPERATIONS

8 BINARY TREE TRAVERSAL

9 AVL TREE OPERATIONS

10 a) GRAPH TRAVERSAL-DEPTH FIRST SEARCH

10 b) GRAPH TRAVERSAL-BREADTH FIRST SEARCH

11 a) SET OPERATIONS - UNION

11 b) SET OPERATIONS – INTERSECTION

11 c) SET OPERATIONS - DIFFERENCE

12 EVALUATION OF POLYNOMIALS USING LINKED LIST


EX. NO 1 a) IMPLEMENTATION OF NON RECURSIVE
LINEAR SEARCH

AIM
To Write a C Program to implement non-recursive linear search.

ALGORITHM
Algorithm linearsearch(a,n)
{
read key;
for(i=0 to n)
{
if (a[i]==key)
{
write"key exist in the i th position";
}
}
write"key does not exist"
}
PROGRAM

#include<stdio.h>
int linearsearch(int a[],int n,int key)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==key)
{
return i;
}
}
return -1;
}
int main()
{
int a[100],n,key,pos,i;
printf("enter n value");
scanf("%d",&n);
printf("enter %d elements into array",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter search key elements");
scanf("%d",&key);
pos=linearsearch(a,n,key);
if(pos>=0)
printf("element found at %d position",pos+1);
else
printf("element not found");
return 1;
}

OUTPUT

enter n value:6
enter 6 elements into array:14 24 34 74 94 164
enter search key elements:74
element found at 4 position

RESULT
Thus the program for non-recursive linear search is executed and verified successfully.
EX. NO: 1 b) IMPLEMENTATION OF RECURSIVE

LINEAR SEARCH

AIM
To Write a C Program to implement recursive linear search.

ALGORITHM
Algorithm linearsearch(a,n)
{
read key;
for(i=0 to n)
{
if (a[i]==key)
{
write"key exist in the i th position";
}
}
write"key does not exist"
}
PROGRAM
#include <stdio.h>
int linear_search_with_recursion(int arr[], int value, int index, int n)
{
int position = 0;
if(index >= n)
{
return 0;
}
else if (arr[index] == value)
{
position = index + 1;
return position;
}
else
{
return linear_search_with_recursion(arr, value, index+1, n);
}
return position;
}
int main()
{
int n, value, position, m = 0, arr[100];
printf("Enter the total elements in the array that you would like to take:");
scanf("%d", &n);
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element you would like to search in the array: ");
scanf("%d", &value);
position = linear_search_with_recursion(arr, value, 0, n);
if (position != 0)
{
printf("Element found at position %d ", position);
}
else
{
printf("Element not found in the array");
}
return 0;
}

OUTPUT

Enter the total elements in the array that you would like to take:5
Enter the elements of the array:
10 20 30 40 50
Enter the element you would like to search in the array: 40
Element found at position 4

RESULT
Thus the program for recursive linear search is executed and verified successfully.
EX. NO: 1 c) IMPLEMENTATION OF NON RECURSIVE

BINARY SEARCH

AIM
To Write a C Program to implement non-recursive Binary search.

ALGORITHM

Algorithm binarysearch(a,n)
{
read key;
mid = (low+high)/2;
while (low<=high)
{
if(key==a[mid])
{
Write "key exist in mid position";
}
else if(key<a[mid])
{
high = mid – 1;
}
else
{
Low = mid – 1;
}
}
PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20], i, n, key, low, high, mid;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter the array elements in ascending order:");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the key element:\n");
scanf("%d", &key);
low = 0;
high = n - 1;
while(high >= low)
{
mid = (low + high) / 2;
if(key == a[mid])
break;
else
{
if(key > a[mid])
low = mid + 1;
else
high = mid - 1;
}
}
if(key == a[mid])
printf("The key element is found at location %d", mid + 1);
else
printf("The key element is not found");
}
OUTPUT

Enter number of elements:5


Enter the array elements in ascending order:11 12 13 14 15
Enter the key element:
13
The key element is found at location 3

RESULT

Thus the program for non-recursive binary search is executed and verified successfully.
EX. NO: 1 d) IMPLEMENTATION OF RECURSIVE BINARY SEARCH

AIM
To Write a C Program to implement recursive Binary search.

ALGORITHM

Algorithm binarysearch(a,n)
{
read key;
mid = (low+high)/2;
while (low<=high)
{
if(key==a[mid])
{
Write "key exist in mid position";
}
else if(key<a[mid])
{
high = mid – 1;
}
else
{
Low = mid – 1;
}
}
PROGRAM

#include<stdio.h>
#include<stdlib.h>
#define size 10
int binsearch(int[], int, int, int);
int main() {
int num, i, key, position;
int low, high, list[size];
printf("Enter the total number of elements:");
scanf("%d", &num);
printf("Enter the elements of list :");
for (i = 0; i < num; i++) {
scanf("%d", &list[i]);
}
low = 0;
high = num - 1;
printf("Enter element to be searched : ");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf("Number present at %d", (position + 1));
} else
printf("The number is not present in the list");
return (0);
}
// Binary search function for binary search
int binsearch(int a[], int x, int low, int high) {
int mid;

if (low > high)


return -1;
mid = (low + high) / 2;

if (x == a[mid]) {
return (mid);
} else if (x < a[mid]) {
binsearch(a, x, low, mid - 1);
} else {
binsearch(a, x, mid + 1, high);
}
}

OUTPUT

Enter the total number of elements:4


Enter the elements of list :15 35 55 75
Enter element to be searched : 35
Number present at 2

RESULT

Thus the program for recursive binary search is executed and verified successfully.
EX. NO: 2 a)
IMPLEMENTATION OF BUBBLE SORT

AIM

To Write a C Program to sort the given set of elements using Bubble Sort.

ALGORITHM

Algorithm bubblesort(a,n)
{
for(i=1 to n) do
{
for(j=1 to n) do
{
if(a[j]>a[j+1]) then
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
PROGRAM
#include <stdio.h>
int main(){
int arr[50], num, x, y, temp;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
printf("Please Enter the Value of Elements: ");
for(x = 0; x < num; x++)
scanf("%d", &arr[x]);
for(x = 0; x < num - 1; x++){
for(y = 0; y < num - x - 1; y++){
if(arr[y] > arr[y + 1]){
temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
printf("Array after implementing bubble sort: ");
for(x = 0; x < num; x++){
printf("%d ", arr[x]);
}
return 0;
}

OUTPUT

Please Enter the Number of Elements you want in the array: 5

Please Enter the Value of Elements: 94 169 74 12 56

Array after implementing bubble sort: 12 56 74 94 169

RESULT

Thus the program for sorting the elements using bubble sort is executed and verified successfully.
EX. NO: 2 b)
IMPLEMENTATION OF SELECTION SORT

AIM

To Write a C Program to sort the given set of elements using Selection Sort.

ALGORITHM
Algorithm selectionsort(a,n)
{
for(i=1 to n) do
{
min=i;
for(j=i+1 to n) do
{
if(a[j]<a[min]) then
{
min=j;
}
}
if(min!=i) then
{
t=a[i];
a[i]=a[min];
a[min]=t;
}
}
}
PROGRAM

#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elements:");
scanf("%d", &n);
printf("Enter %d Numbers:", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("Sorted Array:");
for(i = 0; i < n; i++)
printf("%d\t", a[i]);
return 0;
}

OUTPUT

Enter number of elements:5

Enter 5 Numbers:25 45 15 05 95

Sorted Array:5 15 25 45 95

RESULT
Thus the program for sorting the elements using selection sort is executed and verified successfully.
EX. NO: 2 c)
IMPLEMENTATION OF INSERTION SORT

AIM

To Write a C Program to sort the given set of elements using Insertion Sort.

ALGORITHM
Algorithm selectionsort(a,n)
{
for(i=1 to n) do
{
min=i;
for(j=i+1 to n) do
{
if(a[j]<a[min]) then
{
min=j;
}
}
if(min!=i) then
{
t=a[i];
a[i]=a[min];
a[min]=t;
}
}
}
PROGRAM

#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{
j = i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}

OUTPUT

Enter number of elements:5


Enter 5 integers:
12 32 98 65 61 78
Sorted list in ascending order:
12
32
61
65
98

RESULT

Thus the program for sorting the elements using insertion sort is executed and verified successfully.
EX. NO: 2 d)
IMPLEMENTATION OF SHELL SORT

AIM

To Write a C Program to sort the given set of elements using Shell Sort.

ALGORITHM

Algorithm Shellsort(ElementTypeA[ ], int N)


{
int I, j, increment;
ElementType Temp;
For(increment=N/2;increment>0;increment++)

For(i=increment; i<N; i++)


{
Temp=A[i];
For(j=I;j>=increment;j=increment)
If(Temp<A[j-increment])
A[j]=A[j-increment];
Else
Break;
A[j]=Temp;
}
}
PROGRAM

#include<stdio.h>
void ShellSort(int a[], int n)
{
int i, j, increment, tmp;
for(increment = n/2; increment > 0; increment /= 2)
{
for(i = increment; i < n; i++)
{
tmp = a[i];
for(j = i; j >= increment; j -= increment)
{
if(tmp < a[j-increment])
a[j] = a[j-increment];
else
break;
}
a[j] = tmp;
}
}
}
int main()
{
int i, n, a[10];
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
ShellSort(a,n);
printf("The sorted elements are : ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

OUTPUT

Enter the number of elements : 6


Enter the elements : 33 44 22 55 11 66
The sorted elements are : 11 22 33 44 55 66

RESULT

Thus the program for sorting the elements using shell sort is executed and verified successfully.
EX. NO: 2 e)
IMPLEMENTATION OF HEAP SORT

AIM

To Write a C Program to sort the given set of elements using Heap Sort.

ALGORITHM
● An array A with N elements is given.
● This algorithm sorts the element of A.
1. [Build a heap A ,using a procedure 1]
Repeat for J=1 to N-1
Call INSHEAP(A, J, A[J+1])
2. [sort A by repeatedly deleting the root of H, using procedure 2]
Repeat while N>1:
Call DELHEAP(A , N,VALUE)
Set A[n+1]:=value
Exit
PROGRAM

#include <stdio.h>
int main()
{
int arr[10], no, i, j, c, heap_root, temp;
printf("Input number of elements: ");
scanf("%d", &no);
printf("Input array values one by one : ");
for (i = 0; i < no; i++)
scanf("%d", &arr[i]);
for (i = 1; i < no; i++)
{
c = i;
do
{
heap_root = (c - 1) / 2;
/* to create MAX arr array */
if (arr[heap_root] < arr[c])
{
temp = arr[heap_root];
arr[heap_root] = arr[c];
arr[c] = temp;
}
c = heap_root;
} while (c != 0);
}
printf("Heap array : ");
for (i = 0; i < no; i++)
printf("%d\t ", arr[i]);
for (j = no - 1; j >= 0; j--)
{
temp = arr[0];
arr[0] = arr[j];
arr[j] = temp;
heap_root = 0;
do
{
c = 2 * heap_root + 1;
if ((arr[c] < arr[c + 1]) && c < j-1)
c++;
if (arr[heap_root]<arr[c] && c<j)
{
temp = arr[heap_root];
arr[heap_root] = arr[c];
arr[c] = temp;
}
heap_root = c;
} while (c < j);
}
printf("\nSorted array : ");
for (i = 0; i < no; i++)
printf("\t%d", arr[i]);
printf("\n");
}

OUTPUT

Input number of elements: 4


Input array values one by one : 200 400 300 100
Heap array : 400 200 300 100
Sorted array : 100 200 300 400

RESULT

Thus the program for sorting the elements using heap sort is executed and verified successfully.
EX. NO: 3 a)
ARRAY IMPLEMENTATION OF STACK ADT

AIM

To Write a C Program to implement the stack operations using array.

ALGORITHM

Push(value)
Step 1 - Check whether stack is FULL. (top == SIZE-1)
Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the function.
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value (stack[top] =
value).

Pop()
Step 1 - Check whether stack is EMPTY. (top == -1)
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).

Display()
Step 1 - Check whether stack is EMPTY. (top == -1)
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value and
decrement i value by one (i--).
Step 3 - Repeat above step until i value becomes '0'.
PROGRAM

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define SIZE 10
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value){
if(top == SIZE-1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop(){
if(top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display(){
if(top == -1)
printf("\nStack is Empty!!!");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}

OUTPUT

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 100
Insertion success!!!

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 200
Insertion success!!!

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 300
Insertion success!!!

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements are:
300
200
100

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Deleted : 300

***** MENU *****


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements are:
200
100

RESULT
Thus the program for implementation of stack operations using array is executed and verified
successfully.
EX. NO: 3 b)
ARRAY IMPLEMENTATION OF QUEUE ADT

AIM

To Write a C Program to implement the queue operations using array.

ALGORITHM

Enqueue(value)
Step 1 - Check whether queue is FULL. (rear == SIZE-1)
Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the function.
Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and set queue[rear] = value.

Dequeue()
Step 1 - Check whether queue is EMPTY. (front == rear)
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then increment the front value by one (front ++). Then display queue[front] as
deleted element. Then check whether both front and rear are equal (front == rear), if it TRUE, then set both
front and rear to '-1' (front = rear = -1).

Display()
Step 1 - Check whether queue is EMPTY. (front == rear)
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value reaches
to rear (i <= rear)
PROGRAM

#include<stdio.h>
#include<conio.h>
#define SIZE 10
void enQueue(int);
void deQueue();
void display();
int queue[SIZE], front = -1, rear = -1;
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}

OUTPUT

***** MENU *****


1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 15
Insertion success!!!

***** MENU *****


1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 25
Insertion success!!!

***** MENU *****


1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 35
Insertion success!!!

***** MENU *****


1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
Queue elements are:
15 25 35

***** MENU *****


1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 2
Deleted : 15
***** MENU *****
1. Insertion
2. Deletion
3. Display
4. Exit
Enter your choice: 3
Queue elements are:
25 35

RESULT
Thus the program for implementation of queue operations using array is executed and verified
successfully.
EX. NO: 4
IMPLEMENTATION OF LIST ADT

AIM

To Write a C Program to implement the list ADT operations.

ALGORITHM

Create()
1. Enter number of nodes(n) wanted to be inserted in the list.
2. Use the for loop, declare (i=0) and until (i<n) enter the numbers in the nodes of the list.
3. When (i=n) stop the process and display the elements.

Insert()

1. Get the position(pos) to be inserted in the list.


2. If the position is greater than size of the list then print “Invalid location”.
3. Otherwise declare (i=MAX-1), until (i>=pos-1) assign b[i+1]=b[i].
4. When (i=pos), enter the element to be inserted and increment the size of the list by one(n++).

Delete()

1. Get the position(pos) to be deleted in the list.


2. If the position is greater than size of the list then print “Invalid location”.
3. Otherwise declare (i=pos+1), until (i<n) assign b[i-1]=b[i].
4. When (i=pos), delete the element and decrement the size of the list by one(n--).
5. After deletion, display the elements one by one.

Search()

1. Enter the element to be searched(e) in the list.


2. Search the element in the list one by one linearly.
3. If the search element is found then assign flag = 1, or else keep the flag = 0 until the search element
is found.
4. If the flag =1, then print the search element found with its position in the list.
5. If the flag =0, then print the search element was not found in the list.

Display()

1. Print the “Element of the list ADT are:”.


2. Declare (i=0) until (i<n) traverse each node in the list one by one.
3. Display the elements of the each node immediately after traversing one by one until all the elements
in the list are displayed.
PROGRAM

#include<stdio.h>
#include <conio.h>
#include<stdlib.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos,flag=0;
void main()
{
//clrscr();
int ch;
char g='y';
do
{
printf("\n Main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}

void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
}

void deletion()
{
printf("\n Enter the position u want to delete:");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}
}

void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
flag=1;
}
else
{
flag=0;
continue;
}
if(flag==1)
{
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list::", e);
}
}
}

void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);

if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}

void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}
}
OUTPUT

Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit

Enter your Choice1


Enter the number of nodes3
Enter the Element:10
Enter the Element:20
Enter the Element:30
Do u want to continue:::y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit

Enter your Choice4


Enter the position u need to insert::2
Enter the element to insert::
25
The list after insertion::
The Elements of The list ADT are:
10
20
25
30
Do u want to continue:::y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit

Enter your Choice3


Enter the Element to be searched:30
Value is in the 3 Position
Do u want to continue:::y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit

Enter your Choice2


Enter the position u want to delete::1
The Elements after deletion 10 25 30

RESULT
Thus the program for implementation of list ADT operations is executed and verified successfully.
EX. NO: 5 a)
SINGLY LINKED LIST IMPLEMENTATION OF STACK ADT

AIM

To Write a C Program to implement the stack operations using singly linked list

ALGORITHM

Push(value)
Step 1 - Create a newNode with given value.
Step 2 - Check whether stack is Empty (top == NULL) Step
3 - If it is Empty, then set newNode → next = NULL. Step 4
- If it is Not Empty, then set newNode → next = top. Step 5
- Finally, set top = newNode.
Pop()
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.
Step 4 - Then set 'top = top → next'.
Step 5 - Finally, delete 'temp'. (free(temp)).
Display()
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty, then define a Node pointer 'temp' and initialize with top.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the
first node in the stack. (temp → next != NULL).
Step 5 - Finally! Display 'temp → data ---> NULL'.
PROGRAM
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
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("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
OUTPUT

:: Stack using Linked List ::


****** MENU ******
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 50
Insertion is Success!!!

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 60
Insertion is Success!!!

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 70
Insertion is Success!!!

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
70--->60--->50--->NULL

****** MENU ******


1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
Deleted element: 70

RESULT
Thus the program for implementation of stack operations using singly linked list is executed and
verified successfully.
EX. NO: 5 b)
SINGLY LINKED LIST IMPLEMENTATION OF QUEUE ADT

AIM

To Write a C Program to implement the queue operations using singly linked list

ALGORITHM

Enqueue(value)
Step 1 - Create a newNode with given value and set 'newNode → next' to NULL.
Step 2 - Check whether queue is Empty (rear == NULL)
Step 3 - If it is Empty then, set front = newNode and rear = newNode.
Step 4 - If it is Not Empty then, set rear → next = newNode and rear = newNode.
Dequeue()
Step 1 - Check whether queue is Empty (front == NULL).
Step 2 - If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from the
function
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
Step 4 - Then set 'front = front → next' and delete 'temp' (free(temp)).
Dislay()
Step 1 - Check whether queue is Empty (front == NULL).
Step 2 - If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
Step 4 - Display 'temp → data --->' and move it to the next node. Repeat the same until 'temp' reaches to
'rear' (temp → next != NULL).
Step 5 - Finally! Display 'temp → data ---> NULL'.
PROGRAM

#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
void delete();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}
OUTPUT

:: Queue Implementation using Linked List ::


****** MENU ******
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 65
Insertion is Success!!!

****** MENU ******


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 75
Insertion is Success!!!

****** MENU ******


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be insert: 85
Insertion is Success!!!

****** MENU ******


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
65--->75--->85--->NULL

****** MENU ******


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Deleted element: 65

****** MENU ******


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
75--->85--->NULL

RESULT
Thus the program for implementation of queue operations using singly linked list is executed and
verified successfully.
EX. NO: 6 (a)
IMPLEMENTATION OF DOUBLY LINKED LIST

AIM

To Write a C Program to implement doubly linked list operations.

ALGORITHM

InsertingAtBeginning()
Step 1 - Create a newNode with given value and newNode → previous as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to head.
Step 4 - If it is not Empty then, assign head to newNode → next and newNode to head.
InsertingAtEnd()
Step 1 - Create a newNode with given value and newNode → next as NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty, then assign NULL to newNode → previous and newNode to head.
Step 4 - If it is not Empty, then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next
is equal to NULL).
Step 6 - Assign newNode to temp → next and temp to newNode → previous.
InsertingAfter()
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to both newNode → previous & newNode → next and set newNode
to head.
Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and initialize temp1 with head.
Step 5 - Keep moving the temp1 to its next node until it reaches to the node after which we want to insert the
newNode (until temp1 → data is equal to location, here location is the node value after which we want to
insert the newNode).
Step 6 - Every time check whether temp1 is reached to the last node. If it is reached to the last node then
display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise
move the temp1 to next node.
Step 7 - Assign temp1 → next to temp2, newNode to temp1 → next, temp1 to newNode → previous, temp2
to newNode → next and newNode to temp2 → previous.
DeletingBeginning()
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list is having only one node (temp → previous is equal to temp → next)
Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list conditions)
Step 6 - If it is FALSE, then assign temp → next to head, NULL to head → previous and delete temp.
Deleting End()
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 - Check whether list has only one Node (temp → previous and temp → next both are NULL)
Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate from the function. (Setting
Empty list condition)
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in the list. (until temp → next
is equal to NULL)
Step 7 - Assign NULL to temp → previous → next and delete temp.
DeletingSpecific()
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4 - Keep moving the temp until it reaches to the exact node to be deleted or to the last node.
Step 5 - If it is reached to the last node, then display 'Given node not found in the list! Deletion not possible!!!'
and terminate the fuction.
Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having only one
node or not
Step 7 - If list has only one node and that is the node which is to be deleted then set head to NULL and delete
temp (free(temp)).
Step 8 - If list contains multiple nodes, then check whether temp is the first node in the list (temp == head).
Step 9 - If temp is the first node, then move the head to the next node (head = head → next), set head of
previous to NULL (head → previous = NULL) and delete temp.
Step 10 - If temp is not the first node, then check whether it is the last node in the list (temp → next == NULL).
Step 11 - If temp is the last node then set temp of previous of next to NULL (temp → previous → next =
NULL) and delete temp (free(temp)).
Step 12 - If temp is not the first node and not the last node, then set temp of previous of next to temp of next
(temp → previous → next = temp → next), temp of next of previous to temp of previous (temp → next →
previous = temp → previous) and delete temp (free(temp)).
Display()
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty, then display 'List is Empty!!!' and terminate the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with head.
Step 4 - Display 'NULL <--- '.
Step 5 - Keep displaying temp → data with an arrow (<===>) until temp reaches to the last node
Step 6 - Finally, display temp → data with arrow pointing to NULL (temp → data ---> NULL).
PROGRAM

#include<stdio.h>
#include<conio.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAtAfter(int,int);
void deleteBeginning();
void deleteEnd();
void deleteSpecific(int);
void display();

struct Node
{
int data;
struct Node *previous, *next;
}*head = NULL;

void main()
{
int choice1, choice2, value, location;
clrscr();
while(1)
{
printf("\n*********** MENU *************\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: printf("Enter the value to be inserted: ");
scanf("%d",&value);
while(1)
{
printf("\nSelect from the following Inserting options\n");
printf("1. At Beginning\n2. At End\n3. After a Node\n4. Cancel\nEnter your choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the location after which you want to insert: ");
scanf("%d",&location);
insertAtAfter(value,location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Inserting option!!!\n");
}
}
case 2: while(1)
{
printf("\nSelect from the following Deleting options\n");
printf("1. At Beginning\n2. At End\n3. Specific Node\n4. Cancel\nEnter your choice:
");
scanf("%d",&choice2);
switch(choice2)
{
case 1: deleteBeginning();
break;
case 2: deleteEnd();
break;
case 3: printf("Enter the Node value to be deleted: ");
scanf("%d",&location);
deleteSpecific(location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Deleting option!!!\n");
}
}
EndSwitch: break;
case 3: display();
break;
case 4: break;
default: printf("\nPlease select correct option!!!");
}
}
}

void insertAtBeginning(int value)


{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
newNode -> previous = NULL;
if(head == NULL)
{
newNode -> next = NULL;
head = newNode;
}
else
{
newNode -> next = head;
head = newNode;
}
printf("\nInsertion success!!!");
}
void insertAtEnd(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
newNode -> next = NULL;
if(head == NULL)
{
newNode -> previous = NULL;
head = newNode;
}
else
{
struct Node *temp = head;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newNode;
newNode -> previous = temp;
}
printf("\nInsertion success!!!");
}
void insertAtAfter(int value, int location)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = value;
if(head == NULL)
{
newNode -> previous = newNode -> next = NULL;
head = newNode;
}
else
{
struct Node *temp1 = head, *temp2;
while(temp1 -> data != location)
{
if(temp1 -> next == NULL)
{
printf("Given node is not found in the list!!!");
goto EndFunction;
}
else
{
temp1 = temp1 -> next;
}
}
temp2 = temp1 -> next;
temp1 -> next = newNode;
newNode -> previous = temp1;
newNode -> next = temp2;
temp2 -> previous = newNode;
printf("\nInsertion success!!!");
}
EndFunction:
}
void deleteBeginning()
{
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct Node *temp = head;
if(temp -> previous == temp -> next)
{
head = NULL;
free(temp);
}
else{
head = temp -> next;
head -> previous = NULL;
free(temp);
}
printf("\nDeletion success!!!");
}
}
void deleteEnd()
{
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct Node *temp = head;
if(temp -> previous == temp -> next)
{
head = NULL;
free(temp);
}
else{
while(temp -> next != NULL)
temp = temp -> next;
temp -> previous -> next = NULL;
free(temp);
}
printf("\nDeletion success!!!");
}
}
void deleteSpecific(int delValue)
{
if(head == NULL)
printf("List is Empty!!! Deletion not possible!!!");
else
{
struct Node *temp = head;
while(temp -> data != delValue)
{
if(temp -> next == NULL)
{
printf("\nGiven node is not found in the list!!!");
goto FuctionEnd;
}
else
{
temp = temp -> next;
}
}
if(temp == head)
{
head = NULL;
free(temp);
}
else
{
temp -> previous -> next = temp -> next;
free(temp);
}
printf("\nDeletion success!!!");
}
FuctionEnd:
}
void display()
{
if(head == NULL)
printf("\nList is Empty!!!");
else
{
struct Node *temp = head;
printf("\nList elements are: \n");
printf("NULL <--- ");
while(temp -> next != NULL)
{
printf("%d <===> ",temp -> data);
temp=temp->next;
}
printf("%d ---> NULL", temp -> data);
}
}
OUTPUT

*********** MENU *************


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be inserted: 10
Select from the following Inserting options
1. At Beginning
2. At End
3. After a Node
4. Cancel
Enter your choice: 1
Insertion success!!!
Select from the following Inserting options
1. At Beginning
2. At End
3. After a Node
4. Cancel
Enter your choice: 4

*********** MENU *************


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be inserted: 20
Select from the following Inserting options
1. At Beginning
2. At End
3. After a Node
4. Cancel
Enter your choice: 2
Insertion success!!!
Select from the following Inserting options
1. At Beginning
2. At End
3. After a Node
4. Cancel
Enter your choice: 4

*********** MENU *************


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter the value to be inserted: 25
Select from the following Inserting options
1. At Beginning
2. At End
3. After a Node
4. Cancel
Enter your choice: 3
Enter the location after which you want to insert: 10
Insertion success!!!
Select from the following Inserting options
1. At Beginning
2. At End
3. After a Node
4. Cancel
Enter your choice: 4

*********** MENU *************


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
List elements are:
NULL <--- 10 <===> 25 <===> 20 ---> NULL

*********** MENU *************


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Select from the following Deleting options
1. At Beginning
2. At End
3. Specific Node
4. Cancel
Enter your choice: 2
Deletion success!!!
Select from the following Deleting options
1. At Beginning
2. At End
3. Specific Node
4. Cancel
Enter your choice: 3
Enter the Node value to be deleted: 25
Deletion success!!!
Select from the following Deleting options
1. At Beginning
2. At End
3. Specific Node
4. Cancel
Enter your choice: 4

*********** MENU *************


1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
List elements are:
NULL <--- 10 ---> NULL

RESULT

Thus the program to implement doubly linked list operations is executed and verified
successfully.
EX. NO: 6 (b)
ARRAY IMPLEMENTATION OF DOUBLE ENDED QUEUE ADT

AIM

To Write a C Program to implement the double ended queue ADT operations using an
array.

ALGORITHM
Enqueue(front)
Step 1. Check whether queue is full (f==0 && r==N-1) || (f==r+1)
Step 2. If it is full then display “Queue full, Insertion is not possible” and terminate the function
Step 3. Else if (f==-1 && r==-1) then assign f=r=0 and set dequeue [f]= value
Step 4. Else if (f==0) assign f=n-1 and dequeue[f]= value
Step 5. Else if decrement front value by one(f--) and set dequeue [f]= value

Enqueue(rear)
Step 1. Check whether queue is full
Step 2. If it is full then display “Queue full, Insertion is not possible” and terminate the function
Step 3. Else if (f==-1) && (r==-1) then assign r=0 and set dequeue[r] = value
Step 4. Else if (r==N-1) then assign r=0 and set dequeue[r] = value
Step 5. Else increment rear value by ONE (r++) and set dequeue[r] = value

Dequeue(front)
Step 1. Check whether queue is empty (f==-1 && r==-1)
Step 2. If it is empty then display “Queue is empty, deletion is not possible” and terminate the function
Step 3. Else if (f==r) then print the deleted element and assign f= -1 and r=-1
Step 4. Else if (f==n-1) then print the deleted element and assign f=0
Step 5. Else print the deleted element and increment front value by one(f+1)

Dequeue(rear)
Step 1. Check whether queue is empty (f==-1 && r==-1)
Step 2. If it is empty then display “Queue is empty, deletion is not possible” and terminate the function
Step 3. Else if (f==r) then print the deleted element and assign f=-1 and r=-1
Step 4. Else if (r==0) then print the deleted element and assign r=n-1
Step 5. Else print the deleted element and decrement rear value by one(r-1)
PROGRAM

#include<stdio.h>
#include<conio.h>
#define SIZE 100
void enQueue(int);
int deQueueFront();
int deQueueRear();
void enQueueRear(int);
void enQueueFront(int);
void display();
int queue[SIZE];
int rear = 0, front = 0;
int main()
{
char ch;
int choice1, choice2, value;
printf("\n******* Type of Double Ended Queue *******\n");
do
{
printf("\n1.Input-restricted deque \n");
printf("2.output-restricted deque \n");
printf("\nEnter your choice of Queue Type : ");
scanf("%d",&choice1);
switch(choice1)
{
case 1:
printf("\nSelect the Operation\n");
printf("1.Insert\n2.Delete from Rear\n3.Delete from Front\n4. Display");
do
{
printf("\nEnter your choice for the operation in c deque: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: enQueueRear(value);
display();
break;
case 2: value = deQueueRear();
printf("\nThe value deleted is %d",value);
display();
break;
case 3: value=deQueueFront();
printf("\nThe value deleted is %d",value);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
}
printf("\nDo you want to perform another operation (Y/N): ");
ch=getch();
}while(ch=='y'||ch=='Y');
getch();
break;

case 2 :
printf("\n---- Select the Operation ---- \n");
printf("1. Insert at Rear\n2. Insert at Front\n3. Delete\n4. Display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: enQueueRear(value);
display();
break;
case 2: enQueueFront(value);
display();
break;
case 3: value = deQueueFront();
printf("\nThe value deleted is %d",value);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
}
printf("\nDo you want to perform another operation (Y/N): ");
ch=getch();
} while(ch=='y'||ch=='Y');
getch();
break ;
}
printf("\nDo you want to continue(y/n):");
ch=getch();
}while(ch=='y'||ch=='Y');
}

void enQueueRear(int value)


{
char ch;
if(front == SIZE/2)
{
printf("\nQueue is full!!! Insertion is not possible!!! ");
return;
}
do
{
printf("\nEnter the value to be inserted:");
scanf("%d",&value);
queue[front] = value;
front++;
printf("Do you want to continue insertion Y/N");
ch=getch();
}while(ch=='y');
}

void enQueueFront(int value)


{
char ch;
if(front==SIZE/2)
{
printf("\nQueue is full!!! Insertion is not possible!!!");
return;
}
do
{
printf("\nEnter the value to be inserted:");
scanf("%d",&value);
rear--;
queue[rear] = value;
printf("Do you want to continue insertion Y/N");
ch = getch();
}
while(ch == 'y');
}
int deQueueRear()
{
int deleted;
if(front == rear)
{
printf("\nQueue is Empty!!! Deletion is not possible!!!");
return 0;
}
front--;
deleted = queue[front+1];
return deleted;
}
int deQueueFront()
{
int deleted;
if(front == rear)
{
printf("\nQueue is Empty!!! Deletion is not possible!!!");
return 0;
}
rear++;
deleted = queue[rear-1];
return deleted;
}

void display()
{
int i;
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nThe Queue elements are:");
for(i=rear; i < front; i++)
{
printf("%d\t ",queue[i]);
}
}
}

OUTPUT

******* Type of Double Ended Queue *******


1.Input-restricted deque
2.output-restricted deque
Enter your choice of Queue Type : 1
Select the Operation
1. Insert
2. Delete from Rear
3. Delete from Front
4. Display
Enter your choice for the operation in c deque: 1
Enter the value to be inserted:10
Do you want to continue insertion Y/N
The Queue elements are:10
Do you want to perform another operation (Y/N): y
Enter your choice for the operation in c deque: 1
Enter the value to be inserted:20
Do you want to continue insertion Y/N
The Queue elements are:10 20
Do you want to perform another operation (Y/N): y
Enter your choice for the operation in c deque: 1
Enter the value to be inserted:30
Do you want to continue insertion Y/N
The Queue elements are:10 20 30
Do you want to perform another operation (Y/N): y
Enter your choice for the operation in c deque: 2
The value deleted is 30
The Queue elements are:10 20

RESULT
Thus the program for implementation of double ended queue ADT using an array is executed and
verified successfully.
EX. NO: 7
BINARY SEARCH TREE OPERATIONS

AIM
To develop a program to implement Binary Search Tree.

ALGORITHM

Search Operation in BST

In a binary search tree, the search operation is performed with O(log n) time complexity. The search
operation is performed as follows...
Step 1 - Read the search element from the user.
Step 2 - Compare the search element with the value of root node in the tree.
Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function
Step 4 - If both are not matched, then check whether search element is smaller or
larger than that node value.
Step 5 - If search element is smaller, then continue the search process in left subtree.
Step 6- If search element is larger, then continue the search process in right subtree.
Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node

Step 8 - If we reach to the node having the value equal to the search value then display
"Element is found" and terminate the function.
Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function.

Insertion Operation in BST

In a binary search tree, the insertion operation is performed with O(log n) time complexity. In binary
search tree, new node is always inserted as a leaf node. The insertion operation is performed as
follows...

Step 1 - Create a newNode with given value and set its left and right to NULL.
Step 2 - Check whether tree is Empty.
Step 3 - If the tree is Empty, then set root to newNode.
Step 4 - If the tree is Not Empty, then check whether the value of newNode is
smaller or larger than the node (here it is root node).
Step 5 - If newNode is smaller than or equal to the node then move to its left child. If
newNode is larger than the node then move to its right child.
Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to NULL).
Step 7 - After reaching the leaf node, insert the newNode as left child if the newNode is
smaller or equal to that leaf node or else insert it as right child.

Deletion Operation in BST


In a binary search tree, the deletion operation is performed with O(log n) time complexity.
Deleting a node from Binary search tree includes following three cases...

Case 1: Deleting a Leaf node (A node with no children)


Case 2: Deleting a node with one child
Case 3: Deleting a node with two children

Case 1: Deleting a leaf node

We use the following steps to delete a leaf node from BST...

Step 1 - Find the node to be deleted using search operation


Step 2 - Delete the node using free function (If it is a leaf) and terminate the function.

Case 2: Deleting a node with one child

We use the following steps to delete a node with one child from BST...
Step 1 - Find the node to be deleted using search operation
Step 2 - If it has only one child then create a link between its parent node and child
node.
Step 3 - Delete the node using free function and terminate the function.

Case 3: Deleting a node with two children


We use the following steps to delete a node with two children from BST...

Step 1 - Find the node to be deleted using search operation


Step 2 - If it has two children, then find the largest node in its left subtree (OR)
the smallest node in its right subtree.
Step 3 - Swap both deleting node and node which is found in the above step.
Step 4 - Then check whether deleting node came to case 1 or case 2 or else goto step 2
Step 5 - If it comes to case 1, then delete using case 1 logic.
Step 6- If it comes to case 2, then delete using case 2 logic.
Step 7 - Repeat the same process until the node is deleted from the tree.
PROGRAM

#include <stdio.h>
#include <stdlib.h>

struct node {
int data; //node will store some data
struct node *right_child; // right child
struct node *left_child; // left child
};

//function to create a node


struct node* new_node(int x) {
struct node *temp;
temp = malloc(sizeof(struct node));
temp -> data = x;
temp -> left_child = NULL;
temp -> right_child = NULL;

return temp;
}

// searching operation
struct node* search(struct node * root, int x) {
if (root == NULL || root -> data == x) //if root->data is x then the element is found
return root;
else if (x > root -> data) // x is greater, so we will search the right subtree
return search(root -> right_child, x);
else //x is smaller than the data, so we will search the left subtree
return search(root -> left_child, x);
}

// insertion
struct node* insert(struct node * root, int x) {
//searching for the place to insert
if (root == NULL)
return new_node(x);
else if (x > root -> data) // x is greater. Should be inserted to the right
root -> right_child = insert(root -> right_child, x);
else // x is smaller and should be inserted to left
root -> left_child = insert(root -> left_child, x);
return root;
}

//function to find the minimum value in a node


struct node* find_minimum(struct node * root) {
if (root == NULL)
return NULL;
else if (root -> left_child != NULL) // node with minimum value will have no left child
return find_minimum(root -> left_child); // left most element will be minimum
return root;
}

// deletion
struct node* delete(struct node * root, int x) {
//searching for the item to be deleted
if (root == NULL)
return NULL;
if (x > root -> data)
root -> right_child = delete(root -> right_child, x);
else if (x < root -> data)
root -> left_child = delete(root -> left_child, x);
else {
//No Child node
if (root -> left_child == NULL && root -> right_child == NULL) {
free(root);
return NULL;
}

//One Child node


else if (root -> left_child == NULL || root -> right_child == NULL) {
struct node *temp;
if (root -> left_child == NULL)
temp = root -> right_child;
else
temp = root -> left_child;
free(root);
return temp;
}

//Two Children
else {
struct node *temp = find_minimum(root -> right_child);
root -> data = temp -> data;
root -> right_child = delete(root -> right_child, temp -> data);
}
}
return root;
}

// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) // checking if the root is not null
{
inorder(root -> left_child); // traversing left child
printf(" %d ", root -> data); // printing data at root
inorder(root -> right_child); // traversing right child
}
}

int main() {
struct node *root;
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);

inorder(root);
printf("\n");
root = delete(root, 1);
root = delete(root, 40);
root = delete(root, 45);
root = delete(root, 9);

inorder(root);
printf("\n");
return 0;
}
OUTPUT

1 5 7 9 12 15 20 25 30 40 42 45
5 7 12 15 20 25 30 42

RESULT
Thus the program for implementation of binary search tree operations is executed and verified
successfully.
EX. NO: 8
BINARY TREE TRAVERSAL

AIM

To develop a program to create a Binary tree and perform the 3 different types of traversals: Inorder,
Preorder and Postorder.

ALGORITHM

INORDER ALGORITHM

Step 1 - Repeat Steps 2 to 4 while TREE != NULL

Step 2 - INORDER (TREE -> LEFT)

Step 3 - Write TREE -> DATA

Step 4 - INORDER (TREE -> RIGHT) [END OF LOOP]

Step 5 - END

PREORDER ALGORITHM

Step 1 - Repeat Steps 2 to 4 while TREE != NULL

Step 2 - Write TREE DATA

Step 3 - PREORDER(TREE LEFT)

Step 4 - PREORDER(TREE RIGHT) [END OF LOOP]

Step 5 - END

POSTORDER ALGORITHM

Step 1 - Repeat Steps 2 to 4 while TREE != NULL

Step 2 - POSTORDER(TREE LEFT)

Step 3 - POSTORDER(TREE RIGHT)

Step 4 - Write TREE DATA [END OF LOOP]

Step 5 - END
PROGRAM
#include <stdio.h>
#include <stdlib.h>

struct node {
int data;

struct node *leftChild;


struct node *rightChild;
};

struct node *root = NULL;

void insert(int data) {


struct node tempNode = (struct node) malloc(sizeof(struct node));
struct node *current;
struct node *parent;

tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;

//if tree is empty


if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;

while(1) {
parent = current;

//go to left of the tree


if(data < parent->data) {
current = current->leftChild;

//insert to the left


if(current == NULL) {
parent->leftChild = tempNode;
return;
}
} //go to right of the tree
else {
current = current->rightChild;

//insert to the right


if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}

struct node* search(int data) {


struct node *current = root;
printf("Visiting elements: ");

while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);

//go to left tree


if(current->data > data) {
current = current->leftChild;
}
//else go to right tree
else {
current = current->rightChild;
}

//not found
if(current == NULL) {
return NULL;
}
}

return current;
}

void pre_order_traversal(struct node* root) {


if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}
}

void inorder_traversal(struct node* root) {


if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
}
}

void post_order_traversal(struct node* root) {


if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
printf("%d ", root->data);
}
}

int main() {
int i;
int array[7] = { 27, 14, 35, 10, 19, 31, 42 };

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


insert(array[i]);

i = 31;
struct node * temp = search(i);

if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}

i = 15;
temp = search(i);

if(temp != NULL) {
printf("[%d] Element found.", temp->data);
printf("\n");
}else {
printf("[ x ] Element not found (%d).\n", i);
}

printf("\nPreorder traversal: ");


pre_order_traversal(root);

printf("\nInorder traversal: ");


inorder_traversal(root);

printf("\nPost order traversal: ");


post_order_traversal(root);

return 0;
}

OUTPUT

Visiting elements: 27 35 [31] Element found.


Visiting elements: 27 14 19 [x] Element not found.
[15].
Preorder traversal:27 14 10 19 35 31 42
Inorder traversal:10 14 19 27 31 35 42
Postorder traversal:10 19 14 31 42 35 27

RESULT
Thus the program for implementation of binary tree operations is executed and verified successfully.
EX. NO: 9
AVL TREE OPERATIONS

AIM

To develop a program to create a Binary tree and perform the 3 different types of traversals: Inorder,
Preorder and Postorder.

ALGORITHM

Step 1: Start
Step 2: Insert the new element into the tree using Binary Search Tree insertion logic.
Step 3: After insertion, check the Balance Factor of every node. • If the Balance Factor of every node is
0 or 1 or -1 then go for next operation. • If the Balance Factor of any node is other than 0 or 1 or -1 then
tree is said to be imbalanced. Then perform the suitable Rotation to make it balanced. And go for next
operation.
Step 4: Compare, the search element with the value of root node in the tree. If search element is
larger, then continue the search process in right subtree. If we reach to the node with search value, then
display "Element is found" and terminate the function.
Step 5: Stop.
PROGRAM

// AVL tree implementation in C

#include <stdio.h>
#include <stdlib.h>

// Create Node
struct Node {
int key;
struct Node *left;
struct Node *right;
int height;
};

int max(int a, int b);

// Calculate height
int height(struct Node *N) {
if (N == NULL)
return 0;
return N->height;
}

int max(int a, int b) {


return (a > b) ? a : b;
}

// Create a node
struct Node *newNode(int key) {
struct Node *node = (struct Node *)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
}

// Right rotate
struct Node *rightRotate(struct Node *y) {
struct Node *x = y->left;
struct Node *T2 = x->right;

x->right = y;
y->left = T2;

y->height = max(height(y->left), height(y->right)) + 1;


x->height = max(height(x->left), height(x->right)) + 1;

return x;
}

// Left rotate
struct Node *leftRotate(struct Node *x) {
struct Node *y = x->right;
struct Node *T2 = y->left;

y->left = x;
x->right = T2;

x->height = max(height(x->left), height(x->right)) + 1;


y->height = max(height(y->left), height(y->right)) + 1;

return y;
}

// Get the balance factor


int getBalance(struct Node *N) {
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}

// Insert node
struct Node *insertNode(struct Node *node, int key) {
// Find the correct position to insertNode the node and insertNode it
if (node == NULL)
return (newNode(key));

if (key < node->key)


node->left = insertNode(node->left, key);
else if (key > node->key)
node->right = insertNode(node->right, key);
else
return node;

// Update the balance factor of each node and


// Balance the tree
node->height = 1 + max(height(node->left),
height(node->right));

int balance = getBalance(node);


if (balance > 1 && key < node->left->key)
return rightRotate(node);

if (balance < -1 && key > node->right->key)


return leftRotate(node);

if (balance > 1 && key > node->left->key) {


node->left = leftRotate(node->left);
return rightRotate(node);
}

if (balance < -1 && key < node->right->key) {


node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

struct Node *minValueNode(struct Node *node) {


struct Node *current = node;

while (current->left != NULL)


current = current->left;

return current;
}

// Delete a nodes
struct Node *deleteNode(struct Node *root, int key) {
// Find the node and delete it
if (root == NULL)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);

else if (key > root->key)


root->right = deleteNode(root->right, key);

else {
if ((root->left == NULL) || (root->right == NULL)) {
struct Node *temp = root->left ? root->left : root->right;

if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
struct Node *temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);


}
}

if (root == NULL)
return root;

// Update the balance factor of each node and


// balance the tree
root->height = 1 + max(height(root->left),
height(root->right));

int balance = getBalance(root);


if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);

if (balance > 1 && getBalance(root->left) < 0) {


root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);

if (balance < -1 && getBalance(root->right) > 0) {


root->right = rightRotate(root->right);
return leftRotate(root);
}

return root;
}

// Print the tree


void printPreOrder(struct Node *root) {
if (root != NULL) {
printf("%d ", root->key);
printPreOrder(root->left);
printPreOrder(root->right);
}
}

int main() {
struct Node *root = NULL;

root = insertNode(root, 2);


root = insertNode(root, 1);
root = insertNode(root, 7);
root = insertNode(root, 4);
root = insertNode(root, 5);
root = insertNode(root, 3);
root = insertNode(root, 8);

printPreOrder(root);

root = deleteNode(root, 3);

printf("\nAfter deletion: ");


printPreOrder(root);

return 0;
}
OUTPUT

4 2 1 3 7 5 8
After deletion: 4 2 1 7 5 8

RESULT
Thus the program for implementation of AVL tree operations is executed and verified successfully.
EX. NO: 10 (a)
GRAPH TRAVERSAL-DEPTH FIRST SEARCH

AIM

To develop a program to create a graph traversal technique

ALGORITHM

Algorithm DFS(v)

{ write v;

visited[v]=1;

for i=1 to n

for j=1 to n

if edge[v,i]==1 && visited[i]==0

write [i];

visited[i]=1;

DFS[i];

}
PROGRAM
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
clrscr();
printf("Depth First Search");
printf("\n Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();
}

OUTPUT

Depth First Search


Enter the number of vertices: 3

Enter the adjacency matrix:


123
123
123

1->2
2->3

Graph is connected

RESULT

Thus the program for implementation of Depth first search tree is executed and verified
successfully.
EX. NO: 10(b)
GRAPH TRAVERSAL-BREADTH FIRST SEARCH

AIM

To develop a program to create a graph traversal technique

ALGORITHM

Algorithm BFS(n)
{
\\Given undirected graph G=(V,E) with n vertices
\\Edge[][]- adjacency matrix of given graph
\\visited[] initially set to zero for all vertices write v;
visited[v]=1;
front=rear=0;
while(front==rear)
{ for(i=1 to n)
do
{ for(j=1 to n)
do
{ if (edge[v,i]==1 && visited[i]==0)
then
{ write i;
visited[i]=1;
queue[rear++]=i;
}
}
}
}
v=queue[front];
}
PROGRAM
#include <stdio.h>
#include <conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{ visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{ int v;
clrscr();
printf("\nBreadth First Search");
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}
OUTPUT

Breadth First Search


Enter the number of vertices: 3

Enter graph data in matrix form:


123
123
123

Enter the starting vertex: 2

The node which are reachable are:


1 2 3

RESULT

Thus the program for implementation of breadth first search tree is executed and verified
successfully.
EX. NO: 11(a) SET OPERATIONS - UNION

AIM

To develop a program to implement union set operations.

ALGORITHM
void unionSet(int setA[], int sizeA, int setB[], int sizeB, int resultSet[], int *resultSize)
{
int i, j, k = 0;
for (i = 0; i < sizeA; i++)
{
resultSet[k++] = setA[i];
}
for (i = 0; i < sizeB; i++)
{
int found = 0; for (j = 0; j < sizeA; j++)
{
if (setB[i] == setA[j])
{ found = 1; break;
}
}
if (!found) { resultSet[k++] = setB[i];
}
}
PROGRAM

#include<stdio.h>

#include<conio.h>

void main()

int a[10],b[10],i,c[10],j,k=0,n1,n2;

printf("Enter number of element of set A\n");

scanf("%d",&n1);

printf("Enter the element of set A \n");

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

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

printf("Enter number of element of set B\n");

scanf("%d",&n2);

printf("Enter the element of set B \n");

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

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

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

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

if(c[j]==a[i])

break;

if(j==k) {

c[k]=a[i];

k++;

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

for(j=0;j<k;j++)
{

if(c[j]==b[i])

break;

if(j==k) // if element is not repeted then store in


set C

c[k]=b[i];

k++;

// printing of union of set A and set B

printf("Union of set A and B is:-\n");

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

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

OUTPUT

Enter number of element of set A

Enter the element of set A

112

Enter number of element of set B

Enter the element of set B

3452

Union of set A and B is:-

12345

RESULT
Thus the program for implementation of union operations is executed and verified successfully.
EX. NO: 11(b) SET OPERATIONS - INTERSECTION

AIM

To develop a program to implement intersection set operations.

ALGORITHM
void intersectionSet(int setA[], int sizeA, int setB[], int sizeB, int resultSet[], int *resultSize) {
int k = 0;
for (int i = 0; i < sizeA; i++) {
for (int j = 0; j < sizeB; j++) {
if (setA[i] == setB[j]) {
resultSet[k++] = setA[i];
break;
}
}
}
*resultSize = k;
}
PROGRAM

#include<stdio.h>

int main()

int a[100],b[100],c[100],n1,n2,n,k=0,i,j;

printf("Enter number of element of set A\n");

scanf("%d",&n1);

printf("Enter elements of set A\n");

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

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

printf("Enter number of element of set B\n");

scanf("%d",&n2);

printf("Enter elements of set B\n");

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

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

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

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

if(a[i]==b[j])

c[k]=a[i];

k++;

printf("intersection of set A and set B is:-\n");

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

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

return 0;

}
OUTPUT

Enter number of element of set A

Enter elements of set A

123

Enter number of element of set B

Enter elements of set B

1134

intersection of set A and set B is:-

13

RESULT
Thus the program for implementation of intersection operations is executed and verified
successfully.
EX. NO: 11(c) SET OPERATIONS - DIFFERENCE

AIM

To develop a program to implement difference set operations.

ALGORITHM
void difference(int set1[], int size1, int set2[], int size2) {
printf("Difference (set1 - set2): ");
for (int i = 0; i < size1; i++) {
int found = 0;
for (int j = 0; j < size2; j++) {
if (set1[i] == set2[j]) {
found = 1;
break;
}
}
if (!found) {
printf("%d ", set1[i]);
}
}
printf("\n");
}
PROGRAM
include<stdio.h>
int main()
{
int a[10],b[10],c[10],d[10],m=0,k=0,n1,n2,l,i,j;
printf("Enter size of set A");
scanf("%d",&n1);
printf("Enter element of set");
for( i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("Enter size of set B");
scanf("%d",&n2);
printf("Enter element of set");
for( i=0;i<n2;i++)
scanf("%d",&b[i]);
for( i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(b[j]==a[i])
break;
}
if(j==n2)
{
for(l=0;l<k;l++)
{
if(c[l]==a[i])
break;
}
if(l==k)
{
c[k]=a[i];
k++;
}
}

}
for( i=0;i<n2;i++)
{
for(j=0;j<n1;j++)
{
if(b[i]==a[j])
break;
}
if(j==n1)
{
for(l=0;l<m;l++)
{
if(d[l]==b[i])
break;
}
if(l==m)
{
d[m]=b[i];
m++;
}
}

}
printf("Difference of A-B is:-\n");
for(i=0;i<k;i++)
{
printf("%d ",c[i]);
}
printf("\n");
printf("Difference of B-A is:-\n");
for(i=0;i<m;i++)
{
printf("%d ",d[i]);
}
return 0;

}
OUTPUT

Enter size of set A 4


Enter element of set
1243
Enter size of set B 4
Enter element of set
3546
Difference of A-B is:-
12
Difference of B-A is:-
56

RESULT:

Thus the program for implementation of difference operations is executed and verified
successfully.
EX. NO: 12 EVALUATION OF POLYNOMIALS USING LINKED LIST

AIM

To develop a program to evaluate polynomials using linked list.

ALGORITHM
Algorithm evaluate (int x)
{
double sum = 0.0;
Node *q = p;
while(q != NULL){
sum += q->coff * pow(x, q->exp);
q = q->next;
}
return sum;
}
PROGRAM
#include <stdio.h>
#include <math.h>
struct Node {
int coeff;
int exp;
struct Node * next;
}* poly = NULL;
void create() {
struct Node * t, * last = NULL;
int num, i;
printf("Enter number of terms: ");
scanf("%d", & num);
printf("Enter each term with coeff and exp:\n");
for (i = 0; i < num; i++) {
t = (struct Node * ) malloc(sizeof(struct Node));
scanf("%d%d", & t -> coeff, & t -> exp);
t -> next = NULL;
if (poly == NULL) {
poly = last = t;
} else {
last -> next = t;
last = t;
}
}
}
void Display(struct Node * p) {
printf("%dx%d ", p -> coeff, p -> exp);
p = p -> next;
while (p) {
printf("+ %dx%d ", p -> coeff, p -> exp);
p = p -> next;
}
printf("\n");
}
long Eval(struct Node * p, int x) {
long val = 0;
while (p) {
val += p -> coeff * pow(x, p -> exp);
p = p -> next;
}
return val;
}
int main() {
int x;
create();
Display(poly);
printf("Enter value of x: ");
scanf("%d", &x);
printf("%ld\n", Eval(poly, x));
return 0;
}

OUTPUT
Enter the number of terms: 4
Enter each term with coeff and exp:
4 3
9 2
6 1
7 0
4x3 + 9x2 + 6x1 + 7x0
Enter the value of x: 2
87

RESULT
Thus the program to evaluate polynomials using linked list is executed and verified
successfully.

You might also like