Practical 1 (DS)
Practical 1 (DS)
Practical-1
void main(){
int a[4] = {1,2,3,4};
int b;
Output :
BATCH-B1 PAGE | 1
SUBJECT : DS SEM-3 23012011133
int a;
printf("Enter the value to find factorial : ");
scanf("%d",&a);
printf("factorial of %d! is %d",a,factorial(a));
Output :
void main(){
int a = 5;
int *p = &a;
printf("%u\n",&a);
printf("%u\n",p);
}
Output :
4) To find out the Min, Max element and also Avg of the given
Array.
Code :
#include <stdio.h>
void main(){
int a[4] = {6,4,5,8};
int min = a[0];
int max = a[0];
int sum = 0;
int avg;
for(int i = 0; i < 4; i++){
if(max < a[i]){
BATCH-B1 PAGE | 2
SUBJECT : DS SEM-3 23012011133
max = a[i];
}
}
printf("The maximum value is %d\n",max);
for(int i = 0; i < 4; i++){
if(min > a[i]){
min = a[i];
}
}
printf("The minimum value is %d\n",min);
for(int i = 0; i < 4; i++){
sum = sum + a[i];
}
avg = sum/4;
printf("The average is %d\n",avg);
}
Output :
Output :
BATCH-B1 PAGE | 3
SUBJECT : DS SEM-3 23012011133
Practical -2
1) Write a C Program to Implement an Array
representation of the sparse matrices.
Code :
#include <stdio.h>
int main(void) {
int a[4][5];
int count = 0, c = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
printf("enter the value for element a[%d][%d]: ", i,
j);
scanf("%d", &a[i][j]);
}
}
printf("%d\n", count);
int b[count][3];
BATCH-B1 PAGE | 4
SUBJECT : DS SEM-3 23012011133
Output :
BATCH-B1 PAGE | 5
SUBJECT : DS SEM-3 23012011133
int main(void) {
int a[5] = {1,2,3,4,5};
int initLoc = &a[0];
for(int i = 0; i < 5; i++){
printf("%u\n", initLoc + sizeof(int)*i);
}
return 0;
}
Output :
void main(){
// Row Major Address Calculation:
int a,b;
printf("Enter Number of rows for Array: ");
scanf("%d",&a);
printf("Enter Number of columns for Array: ");
scanf("%d",&b);
int arr[a][b];
for(int i = 0; i < a; i++){
for(int j = 0; j < b; j++){
printf("Enter value for a[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
BATCH-B1 PAGE | 6
SUBJECT : DS SEM-3 23012011133
}
}
Output :
BATCH-B1 PAGE | 7
SUBJECT : DS SEM-3 23012011133
void main(){
int a[2][2][2] = {{{1,2},{3,4}},{{1,3},{4,5}}};
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
for(int k = 0; k < 2; k++){
printf("value of a[%d][%d][%d] is
%d\n",i,j,k,a[i][j][k]);
}
}
}
}
BATCH-B1 PAGE | 8
SUBJECT : DS SEM-3 23012011133
Output :
void main(){
int a[2][2] = {{1,2},{3,4}};
int b;
Output :
BATCH-B1 PAGE | 9
SUBJECT : DS SEM-3 23012011133
Practical - 3
int stack[MAX];
int top = -1;
void pop() {
if (top == -1) {
printf("STACK UNDERFLOW");
}
else {
stack[--top];
}
}
void display() {
if (top == -1) {
printf("Stack is Empty");
}
else {
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
BATCH-B1 PAGE | 10
SUBJECT : DS SEM-3 23012011133
}
}
int peep() {
if (top == -1) {
printf("Stack is Empty");
return -1;
} else {
return stack[top];
}
}
void main() {
push(20);
push(30);
push(40);
printf("\n");
display();
pop();
printf("\n");
display();
printf("%d\n", isEmpty());
printf("%d\n", isFull());
printf("%d\n", peep());
}
Output :
BATCH-B1 PAGE | 11
SUBJECT : DS SEM-3 23012011133
Practical-4
1) Write a program to recognize the string with language
L={wcwR / w takes multiple occurrences of {a,b}}.
Code :
#include <stdio.h>
#define MAX 100
char str[MAX];
int top = -1;
char pop() {
if (top == -1) {
printf("Stack Underflow");
} else {
top--;
return str[top + 1];
}
}
BATCH-B1 PAGE | 12
SUBJECT : DS SEM-3 23012011133
int main() {
char inputStr[40];
int target;
printf("Enter a input String of the format aCb: ");
scanf("%s", inputStr);
if (check(inputStr, target)) {
printf("Expression is palindrome\n");
} else {
printf("Expression is not Palindrome\n");
}
return 0;
}
Output :
BATCH-B1 PAGE | 13
SUBJECT : DS SEM-3 23012011133
char str[MAX];
int top = -1;
char pop() {
if (top == -1) {
printf("Stack Underflow");
} else {
top--;
return str[top + 1];
}
}
int isEmpty(){
if(top == -1){
return 1;
}
else{
return 0;
}
}
BATCH-B1 PAGE | 14
SUBJECT : DS SEM-3 23012011133
int main() {
char inputStr[40];
int target;
printf("Enter a input String: ");
scanf("%s", inputStr);
for(int i = 0; inputStr[i] != '\0';i++){
if(inputStr[i] == '[' || inputStr[i] == '{' || inputStr[i]
== '('){
push(inputStr[i]);
}
}
if(isEmpty()){
printf("The expression is valid");
}
else{
printf("The expression is not valid");
}
return 0;
}
Output :
BATCH-B1 PAGE | 15
SUBJECT : DS SEM-3 23012011133
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
BATCH-B1 PAGE | 16
SUBJECT : DS SEM-3 23012011133
while (!isEmpty(top)) {
postfix[j++] = pop(stack, &top);
}
postfix[j] = '\0';
}
BATCH-B1 PAGE | 17
SUBJECT : DS SEM-3 23012011133
int main() {
char infix[MAX], postfix[MAX];
infixToPostfix(infix, postfix);
return 0;
}
Output:
BATCH-B1 PAGE | 18
SUBJECT : DS SEM-3 23012011133
case '*':
return a * b;
case '/':
return a / b;
default:
return -1;
}
}
if (isOperand(postfix[i])) {
stack[++top] = postfix[i] - '0';
}
else {
int op2 = stack[top--];
int op1 = stack[top--];
int result = performOperation(op1, op2, postfix[i]);
if (result == -1) {
printf("Invalid operator\n");
return -1;
}
stack[++top] = result;
}
}
return stack[top];
}
int main() {
char postfix[MAX];
BATCH-B1 PAGE | 19
SUBJECT : DS SEM-3 23012011133
if (result != -1) {
printf("Result: %d\n", result);
}
return 0;
}
Output :
int fibonacci(int n) {
if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int main() {
int n;
BATCH-B1 PAGE | 20
SUBJECT : DS SEM-3 23012011133
printf("Fibonacci series:\n");
for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
Output :
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
printf("The sequence of moves involved in the Tower of Hanoi
are:\n");
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
BATCH-B1 PAGE | 21
SUBJECT : DS SEM-3 23012011133
Output :
Practical-5
1) Write a program to Implement Simple Queue with following
operations:
Code :
#include <stdio.h>
#define Max 5
int arr[Max];
int front = -1;
int rear = -1;
void insert(){
if (rear == Max){
printf("Queue is Full!\n");
return;
}
int m;
printf("Enter a value to add : ");
scanf("%d",&m);
BATCH-B1 PAGE | 22
SUBJECT : DS SEM-3 23012011133
BATCH-B1 PAGE | 23
SUBJECT : DS SEM-3 23012011133
}
}
Output :
BATCH-B1 PAGE | 24
SUBJECT : DS SEM-3 23012011133
else{
rear = (rear + 1) % m;
cqueue[rear] = t;
}
void dequeue(){
if(front == -1 && rear == -1){
printf("Cqueue is Empty!\n");
}
else{
printf("deleted element is %d\n",cqueue[front]);
BATCH-B1 PAGE | 25
SUBJECT : DS SEM-3 23012011133
front = (front + 1) % m;
}
}
void display(){
int i = front;
if(front == -1 && rear == -1){
printf("Cqueue is Empty!\n");
}
else{
printf("Queue is : \n");
while(i != rear){
printf("%d\n",cqueue[i]);
i = (i+1)%m;
}
printf("%d\n",cqueue[i]);
}
}
void main(){
int a;
while(1){
printf("Enter Value of Your choice : ");
scanf("%d",&a);
switch(a){
case 1 :
int s;
printf("Enter Value to add in Circular queue: ");
scanf("%d",&s);
enqueue(s);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
}
}
}
BATCH-B1 PAGE | 26
SUBJECT : DS SEM-3 23012011133
Output :
BATCH-B1 PAGE | 27
SUBJECT : DS SEM-3 23012011133
BATCH-B1 PAGE | 28
SUBJECT : DS SEM-3 23012011133
DEqueue[rear] = t;
}
}
void deleteAtRear(){
if(front == -1 && rear == -1){
printf("DEqueue is empty\n");
}
else if(front == rear){
printf("Deleted Element is %d\n",DEqueue[rear]);
front = rear = -1;
}
else if(rear == 0){
printf("Deleted Element is %d\n",DEqueue[rear]);
rear = m-1;
}
else{
printf("Deleted Element is %d\n",DEqueue[rear]);
rear = rear - 1;
}
}
void deleteAtFront(){
if(front == -1 && rear == -1){
printf("DEqueue is empty\n");
}
else if(front == rear){
printf("Deleted Element is %d\n",DEqueue[front]);
front = rear = -1;
}
else if(front == m-1){
printf("Deleted Element is %d\n",DEqueue[front]);
front = 0;
}
else{
printf("Deleted Element is %d\n",DEqueue[front]);
front = front + 1;
}
}
void display(){
int i = front;
if(front == -1 && rear == -1){
BATCH-B1 PAGE | 29
SUBJECT : DS SEM-3 23012011133
printf("DEqueue is empty!");
}
else{
printf("DEqueue is : \n");
while(i != rear){
printf("%d\n",DEqueue[i]);
i = (i+1) % m;
}
printf("%d\n",DEqueue[i]);
}
}
void main(){
int a;
int b;
while(1){
printf("Enter Type of Queue to implement 1 -> insertion
restricted 2 -> deletion restricted : ");
scanf("%d",&a);
switch(a){
case 1 :
printf("You can implement Insertion restricted
Queue!\n");
while(1){
printf("Enter Value of operation of Your choice :
");
scanf("%d",&b);
switch(b){
case 1:
int s;
printf("Enter Value to add in DEqueue queue:
");
scanf("%d",&s);
insertAtFront(s);
break;
case 2:
deleteAtFront();
break;
case 3:
BATCH-B1 PAGE | 30
SUBJECT : DS SEM-3 23012011133
deleteAtRear();
break;
case 4:
display();
break;
}
}
break;
case 2 :
printf("You can implement deletion restricted
Queue!\n");
while(1){
printf("Enter Value of operation Your choice : ");
scanf("%d",&b);
switch(b){
case 1:
int s;
printf("Enter Value to add in DEqueue queue:
");
scanf("%d",&s);
insertAtFront(s);
break;
case 2:
int s2;
printf("Enter Value to add in DEqueue queue:
");
scanf("%d",&s2);
insertAtRear(s2);
break;
case 3:
deleteAtFront();
break;
case 4:
display();
break;
}
}
break;
}
}
BATCH-B1 PAGE | 31
SUBJECT : DS SEM-3 23012011133
Output -1 :
Output -2 :
BATCH-B1 PAGE | 32
SUBJECT : DS SEM-3 23012011133
Practical - 6
1) To implement singly linked list with following operations:
Code :
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *link;
} *head = NULL , *temp = NULL , *temp2 = NULL;
void create(int t){
temp = (struct node*)malloc(sizeof(struct node));
temp -> data = t;
temp -> link = NULL;
BATCH-B1 PAGE | 33
SUBJECT : DS SEM-3 23012011133
}
void insertAtStart(int b){
create(b);
if(head == NULL){
head = temp;
}
else{
temp -> link = head;
head = temp;
}
}
void insertAtEnd(int b){
create(b);
if(head == NULL){
head = temp;
}
else{
temp2 = head;
while(temp2-> link != NULL){
temp2 = temp2 -> link;
}
temp2 -> link = temp;
}
}
void insertAtSpecifiedNode(int b){
int n;
create(b);
printf("Enter Location: ");
scanf("%d",&n);
if(head == NULL){
head = temp;
}
else{
temp2 = head;
for(int i = 1; i < n; i++){
temp2 = temp2 -> link;
if(temp2 -> link == NULL){
printf("No of nodes is less then required");
return;
}
BATCH-B1 PAGE | 34
SUBJECT : DS SEM-3 23012011133
}
temp -> link = temp2 -> link;
temp2 -> link = temp;
}
}
void display(){
if(head == NULL){
printf("Linked List is empty!");
}
else{
temp = head;
while(temp != NULL){
printf("%d\n", temp -> data);
temp = temp -> link;
}
}
}
void deleteAtStart(){
int t;
if(head == NULL){
printf("Linked List is Empty!");
}
else{
temp = head;
head = temp -> link;
t = temp -> data;
free(temp);
}
printf("The deleted value is %d\n",t);
}
void deleteAtEnd(){
int t;
if(head == NULL){
printf("Linked List is Empty!");
}
else{
temp = head;
temp2 = head;
while(temp -> link -> link != NULL){
temp = temp -> link;
BATCH-B1 PAGE | 35
SUBJECT : DS SEM-3 23012011133
}
temp2 = temp -> link;
t = temp2 -> data;
free(temp2);
temp -> link = NULL;
}
printf("The deleted value is %d\n",t);
}
void deleteAtSpecifiedNode(){
int n,t;
printf("Enter Location: ");
scanf("%d",&n);
if(head == NULL){
printf("Linked List is Empty!");
}
else{
temp = head;
for(int i = 1; i < n; i++){
temp = temp -> link;
}
temp2 = temp -> link;
t = temp2 -> data;
temp -> link = temp2 -> link;
free(temp);
}
printf("The deleted value is %d\n",t);
}
void main(){
int choice,val;
printf("1: insertAtStart\n2: insertAtEnd\n3:
insertAtSpecifiedNode\n4: deleteAtStart\n5: deleteAtEnd\n6:
deleteAtSpecifiedNode\n7: display\n8: exit\n");
while(1){
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter a value to insert: ");
scanf("%d",&val);
insertAtStart(val);
BATCH-B1 PAGE | 36
SUBJECT : DS SEM-3 23012011133
break;
case 2:
printf("Enter a value to insert: ");
scanf("%d",&val);
insertAtEnd(val);
break;
case 3:
printf("Enter a value to insert: ");
scanf("%d",&val);
insertAtSpecifiedNode(val);
break;
case 4:
deleteAtStart();
break;
case 5:
deleteAtEnd();
break;
case 6:
deleteAtSpecifiedNode();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Select Correct Option!\n");
}
}
}
Output :
BATCH-B1 PAGE | 37
SUBJECT : DS SEM-3 23012011133
BATCH-B1 PAGE | 38
SUBJECT : DS SEM-3 23012011133
BATCH-B1 PAGE | 39
SUBJECT : DS SEM-3 23012011133
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
struct node *pre;
} *head = NULL , *temp = NULL , *temp2 = NULL;
else{
temp -> next = head;
head -> pre = temp;
head = temp;
}
}
else{
temp2 = head;
while(temp2 -> next != NULL){
BATCH-B1 PAGE | 40
SUBJECT : DS SEM-3 23012011133
}
}
void insertAtSpecifiedNode(int t){
int n;
printf("Enter the position of the node: ");
scanf("%d",&n);
create(t);
if(head == NULL){
head = temp;
}
else{
temp2 = head;
for(int i = 1; i < n; i++){
temp2 = temp2 -> next;
if(temp2 == NULL){
printf("No of nodes is less then required\n");
return;
}
}
void deleteAtStart(){
int t;
BATCH-B1 PAGE | 41
SUBJECT : DS SEM-3 23012011133
if(head == NULL){
printf("Doubly Linked List is Empty!\n");
return;
}
else{
else{
temp = head;
head = head -> next;
head -> pre = NULL;
t = temp -> data;
free(temp);
}
printf("So the deleted value is %d\n",t);
}
void deleteAtEnd(){
int t;
if(head == NULL){
printf("Doubly Linked List is Empty!\n");
}
else{
BATCH-B1 PAGE | 42
SUBJECT : DS SEM-3 23012011133
else{
temp = head;
while(temp -> next != NULL){
temp2 = temp;
temp = temp -> next;
}
temp2 -> next = NULL;
t = temp -> data;
free(temp);
}
printf("So the deleted value is %d\n",t);
}
void deleteAtSpecifiedNode(){
int t,n;
printf("Enter the position of the node: ");
scanf("%d",&n);
if(head == NULL){
printf("Doubly Linked List is Empty!\n");
}
else{
temp = head;
for(int i = 1; i<n; i++){
temp2 = temp;
temp = temp -> next;
BATCH-B1 PAGE | 43
SUBJECT : DS SEM-3 23012011133
return;
}
}
void display(){
if(head == NULL){
printf("Doubly Linked List is Empty!\n");
}
else{
temp = head;
while(temp != NULL){
printf("%d\n",temp -> data);
temp = temp -> next;
}
}
}
void main(){
int choice,n;
while(1){
printf("Enter the value for choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the value to add: ");
scanf("%d",&n);
BATCH-B1 PAGE | 44
SUBJECT : DS SEM-3 23012011133
insertAtStart(n);
break;
case 2:
printf("Enter the value to add: ");
scanf("%d",&n);
insertAtEnd(n);
break;
case 3:
printf("Enter the value to add: ");
scanf("%d",&n);
insertAtSpecifiedNode(n);
break;
case 4:
deleteAtStart();
break;
case 5:
deleteAtEnd();
break;
case 6:
deleteAtSpecifiedNode();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Enter Correct Choice!\n");
break;
}
}
}
Output :
BATCH-B1 PAGE | 45
SUBJECT : DS SEM-3 23012011133
BATCH-B1 PAGE | 46
SUBJECT : DS SEM-3 23012011133
BATCH-B1 PAGE | 47
SUBJECT : DS SEM-3 23012011133
}
temp2 -> link = temp;
temp -> link = head;
}
}
void insertAtSpecifiedNode(int b){
int n;
create(b);
printf("Enter Location: ");
scanf("%d",&n);
if(head == NULL){
head = temp;
temp -> link = head;
}
else{
temp2 = head;
for(int i = 1; i < n; i++){
temp2 = temp2 -> link;
if(temp2 -> link == head){
printf("No of nodes is less then required\n");
return;
}
}
temp -> link = temp2 -> link;
temp2 -> link = temp;
}
}
void display(){
if(head == NULL){
printf("Linked List is empty!");
}
else{
temp = head;
while(temp->link != head){
printf("%d\n", temp -> data);
temp = temp -> link;
}
printf("%d\n", temp -> data);
}
}
BATCH-B1 PAGE | 48
SUBJECT : DS SEM-3 23012011133
void deleteAtStart(){
int t;
if(head == NULL){
printf("Linked List is Empty!");
}
else if(head -> link == head){
temp = head;
free(temp);
}
else{
temp = head;
temp2 = head;
while(temp2->link != head){
temp2 = temp2 -> link;
}
head = temp -> link;
temp2 -> link = head;
t = temp -> data;
free(temp);
}
printf("The deleted value is %d\n",t);
}
void deleteAtEnd(){
int t;
if(head == NULL){
printf("Linked List is Empty!");
}
else if(head -> link == head){
temp = head;
free(temp);
}
else{
temp = head;
temp2 = head;
while(temp -> link != head){
temp2 = temp;
temp = temp -> link;
}
temp2 -> link = temp -> link;
t = temp -> data;
BATCH-B1 PAGE | 49
SUBJECT : DS SEM-3 23012011133
free(temp);
}
printf("The deleted value is %d\n",t);
}
void deleteAtSpecifiedNode(){
int n,t;
printf("Enter Location: ");
scanf("%d",&n);
if(head == NULL){
printf("Linked List is Empty!");
}
else if(head -> link == head){
printf("It has only one element");
}
else{
temp = head;
temp2 = head;
for(int i = 1; i < n; i++){
temp2 = temp;
temp = temp -> link;
if(temp -> link == head){
printf("No of nodes is less then required\n");
return;
}
}
temp2 -> link = temp -> link;
t = temp -> data;
free(temp);
}
printf("The deleted value is %d\n",t);
}
void main(){
int choice,val;
printf("1: insertAtStart\n2: insertAtEnd\n3:
insertAtSpecifiedNode\n4: deleteAtStart\n5: deleteAtEnd\n6:
deleteAtSpecifiedNode\n7: display\n8: exit\n");
while(1){
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
BATCH-B1 PAGE | 50
SUBJECT : DS SEM-3 23012011133
case 1:
printf("Enter a value to insert: ");
scanf("%d",&val);
insertAtStart(val);
break;
case 2:
printf("Enter a value to insert: ");
scanf("%d",&val);
insertAtEnd(val);
break;
case 3:
printf("Enter a value to insert: ");
scanf("%d",&val);
insertAtSpecifiedNode(val);
break;
case 4:
deleteAtStart();
break;
case 5:
deleteAtEnd();
break;
case 6:
deleteAtSpecifiedNode();
break;
case 7:
display();
break;
case 8:
exit(0);
default:
printf("Select Correct Option!\n");
}
}
}
Output :
BATCH-B1 PAGE | 51
SUBJECT : DS SEM-3 23012011133
Practical - 7
1) Implement various sorting algorithms:
i) Selection Sorting
Code :
#include <stdio.h>
void main(){
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
int temp;
BATCH-B1 PAGE | 52
SUBJECT : DS SEM-3 23012011133
if(min != i){
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
Output :
void main(){
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
int temp;
int flag = 0;
BATCH-B1 PAGE | 53
SUBJECT : DS SEM-3 23012011133
if(flag == 0){
break;
}
}
Output :
void main(){
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
int temp;
int flag = 0;
BATCH-B1 PAGE | 54
SUBJECT : DS SEM-3 23012011133
j--;
}
a[j+1] = temp;
}
Output :
void main(){
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
int temp;
BATCH-B1 PAGE | 55
SUBJECT : DS SEM-3 23012011133
Output :
v) Merge Sort :
Code :
#include <stdio.h>
void merge(int v[], int lb, int mid, int ub, int u[]) {
int i = lb;
int j = mid + 1;
int k = lb;
BATCH-B1 PAGE | 56
SUBJECT : DS SEM-3 23012011133
int main() {
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
mergeSort(a, 0, n - 1);
printf("Merge Sorting: ");
for (int k = 0; k < n; k++) {
printf("%d ", a[k]);
}
return 0;
}
Output :
BATCH-B1 PAGE | 57
SUBJECT : DS SEM-3 23012011133
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
heapSort(a, n);
BATCH-B1 PAGE | 58
SUBJECT : DS SEM-3 23012011133
return 0;
}
Output :
BATCH-B1 PAGE | 59
SUBJECT : DS SEM-3 23012011133
if(lb<ub){
int loc = part(c,lb,ub);
quickSort(c,lb,loc-1);
quickSort(c,loc+1,ub);
}
}
void main() {
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
quickSort(a, 0, n - 1);
Output :
BATCH-B1 PAGE | 60
SUBJECT : DS SEM-3 23012011133
int main() {
int a[] = {4, 2, 2, 8, 3, 3, 1};
int n = 7;
int b[n];
int m = maxValue(a, n);
int count[m + 1];
return 0;
}
Output :
BATCH-B1 PAGE | 61
SUBJECT : DS SEM-3 23012011133
BATCH-B1 PAGE | 62
SUBJECT : DS SEM-3 23012011133
a[x] = b[x];
}
}
int main() {
int a[] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
printf("Radix Sorting:\n");
radixSort(a, n);
return 0;
}
Output :
Practical-8
(1) Write C Programs to Implement following operations of Binary
Search Tree: 1. Insert 2. Delete 3. Create
BATCH-B1 PAGE | 63
SUBJECT : DS SEM-3 23012011133
Code :
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
BATCH-B1 PAGE | 64
SUBJECT : DS SEM-3 23012011133
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}
struct Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
void inorderTraversal(struct Node* root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
int main() {
struct Node* root = NULL;
int choice, value;
printf("\nBinary Search Tree Operations:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display (Inorder Traversal)\n");
printf("4. Exit\n");
while (choice != 4){
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
BATCH-B1 PAGE | 65
SUBJECT : DS SEM-3 23012011133
case 3:
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Output:
Practical-9:
Write C Program to implement searching algorithms for the
following:
BATCH-B1 PAGE | 66
SUBJECT : DS SEM-3 23012011133
1) SequentialSearch( ):
Code :
#include <stdio.h>
int main()
{
int a[] = {28,36,17,23,24,18,7,8,45};
int flag = 0;
int len = sizeof(a)/sizeof(a[0]);
int val;
printf("Enter a value to search:");
scanf("%d",&val);
for(int i = 0; i < len; i++){
if(a[i] == val){
flag = 1;
}
}
if(flag == 1){
printf("Element found!");
}else{
printf("Element not found");
}
}
Output :
2) BinarySearch( ) :
Code :
#include <stdio.h>
int binarySearch(int d[], int beg,int end,int b){
int m;
while(beg <= end){
m = (beg + end)/2;
if(d[m] == b){
return m + 1;
BATCH-B1 PAGE | 67
SUBJECT : DS SEM-3 23012011133
}
else if(d[m] < b){
return binarySearch(d,m+1,end,b);
}
else if(d[m] > b){
return binarySearch(d,beg,m-1,b);
}
}
return -1;
}
int main()
{
int a[] = {3,7,10,18,23,25,33,38,45,47,50};
int n = sizeof(a)/sizeof(a[0]);
int v;
printf("Enter the value to search: ");
scanf("%d",&v);
int r = binarySearch(a,0,n-1,v);
if (r == -1) {
printf("Element not found!\n");
} else {
printf("Element found at position %d\n", r);
}
return 0;
}
Output :
Practical-10
1) Write C Programs to Implement Hash Tables.
Code :
#include <stdio.h>
BATCH-B1 PAGE | 68
SUBJECT : DS SEM-3 23012011133
#include <stdlib.h>
#define TABLE_SIZE 10
#define EMPTY -1
int hashTable[TABLE_SIZE];
void initializeTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable[i] = EMPTY;
}
}
hashTable[index] = key;
printf("Key %d inserted at index %d.\n", key, index);
}
BATCH-B1 PAGE | 69
SUBJECT : DS SEM-3 23012011133
}
index = (index + 1) % TABLE_SIZE;
if (index == startIndex) {
break;
}
}
void display() {
printf("Hash Table:\n");
for (int i = 0; i < TABLE_SIZE; i++) {
if (hashTable[i] == EMPTY) {
printf("Index %d: EMPTY\n", i);
} else {
printf("Index %d: %d\n", i, hashTable[i]);
}
}
}
int main() {
int choice, key;
BATCH-B1 PAGE | 70
SUBJECT : DS SEM-3 23012011133
initializeTable();
printf("Hash Table Operations:\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
while(choice != 5){
switch (choice) {
case 1:
printf("Enter key to insert: ");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Enter key to search: ");
scanf("%d", &key);
search(key);
break;
case 3:
printf("Enter key to delete: ");
scanf("%d", &key);
deleteKey(key);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
BATCH-B1 PAGE | 71
SUBJECT : DS SEM-3 23012011133
Output :
BATCH-B1 PAGE | 72