0% found this document useful (0 votes)
8 views72 pages

Practical 1 (DS)

The document contains a series of practical programming exercises in C, focusing on data structures and algorithms. It includes code examples for array traversal, recursion for factorial calculation, pointer usage, and operations on stacks, as well as handling sparse matrices and expression validation. Each section provides code snippets along with expected outputs for various tasks related to arrays, pointers, and stack implementations.

Uploaded by

XI-A-30- Krish
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)
8 views72 pages

Practical 1 (DS)

The document contains a series of practical programming exercises in C, focusing on data structures and algorithms. It includes code examples for array traversal, recursion for factorial calculation, pointer usage, and operations on stacks, as well as handling sparse matrices and expression validation. Each section provides code snippets along with expected outputs for various tasks related to arrays, pointers, and stack implementations.

Uploaded by

XI-A-30- Krish
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
You are on page 1/ 72

SUBJECT : DS SEM-3 23012011133

Practical-1

1) To traverse elements of an array.


Code :
#include <stdio.h>

void main(){
int a[4] = {1,2,3,4};
int b;

printf("Elements of Array are : \n");


for(int i = 0; i < 4; i++){
printf("a[%d] is %d\n",i,a[i]);
}
}

Output :

2) To calculate the factorial of a number using recursion.


Code :
#include <stdio.h>
int factorial(int a){
if(a == 1 || a == 0){
return 1;
}
else{
return a*factorial(a-1);
}
}
void main(){

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 :

3) To find the address of a variable using pointers.


Code :
#include <stdio.h>

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 :

5) Add two numbers using a pointer and display the sum of


two numbers.
Code :
#include <stdio.h>
void main(){
int a = 5;
int b = 6;
int *ptr1 = &a;
int *ptr2 = &b;

int sum = *ptr1 + *ptr2;


printf("Sum is %d",sum);
}

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]);
}
}

for (int i = 0; i < 4; i++) {


for (int j = 0; j < 5; j++) {
if (a[i][j] != 0) {
count++;
}
}
}

printf("%d\n", count);
int b[count][3];

for (int i = 0; i < 4; i++) {


for (int j = 0; j < 5; j++) {
if (a[i][j] != 0) {
b[c][0] = i;
b[c][1] = j;
b[c][2] = a[i][j];
c++;
}
}
}

BATCH-B1 PAGE | 4
SUBJECT : DS SEM-3 23012011133

for (int i = 0; i < count; i++) {


for (int j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
return 0;
}

Output :

2) Explain the Address Calculation of 1D Array.


Code :
#include <stdio.h>

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 :

3) Explain the Address Calculation of 2D Array using Row


Major Order and Column Major Order.
Code :
#include <stdio.h>
#include <conio.h>

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

}
}

int loc = &arr[0][0];


for(int i = 0; i < a; i++){
for(int j = 0; j < b; j++){
printf("Address of a[%d][%d] is %ld\n",i,j,(loc +
(i*b + j)*sizeof(int)));
}
}

// Column 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]);
}
}

int loc = &arr[0][0];


for(int i = 0; i < a; i++){
for(int j = 0; j < b; j++){
printf("Address of a[%d][%d] is %ld\n",i,j,(loc +
(i + j*a)*sizeof(int)));
}
}
}

Output :

BATCH-B1 PAGE | 7
SUBJECT : DS SEM-3 23012011133

4) Write a C Program to create a 3D array, initialize the 3D


Array then Print the value of the 3D array.
Code :
#include <stdio.h>

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 :

5) Write a C Program to search the element in 2D Array.


Code :
#include <stdio.h>

void main(){
int a[2][2] = {{1,2},{3,4}};
int b;

printf("Enter the value to search: ");


scanf("%d",&b);
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
if(a[i][j] == b){
printf("Value found!");
break;
}
}
}
}

Output :

BATCH-B1 PAGE | 9
SUBJECT : DS SEM-3 23012011133

Practical - 3

1) Implement function of stack with following


operations:
Code :
#include <stdio.h>
#define MAX 100

int stack[MAX];
int top = -1;

void push(int value) {


if (top == MAX - 1) {
printf("STACK OVERFLOW");
} else {
stack[++top] = value;
printf("%d pushed onto stack\n", value);
}
}

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

int isEmpty() { return top == -1; }

int isFull() { return top == MAX - 1; }

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;

void push(char val) {


if (top == MAX - 1) {
printf("Stack Overflow!");
} else {
str[++top] = val;
}
}

char pop() {
if (top == -1) {
printf("Stack Underflow");
} else {
top--;
return str[top + 1];
}
}

BATCH-B1 PAGE | 12
SUBJECT : DS SEM-3 23012011133

int check(char sideStr[], int tar) {


for (int i = tar; sideStr[i] != '\0'; i++) {
if (sideStr[i] != pop()) {
return 0;
}
}
return top == -1;
}

int main() {
char inputStr[40];
int target;
printf("Enter a input String of the format aCb: ");
scanf("%s", inputStr);

for (int i = 0; inputStr[i] != 'C'; i++) {


push(inputStr[i]);
target = i + 2;
}

for (int i = 0; str[i] != '\0'; i++) {


printf("%c\n", str[i]);
}

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

2) Write a program to check the validity of expressions, which


contains multiple opening and closing brackets. (i.e., [{(a+b)
* c} – d]).
Code :
#include <stdio.h>
#define MAX 100

char str[MAX];
int top = -1;

void push(char val) {


if (top == MAX - 1) {
printf("Stack Overflow!");
} else {
str[++top] = val;
}
}

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]);
}
}

for(int i = 0; inputStr[i] != '\0';i++){


if(inputStr[i] == ']' || inputStr[i] == '}' || inputStr[i]
== ')'){
pop();
}
}

if(isEmpty()){
printf("The expression is valid");
}
else{
printf("The expression is not valid");
}
return 0;
}

Output :

3) Write a program to convert unparenthized infix expression


to postfix.
Code :

BATCH-B1 PAGE | 15
SUBJECT : DS SEM-3 23012011133

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

#define MAX 100

int isEmpty(int top) {


return top == -1;
}

int isFull(int top) {


return top == MAX - 1;
}

void push(char *stack, int *top, char item) {


if (isFull(*top)) {
printf("Stack overflow\n");
return;
}
stack[++(*top)] = item;
}

char pop(char *stack, int *top) {


if (isEmpty(*top)) {
printf("Stack underflow\n");
return '\0';
}
return stack[(*top)--];
}

int isOperand(char ch) {


return (ch >= 'a' && ch <= 'z') ;
}

BATCH-B1 PAGE | 16
SUBJECT : DS SEM-3 23012011133

int precedence(char operator) {


switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
}

void infixToPostfix(char *infix, char *postfix) {


char stack[MAX];
int top = -1;
int i, j = 0;

for (i = 0; infix[i] != '\0'; i++) {


if (isOperand(infix[i])) {
postfix[j++] = infix[i];
} else {
while (!isEmpty(top) && precedence(infix[i]) <=
precedence(stack[top])) {
postfix[j++] = pop(stack, &top);
}
push(stack, &top, infix[i]);
}
}

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

printf("Enter an infix expression (without parentheses): ");


scanf("%s",infix);

infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

return 0;
}

Output:

4) Write a program to evaluate the given postfix expression.


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

#define MAX 100

int isOperand(char ch) {


return (ch >= '0' && ch <= '9');
}

int performOperation(int a, int b, char operator) {


switch (operator) {
case '+':
return a + b;
case '-':
return a - b;

BATCH-B1 PAGE | 18
SUBJECT : DS SEM-3 23012011133

case '*':
return a * b;
case '/':
return a / b;
default:
return -1;
}
}

int evaluatePostfix(char *postfix) {


int stack[MAX];
int top = -1;
int i;

for (i = 0; postfix[i] != '\0'; i++) {

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

printf("Enter a postfix expression: ");


scanf("%s",postfix);

int result = evaluatePostfix(postfix);

if (result != -1) {
printf("Result: %d\n", result);
}

return 0;
}

Output :

5) Write a program to find the Fibonacci series using recursion


for the given number.
Code :
#include <stdio.h>

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;

printf("Enter the number of terms: ");


scanf("%d", &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 :

6) Write a program for the Tower of Hanoi problem.


Code :
#include <stdio.h>

void towerOfHanoi(int n, char s, char d, char i) {


if (n == 1) {
printf("Move disk 1 from %c to %c\n", s, d);
return;
}
towerOfHanoi(n - 1, s, i, d);
printf("Move disk %d from %c to %c\n", n, s, d);
towerOfHanoi(n - 1, i, d, s);
}

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

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


front = rear = 0;
}
arr[rear] = m;
rear = rear + 1;
}
void display(){
if(front != -1){
for(int i = front; i < Max; i++){
printf("%d\n",arr[i]);
}
}
}
void delete(){
if(front == -1){
printf("Queue is empty!\n");
return;
}
if(front == rear){
front = rear = -1;
}
printf("The element deleted is %d\n",arr[front]);
front = front + 1;
}
void main(){
while(1){
int n;
printf("Enter a number of your choice : ");
scanf("%d",&n);
switch(n){
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
}

BATCH-B1 PAGE | 23
SUBJECT : DS SEM-3 23012011133

}
}

Output :

BATCH-B1 PAGE | 24
SUBJECT : DS SEM-3 23012011133

2) Write a program to Implement Circular Queue with following


operations:
Code :
#include <stdio.h>
#define m 5
int cqueue[m];
int front = -1;
int rear = -1;

void enqueue(int t){


if(front == -1 && rear == -1){
front = 0;
rear = 0;
cqueue[rear] = t;
}

else if((rear+1)%m == front){


printf("Cqueue is Full !\n");
}

else{
rear = (rear + 1) % m;
cqueue[rear] = t;
}

void dequeue(){
if(front == -1 && rear == -1){
printf("Cqueue is Empty!\n");
}

else if(front == rear){


printf("deleted element is %d\n",cqueue[rear]);
rear = front = -1;
}

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

3) Write a program to Implement Deque with following


operations:
Code :
#include <stdio.h>
#define m 5
int DEqueue[m];
int front = -1;
int rear = -1;
void insertAtFront(int t){
if((front == 0 && rear == m-1) || front == rear + 1){
printf("DEqueue is Full\n");
}
else if(front == -1 && rear == -1){
rear = front = 0;
DEqueue[front] = t;
}
else if(front == 0){
front = m-1;
DEqueue[front] = t;
}
else{
front = front - 1;
DEqueue[front] = t;
}
}
void insertAtRear(int t){
if((rear+1) % m == front){
printf("DEqueue is Full\n");
}
else if(front == -1 && rear == -1){
rear = front = 0;
DEqueue[rear] = t;
}
else if(rear == m-1){
rear = 0;
DEqueue[rear] = t;
}
else{
rear = rear + 1;

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

2) To implement a doubly linked list with above functions


Code:

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;

void create(int t){


temp = (struct node*)malloc(sizeof(struct node));
temp -> data = t;
temp -> next = NULL;
temp -> pre = NULL;
}

void insertAtStart(int t){


create(t);
if(head == NULL){
head = temp;
}

else{
temp -> next = head;
head -> pre = temp;
head = temp;
}
}

void insertAtEnd(int t){


create(t);
if(head == NULL){
head = temp;
}

else{

temp2 = head;
while(temp2 -> next != NULL){

BATCH-B1 PAGE | 40
SUBJECT : DS SEM-3 23012011133

temp2 = temp2 -> next;


}

temp2 -> next = temp;


temp -> pre = temp2;

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

temp -> next = temp2 -> next;


temp -> pre = temp2;
temp2 -> next = temp;
temp -> next -> pre = temp;
}
}

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{

if(head -> next == NULL){


temp = head;
t = temp -> data;
head = NULL;
free(temp);
}

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{

if(head -> next == NULL){


temp = head;
t = temp -> data;
head = NULL;
free(temp);

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 if(head -> next == NULL){


printf("It has only one element!\n");
}

else{
temp = head;
for(int i = 1; i<n; i++){
temp2 = temp;
temp = temp -> next;

if(temp -> next == NULL){


printf("No of nodes is less then required\n");

BATCH-B1 PAGE | 43
SUBJECT : DS SEM-3 23012011133

return;
}
}

temp2 -> next = temp -> next;


temp -> next -> pre = temp2;

t = temp -> data;

printf("So the deleted data is %d\n",t);


free(temp);
}
}

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

3) To implement a circular linked list with above functions.


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;
}
void insertAtStart(int b){
create(b);
if(head == NULL){
head = temp;
temp -> link = head;
}
else{
temp2 = head;
while(temp2 -> link != head){
temp2 = temp2 -> link;
}
temp2 -> link = temp;
temp -> link = head;
head = temp;
}
}
void insertAtEnd(int b){
create(b);
if(head == NULL){
head = temp;
temp -> link = head;
}
else{
temp2 = head;
while(temp2-> link != head){
temp2 = temp2 -> link;

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

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


int min = i;
for(int j = i+1 ; j < n; j++){
if(a[j] < a[min]){
min = j;
}
}

if(min != i){
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}

printf("Selection Sorting: ");


for(int k = 0; k < n; k++){
printf("%d ",a[k]);
}
}

Output :

ii) Bubble 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;
int flag = 0;

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


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

BATCH-B1 PAGE | 53
SUBJECT : DS SEM-3 23012011133

if(a[j] > a[j+1]){


temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
flag = 1;
}
}

if(flag == 0){
break;
}
}

printf("Bubble Sorted Array: ");


for(int k = 0; k < n; k++){
printf("%d ",a[k]);
}
}

Output :

iii) Insertion sort


Code :
#include <stdio.h>

void main(){
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
int temp;
int flag = 0;

for(int i = 1; i < n; i++){


temp = a[i];
int j = i-1;
while(j >= 0 && a[j] > temp ){
a[j + 1] = a[j];

BATCH-B1 PAGE | 54
SUBJECT : DS SEM-3 23012011133

j--;
}
a[j+1] = temp;
}

printf("Insertion Sorting: ");


for(int k = 0; k < n; k++){
printf("%d ",a[k]);
}
}

Output :

iv) Shell Sort :


Code :
#include <stdio.h>

void main(){
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;
int temp;

for(int g = n/2; g > 1; g /= 2){


for(int j = g; j < n; j++){
for(int i = j - g; i>= 0; i -= g){
if(a[i+g] > a[i]){
break;
}else{
temp = a[i+g];
a[i+g] = a[i];
a[i] = temp;
}
}
}
}

printf("Shell Sorting: ");

BATCH-B1 PAGE | 55
SUBJECT : DS SEM-3 23012011133

for(int k = 0; k < n; k++){


printf("%d ",a[k]);
}
}

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;

while (i <= mid && j <= ub) {


if (v[i] < v[j]) {
u[k++] = v[i++];
} else {
u[k++] = v[j++];
}
}

while (i <= mid) {


u[k++] = v[i++];
}

while (j <= ub) {


u[k++] = v[j++];
}

for (int x = lb; x <= ub; x++) {


v[x] = u[x];
}
}

BATCH-B1 PAGE | 56
SUBJECT : DS SEM-3 23012011133

void mergeSort(int e[], int lb, int ub) {


int x[10];
if (lb < ub) {
int mid = (lb + ub) / 2;
mergeSort(e, lb, mid);
mergeSort(e, mid + 1, ub);
merge(e, lb, mid, ub, x);
}
}

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 :

vi) Heap Sort :


Code :
#include <stdio.h>
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest]) {


largest = left;
}

BATCH-B1 PAGE | 57
SUBJECT : DS SEM-3 23012011133

if (right < n && arr[right] > arr[largest]) {


largest = right;
}

if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {

for (int i = n / 2 - 1; i >= 0; i--) {


heapify(arr, n, i);
}

for (int i = n - 1; i > 0; i--) {

int temp = arr[0];


arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
int main() {
int n = 10;
int a[10] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};

heapSort(a, n);

printf("Heap Sorting :\n");


for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");

BATCH-B1 PAGE | 58
SUBJECT : DS SEM-3 23012011133

return 0;
}

Output :

vii) Quick Sort :


Code :
#include <stdio.h>
int part(int x[],int lb, int ub){
int p = x[lb];
int start = lb;
int end = ub;
int temp;
while(start < end){

while(x[start] <= p){


start++;
}
while(x[end] > p){
end--;
}
if(start < end){
temp = x[start];
x[start] = x[end];
x[end] = temp;
}
}
temp = x[lb];
x[lb] = x[end];
x[end]= temp;
return end;
}

void quickSort(int c[],int lb, int ub){

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

printf("Quick Sorting: ");


for (int k = 0; k < n; k++) {
printf("%d ", a[k]);
}
}

Output :

viii) Counting Sort


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

int maxValue(int arr[], int len) {


int max = arr[0];
for (int i = 1; i < len; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}

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

// Initialize count array to 0


for (int i = 0; i <= m; i++) {
count[i] = 0;
}

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


count[a[i]]++;
}

for (int k = 1; k <= m; k++) {


count[k] += count[k - 1];
}

for (int j = n - 1; j >= 0; j--) {


b[count[a[j]] - 1] = a[j];
count[a[j]]--;
}

for (int x = 0; x < n; x++) {


a[x] = b[x];
}

for (int y = 0; y < n; y++) {


printf("%d ", a[y]);
}

return 0;
}

Output :

BATCH-B1 PAGE | 61
SUBJECT : DS SEM-3 23012011133

ix) Radix Sort


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

int maxValue(int arr[], int len) {


int max = arr[0];
for (int i = 1; i < len; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}

void countSort(int a[], int n, int pos) {


int b[n];
int count[10] = {0};

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


int digit = (a[i] / pos) % 10;
count[digit]++;
}

for (int k = 1; k < 10; k++) {


count[k] += count[k - 1];
}

for (int j = n - 1; j >= 0; j--) {


int digit = (a[j] / pos) % 10;
b[count[digit] - 1] = a[j];
count[digit]--;
}

for (int x = 0; x < n; x++) {

BATCH-B1 PAGE | 62
SUBJECT : DS SEM-3 23012011133

a[x] = b[x];
}
}

void radixSort(int a[], int n) {


int m = maxValue(a, n);

for (int pos = 1; m / pos > 0; pos *= 10) {


countSort(a, n, pos);
}
}

int main() {
int a[] = {18, 93, 80, 41, 51, 98, 26, 83, 14, 3};
int n = 10;

printf("Radix Sorting:\n");
radixSort(a, n);

for (int y = 0; y < n; y++) {


printf("%d ", a[y]);
}
printf("\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;
};

struct Node* createNode(int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}
struct Node* findMin(struct Node* root) {
while (root && root->left != NULL) {
root = root->left;
}
return root;
}
struct Node* deleteNode(struct Node* root, int value) {
if (root == NULL) {
return root;
}
if (value < root->data) {
root->left = deleteNode(root->left, value);
} else if (value > root->data) {
root->right = deleteNode(root->right, value);
} else {

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

int hashFunction(int key) {


return key % TABLE_SIZE;
}

void initializeTable() {
for (int i = 0; i < TABLE_SIZE; i++) {
hashTable[i] = EMPTY;
}
}

void insert(int key) {


int index = hashFunction(key);
int startIndex = index;

while (hashTable[index] != EMPTY) {

index = (index + 1) % TABLE_SIZE;


if (index == startIndex) {
printf("Hash table is full. Cannot insert key %d.\n", key);
return;
}
}

hashTable[index] = key;
printf("Key %d inserted at index %d.\n", key, index);
}

void search(int key) {


int index = hashFunction(key);
int startIndex = index;

while (hashTable[index] != EMPTY) {


if (hashTable[index] == key) {
printf("Key %d found at index %d.\n", key, index);
return;

BATCH-B1 PAGE | 69
SUBJECT : DS SEM-3 23012011133

}
index = (index + 1) % TABLE_SIZE;
if (index == startIndex) {
break;
}
}

printf("Key %d not found in the hash table.\n", key);


}

void deleteKey(int key) {


int index = hashFunction(key);
int startIndex = index;

while (hashTable[index] != EMPTY) {


if (hashTable[index] == key) {
hashTable[index] = EMPTY;
printf("Key %d deleted from index %d.\n", key, index);
return;
}
index = (index + 1) % TABLE_SIZE;
if (index == startIndex) {
break;
}
}

printf("Key %d not found in the hash table.\n", key);


}

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){

printf("Enter your choice: ");


scanf("%d", &choice);

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

You might also like