DS Rec Final
DS Rec Final
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
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 (x == a[mid]) {
return (mid);
} else if (x < a[mid]) {
binsearch(a, x, low, mid - 1);
} else {
binsearch(a, x, mid + 1, high);
}
}
OUTPUT
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
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 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
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
#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
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
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
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
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
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
RESULT
Thus the program for implementation of queue operations using array is executed and verified
successfully.
EX. NO: 4
IMPLEMENTATION OF LIST ADT
AIM
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()
Delete()
Search()
Display()
#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
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
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
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
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!!!");
}
}
}
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 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
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
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.
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.
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.
#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
};
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;
}
// 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;
}
//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 5 - END
PREORDER ALGORITHM
Step 5 - END
POSTORDER ALGORITHM
Step 5 - END
PROGRAM
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1) {
parent = current;
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//not found
if(current == NULL) {
return NULL;
}
}
return current;
}
int main() {
int i;
int array[7] = { 27, 14, 35, 10, 19, 31, 42 };
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);
}
return 0;
}
OUTPUT
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
#include <stdio.h>
#include <stdlib.h>
// Create Node
struct Node {
int key;
struct Node *left;
struct Node *right;
int height;
};
// Calculate height
int height(struct Node *N) {
if (N == NULL)
return 0;
return N->height;
}
// 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;
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;
return y;
}
// 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));
return node;
}
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 ((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;
if (root == NULL)
return root;
return root;
}
int main() {
struct Node *root = NULL;
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
ALGORITHM
Algorithm DFS(v)
{ write v;
visited[v]=1;
for i=1 to n
for j=1 to n
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
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
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
RESULT
Thus the program for implementation of breadth first search tree is executed and verified
successfully.
EX. NO: 11(a) SET OPERATIONS - UNION
AIM
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;
scanf("%d",&n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
scanf("%d",&n2);
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;
c[k]=b[i];
k++;
for(i=0;i<k;i++)
printf("%d ",c[i]);
OUTPUT
112
3452
12345
RESULT
Thus the program for implementation of union operations is executed and verified successfully.
EX. NO: 11(b) SET OPERATIONS - INTERSECTION
AIM
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;
scanf("%d",&n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
scanf("%d",&n2);
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++;
for(i=0;i<k;i++)
printf("%d ",c[i]);
return 0;
}
OUTPUT
123
1134
13
RESULT
Thus the program for implementation of intersection operations is executed and verified
successfully.
EX. NO: 11(c) SET OPERATIONS - DIFFERENCE
AIM
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
RESULT:
Thus the program for implementation of difference operations is executed and verified
successfully.
EX. NO: 12 EVALUATION OF POLYNOMIALS USING LINKED LIST
AIM
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.