Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Program_print -CPDS lab

The document contains multiple C programs demonstrating various data structures and algorithms, including structures for student records, pointer manipulation, file operations, and implementations of lists, stacks, queues, and linked lists. Each program includes user interaction for input and output, showcasing operations like insertion, deletion, and display. The programs are designed for educational purposes to illustrate fundamental programming concepts.

Uploaded by

dhaarani0507
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program_print -CPDS lab

The document contains multiple C programs demonstrating various data structures and algorithms, including structures for student records, pointer manipulation, file operations, and implementations of lists, stacks, queues, and linked lists. Each program includes user interaction for input and output, showcasing operations like insertion, deletion, and display. The programs are designed for educational purposes to illustrate fundamental programming concepts.

Uploaded by

dhaarani0507
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Exp3

Program For Structure:


#include<stdio.h>
#include<conio.h>
struct student
{
int regno,mark1,mark2,mark3,total;
char name[20];
};
int main()
{
struct student s1[10];
int i,n;
printf(“Enter number of students”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“Enter the student register no”);
scanf(“%d”,&s1[i].regno);
printf(“Enter the student name”);
scanf(“%s”,&s1[i].name);
printf(“Enter the student mark1”);
scanf(“%d”,&s1[i].mark1);
printf(“Enter the student mark2”);
scanf(“%d”,&s1[i].mark2);
printf(“Enter the student mark3”);
scanf(“%d”,&s1[i].mark3);
s1[i].total=s1[i].mark1+s1[i].mark2+s1[i].mark3;
}
printf(“\n Registerno \t Name\t Mark1\t Mark2\t Mark3\t Total”);
for(i=1;i<=n;i++)
{
printf(“\n%d\t%s\t%d\t%d\t%d\t%d”,s1[i].regno,s1[i].name,s1[i].mark1,
s1[i].mark2,s1[i].mark3,s1[i].total);
}
getch();
return 0;
}
Exp3
Program For Pointer:
#include <stdio.h>
#include <conio.h>
void swap(int *, int *);
int main()
{
int a, b, *p1, *p2;
printf("Enter value for a and b: ");
scanf("%d %d", &a, &b);
p1 = &a;
p2 = &b;
printf("The values before swapping: %d:%d\n", a, b);
swap(p1, p2);
getch();
return 0;
}
void swap(int *p1, int *p2)
{
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
printf("The values after swapping: %d:%d\n", *p1, *p2);
}
Exp-4
Program for Copy a File:
#include <stdio.h>
#include<stdlib.h>
int main()
{
FILE *sourceFile, *destFile;
char sourceFileName[100], destFileName[100];
char ch;
printf("Enter the source file name: ");
scanf("%s",sourceFileName);
printf("Enter the destination file name: ");
scanf("%s",destFileName)
;
sourceFile = fopen(sourceFileName, "r");
if (sourceFile == NULL)
{
printf("Error: Could not open source file %s.\n", sourceFileName);
exit(1);
}
destFile = fopen(destFileName, "w");
if (destFile == NULL)
{
printf("Error: Could not open destination file %s.\n", destFileName);
fclose(sourceFile);
exit(1);
}
while ((ch = fgetc(sourceFile)) != EOF)
{
fputc(ch, destFile);
}
printf("File copied successfully from %s to %s.\n", sourceFileName, destFileName);
fclose(sourceFile);
fclose(destFile);
return 0;
}
Exp-4
Program for Creating a NewFile

#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp1,*fp2;int i,n,a[10],b;
fp1=fopen("file1.txt","w");
printf("enter number of elements");scanf("%d",&n);
printf("enter the values");for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
fprintf(fp1,"\n%d",a[i]);
}
fclose(fp1);
printf(“The file contents are \n”);fp2=fopen("file1.txt","r");
while(!feof(fp2))
{
fscanf(fp2,"%d",&b);
printf("\n%d",b);
}
fclose(fp2);getch();
return 0;
}
Exp-5
Program for Tic Tac Toe game
#include <stdio.h>
char board[3][3];
char currentPlayer;
void initializeBoard()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
}
void displayBoard()
{
printf("\n");
for (int i = 0; i < 3; i++)
{
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i < 2)
{
printf("---|---|---\n");
}
}
printf("\n");
}
int checkWin()
{
for (int i = 0; i < 3; i++)
{
if ((board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] ==
currentPlayer) ||
(board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer))
{
return 1;
}}
if ((board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] ==
currentPlayer) ||
(board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer))
{
return 1;
}
return 0;
}
int checkTie()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] == ' ')
{
return 0;
}} }
return 1;
}
int main()
{
int row, col;
int moves = 0;
initializeBoard();
currentPlayer = 'X';
while (1)
{
displayBoard();
printf("Player %c, enter your move (row and column): ", currentPlayer);
scanf("%d %d", &row, &col);
if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') {
printf("Invalid move. Try again.\n");
continue; }
board[row][col] = currentPlayer;
moves++;
if (checkWin())
{
displayBoard();
printf("Player %c wins!\n", currentPlayer);
break;
}
if (checkTie())
{
displayBoard();
printf("It's a tie!\n");
break;
}
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
return 0;
}
Exp-6
Program for Array Implementation of list
#include<stdio.h>
#include<conio.h>
#define max 30
int delete1(int a[],int n, int pos);
void printlist(int n,int a[]);
void insert1(int a[],int n,int x,int pos);
int main()
{
int i,a[10],n,pos,ch,x,c,num;
printf("Creation of a List\n");
printf("Enter Number of elements");
scanf("%d",&n);
printf("Enter The Elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
do
{
printf("\n1.Insert\n2:Delete\n3:PrintList\n");
printf("\nEnter your Choice 1/2/3 :");
scanf("%d",&ch);
switch(ch)
case 1:
printf("Enter the element to insert:");
scanf("%d",&x);
printf("\nEnter the position to insert:");scanf("%d",&pos);
insert1(a,n,x,pos);
break;
case 2:
printf("\nEnter the position to delete:");
scanf("%d",&pos);
num=delete1(a,n,pos);
printf("\nThe deleted element is : %d",num);
break;
case 3
:printlist(n,a);
break;
}
printf("\nDo you want to continue(0/1) :");
scanf("%d",&c);
}
while(c==1);
getch();
return 0;
}
void insert1(int a[],int n,int x,int pos)
{
int i;
if(n==max-1)
printf("\nThe list is full cant insert");
}
else
{
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=x;n=n+1;
}
}
int delete1(int a[],int n, int pos)
{
int i,d;
if(n==0)
{
printf("\nList is empty and cannot delete");
}
else
{
d=a[pos];
for(i=pos;i<=n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
return d;
}
}
void printlist(int n,int a[])
{
int i;
if(n==0)
{printf("\nlist is empty");
}else
{for(i=0;i<n;i++){
printf("\n%d",a[i]);
}}}
Ex-7
Program for Array Implementation of Stack:
#include<stdio.h>
#include<conio.h>
#define MAX 20
int st[MAX],top=-1;
int isfull();
int isempty();
void push(int st[],int x);
int pop(int st[]);
void displaytop(int st[]);
void display(int st[]);
int main()
{
int ch,c,x;
do{
printf("\n1:Push\n2:Pop\n3:Displaytop\n4:Display\n");
printf("Enter your Choice 1/2/3/4 : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element to insert");
scanf("%d",&x);
push(st,x);
break;
case 2:
x=pop(st);
printf("The deleted Element is : %d",x);
break;
case 3:
displaytop(st);
break;
case 4:
display(st);
break;
}
printf("\n Do you want to continue 0/1 :");
scanf("%d",&c);
}
while(c==1);
getch();
return 0;
}
int isempty()
{
if(top == -1)
return(1);
else
return(0);
}
int isfull(){
if(top==MAX-1)
return(1);
else return(0);
}
void push(int st[],int x){
if(isfull())
printf("stack is full");
else
{
top=top+1;
st[top]=x;
}}
int pop(int st[])
{
int x;
if(isempty())
{
printf("stack is empty");
}
else{
x=st[top];
top=top-1;}
return(x);}
void displaytop(int st[])
{
if(isempty()){
printf("stack is empty");
}
else
printf("\nThe Top Element is :%d",st[top]);
}
void display(int st[]){
int i;
if(isempty()){
printf("stack is empty");
}
else{
for(i=0;i<=top;i++)
printf("\n%d",st[i]);
}
Exp-7
Program for Array Implementation of Queue
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
int q[MAX],front=-1,rear=-1;
void enqueue(int q[],int x);
void dequeue(int q[]);
void display(int q[]);
int main()
{
int ch,c,x;
do
{
printf("\n1:Enqueue\n2:Dequeue\n3:Display\n");
printf("Enter your Choice 1/2/3 : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the element to insert");
scanf("%d",&x);
enqueue(q,x);
break;
case 2:
dequeue(q);
break;
case 3:
display(q);
break;
}
printf("\n Do you want to continue 0/1 :");
scanf("%d",&c);
}
while(c==1);
getch();
return 0;
}
void enqueue(int q[],int x)
{
if(rear==MAX-1)
printf("\n queue is full");
else if(front==-1 && rear ==-1)
rear=front=0;
else rear=rear+1;
q[rear]=x;
}
void dequeue(int q[])
{
if(front==-1 || front > rear)
printf("queue is empty");
else
{
printf("\nThe Deleted Element is : %d\n", q[front]);
front = front+1;
}
}
void display(int q[])
{
int i; for(i=front;i<=rear;i++)
printf("\n%d",q[i]);
}
Ex-8
Program for Linked List implementation of list:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *insertatbeg(struct node *head, int x);
struct node *insertatend(struct node *head, int x);
struct node *insertatpos(struct node *head, int x, int pos);
struct node *deleteatend(struct node *head);
struct node *deleteatbeg(struct node *head);
struct node *deleteatpos(struct node *head, int pos);
void display(struct node *head);
int main()
{
int ch, c, x, pos;
do
{
printf("\n1. Insert at Beginning\n2. Insert at End\n3. Insert at Position\n4. Delete at
Beginning\n5. Delete at End\n6. Delete at Position\n7. Display\n");
printf("Enter your Choice (1/2/3/4/5/6/7): ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the element to insert: ");
scanf("%d", &x);
head = insertatbeg(head, x);
break;
case 2:
printf("\nEnter the element to insert: ");
scanf("%d", &x);
head = insertatend(head, x);
break;
case 3:
printf("\nEnter the element to insert: ");
scanf("%d", &x);
printf("Enter the position after which to insert: ");
scanf("%d", &pos);
head = insertatpos(head, x, pos);
break;
case 4:
head = deleteatbeg(head);
break;
case 5:
head = deleteatend(head);
break;
case 6:
printf("\nEnter the position from which to delete: ");
scanf("%d", &pos);
head = deleteatpos(head, pos);
break;
case 7:
display(head);
break;
default:
printf("Invalid choice! Please try again.\n");
}
printf("\nDo you want to continue (0/1): ");
scanf("%d", &c);
}
while (c == 1);
return 0;
}
struct node *insertatbeg(struct node *head, int x)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = x;
newnode->next = head;
head = newnode;
return head;
}
struct node *insertatend(struct node *head, int x) {
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = x;
newnode->next = NULL;
if (head == NULL)
{
head = newnode;
}
else
{
struct node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode; // Link the last node to the new node
}
return head;
}
struct node *insertatpos(struct node *head, int x, int pos) {
struct node *newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = x;
if (pos == 0)
{
newnode->next = head;
head = newnode;
return head;
}
struct node *temp = head;
for (int i = 0; temp != NULL && i < pos - 1; i++)
{
temp = temp->next;
}
if (temp == NULL)
{
printf("Position out of bounds!\n");
free(newnode);
return head;
}
newnode->next = temp->next;
temp->next = newnode;
return head;
}

struct node *deleteatbeg(struct node *head)


{
if (head == NULL)
{
printf("List is empty. Cannot delete.\n");
return head;
}
struct node *temp = head;
head = head->next;
free(temp);
return head;
}
struct node *deleteatend(struct node *head)
{
if (head == NULL)
{
printf("List is empty. Cannot delete.\n");
return head;
}
if (head->next == NULL) {
free(head);
return NULL; }
struct node *temp = head;
while (temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
return head;
}
struct node *deleteatpos(struct node *head, int pos) {
if (head == NULL) {
printf("List is empty. Cannot delete.\n");
return head;
}
if (pos == 0) {
return deleteatbeg(head);
}
struct node *temp = head;
for (int i = 0; temp != NULL && i < pos - 1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
printf("Position out of bounds!\n");
return head;
}
struct node *nodeToDelete = temp->next;
temp->next = nodeToDelete->next;
free(nodeToDelete);
return head;}
void display(struct node *head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct node *temp = head;
printf("Elements in the list: ");
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
Ex-9
Program for Application of list
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int coeff;
int pow;
struct Node* next;
};
struct Node* createNode(int coeff, int pow)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coeff = coeff;
newNode->pow = pow;
newNode->next = NULL;
return newNode;
}
void insertNode(struct Node** poly, int coeff, int pow)
{
struct Node* newNode = createNode(coeff, pow);
if (*poly == NULL)
{
*poly = newNode;
}
else
{
struct Node* temp = *poly;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
}
void displayPoly(struct Node* poly)
{
while (poly != NULL)
{
printf("%dx^%d", poly->coeff, poly->pow);
if (poly->next != NULL)
{
printf(" + ");
}
poly = poly->next;
}
printf("\n");
}
struct Node* addPolynomials(struct Node* poly1, struct Node* poly2) {
struct Node* result = NULL;
while (poly1 != NULL && poly2 != NULL)
{
if (poly1->pow > poly2->pow) {
insertNode(&result, poly1->coeff, poly1->pow);
poly1 = poly1->next;
}
else if (poly1->pow < poly2->pow)
{
insertNode(&result, poly2->coeff, poly2->pow);
poly2 = poly2->next;
}
else
{
int sumCoeff = poly1->coeff + poly2->coeff;
if (sumCoeff != 0) {
insertNode(&result, sumCoeff, poly1->pow);
}
poly1 = poly1->next;
poly2 = poly2->next;
}
}
while (poly1 != NULL)
{
insertNode(&result, poly1->coeff, poly1->pow);
poly1 = poly1->next;
}
while (poly2 != NULL) {
insertNode(&result, poly2->coeff, poly2->pow);
poly2 = poly2->next;}
return result;}
int main()
{
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* result = NULL;
insertNode(&poly1, 3, 3);
insertNode(&poly1, 2, 2);
insertNode(&poly1, 1, 0);
insertNode(&poly2, 4, 3);
insertNode(&poly2, 1, 1);
insertNode(&poly2, 2, 0);
printf("First Polynomial: ");
displayPoly(poly1);
printf("Second Polynomial: ");
displayPoly(poly2);
result = addPolynomials(poly1, poly2);
printf("Resultant Polynomial after addition: ");
displayPoly(result);
return 0;
}
Ex-9
Program for Application of stack
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
struct Stack
{
int top;
char arr[MAX];
};
void initStack(struct Stack* stack)
{
stack->top = -1;
}
void push(struct Stack* stack, char c)
{
if (stack->top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
stack->arr[++(stack->top)] = c;
}
char pop(struct Stack* stack)
{
if (stack->top == -1)
{
printf("Stack Underflow\n");
return -1;
}
return stack->arr[(stack->top)--];
}
char peek(struct Stack* stack)
{
return stack->arr[stack->top];
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
int precedence(char ch)
{
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int isOperand(char ch)
{
return isalnum(ch);
}
void infixToPostfix(char* infix, char* postfix)
{
struct Stack stack;
initStack(&stack);
int k = 0;
for (int i = 0; infix[i] != '\0'; i++)
{
if (isOperand(infix[i]))
{
postfix[k++] = infix[i];
}
else if (infix[i] == '(')
{
push(&stack, infix[i]);
}
else if (infix[i] == ')')
{
while (!isEmpty(&stack) && peek(&stack) != '(')
{
postfix[k++] = pop(&stack);
}
pop(&stack);
}
else {
while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(infix[i]))
{
postfix[k++] = pop(&stack);
}
push(&stack, infix[i]);
}}
while (!isEmpty(&stack))
{
postfix[k++] = pop(&stack);
}
postfix[k] = '\0';
}
int main()
{
char infix[MAX],postfix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
Ex-10
Program for Implementation of Binary tree:

#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct Node* insertNode(struct Node* root, int data)
{
if (root == NULL)
{
return createNode(data);
}
struct Node* temp;
struct Node* queue[100];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear)
{
temp = queue[front++];
if (temp->left == NULL)
{
temp->left = createNode(data);
break;
}
else
{
queue[rear++] = temp->left;
}
if (temp->right == NULL)
{
temp->right = createNode(data);
}
else
{
queue[rear++] = temp->right;
}
}
return root;
}
void inorderTraversal(struct Node* root)
{
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
void preorderTraversal(struct Node* root)
{
if (root == NULL) return;
printf("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct Node* root)
{
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
int main()
{
struct Node* root = NULL;
int choice, data, n;
printf("Binary Tree Operations:\n");
printf("1. Insert Node\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. Exit\n");
do {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the number of nodes to insert: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Enter data for node %d: ", i + 1);
scanf("%d", &data);
root = insertNode(root, data);
}
break;
case 2:
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
}
while (choice != 5);
return 0;
}
Ex-11
Program For Binary Search tree
#include <stdio.h>
#include <stdlib.h>
struct bstnode
{
int data;
struct bstnode *left;
struct bstnode *right;
};
struct bstnode *insert(struct bstnode *, int);
struct bstnode *delete1(struct bstnode *, int);
void find(struct bstnode *, int);
struct bstnode *findmin(struct bstnode *);
struct bstnode *findmax(struct bstnode *);
void print(struct bstnode *);
int main()
{
int ch, x;
struct bstnode *t = NULL; // Initialize the root to NULL.
struct bstnode *t1;
printf("Enter the value of the root node: ");
t = (struct bstnode *)malloc(sizeof(struct bstnode));
scanf("%d", &t->data);
t->left = NULL;
t->right = NULL;
while (1)
{
printf("\n1. Insertion\n2. Deletion\n3. Find\n4. Find Min\n5. Find Max\n6. Print\n7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter the data to be inserted: ");
scanf("%d", &x);
t = insert(t, x);
print(t);
break;
case 2:
printf("Enter the data to be removed: ");
scanf("%d", &x);
t = delete1(t, x);
print(t);
break;
case 3:
printf("Enter the data to be searched: ");
scanf("%d", &x);
find(t, x);
break;
case 4:
t1 = findmin(t);
if (t1 != NULL)
printf("The minimum value is %d\n", t1->data);
else
printf("The tree is empty.\n");
break;
case 5:
t1 = findmax(t);
if (t1 != NULL)
printf("The maximum value is %d\n", t1->data);
else
printf("The tree is empty.\n");
break;
case 6:
print(t);
break;
case 7:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
struct bstnode *insert(struct bstnode *t, int x)
{
if (t == NULL)
{
t = (struct bstnode *)malloc(sizeof(struct bstnode));
t->data = x;
t->left = NULL;
t->right = NULL;
}
else if (x < t->data)
{
t->left = insert(t->left, x);
}
else if (x > t->data)
{
t->right = insert(t->right, x);
}
return t;
}
struct bstnode *delete1(struct bstnode *t, int x)
{
struct bstnode *tmp;
if (t == NULL)
{
printf("The element is not present in the tree.\n");
}
else if (x < t->data)
{
t->left = delete1(t->left, x);
}
else if (x > t->data)
{
t->right = delete1(t->right, x);
}
else
{
if (t->left != NULL && t->right != NULL)
{
tmp = findmin(t->right);
t->data = tmp->data;
t->right = delete1(t->right, tmp->data);
}
else
{
tmp = t;
if (t->left == NULL)
{
t = t->right;
}
else if (t->right == NULL)
{
t = t->left;
}
free(tmp);
}
}
return t;
}
void find(struct bstnode *t, int x)
{
if (t == NULL)
{
printf("The element is not present in the tree.\n");
return;
}
if (x < t->data)
{
find(t->left, x);
} else if (x > t->data)
{
find(t->right, x);
} else
{
printf("The element is present in the tree.\n");
}
}
struct bstnode *findmin(struct bstnode *t)
{
if (t == NULL)
return NULL;
while (t->left != NULL)
{
t = t->left;
}
return t;
}

struct bstnode *findmax(struct bstnode *t) {


if (t == NULL) return NULL;
while (t->right != NULL) {
t = t->right;
}
return t;
}
void print(struct bstnode *t)
{
if (t == NULL) return;
print(t->left);
printf("%d\t", t->data);
print(t->right);
}
Ex-12
Program for Linear Search
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int a[20], n, i, x, low, high, mid, flag = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements in sorted order:\n");
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &a[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &x);
low = 0;
high = n - 1;
while (low <= high)
{
mid = low + (high - low) / 2;
if (x == a[mid])
{
printf("The element is present at index %d\n", mid);
flag = 1;
break;
}
else if (x < a[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
if (flag == 0)
{
printf("The element is not present in the list\n");
}
return 0;
}
Ex-12
Program for Binary Search
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[20], n, i, x, flag = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements one by one:\n");
for (i = 0; i < n; i++)
{
printf("Enter element %d: ", i + 1);
scanf("%d", &a[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &x);
for (i = 0; i < n; i++)
{
if (a[i] == x)
{
printf("The element is present at index %d\n", i);
flag = 1;
break;
}
}
if (flag == 0)
{
printf("The element is not present in the list\n");
}
return 0;
}
Ex-13
Program for Insertion sort
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10], n, i, p, j, tmp;
printf("Enter the number of elements in the array (max 10): ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter array element %d: ", i + 1);
scanf("%d", &a[i]);
}
for (p = 1; p < n; p++)
{
tmp = a[p];
j = p - 1;
while (j >= 0 && a[j] > tmp)
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = tmp;
}
printf("Array Elements after sorting:\n");
for (i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
return 0;
}
Ex-13
Program for Quick sort

#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Ex-13
Program for Merge sort
#include <stdio.h>
void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right)
{
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
mergeSort(arr, 0, n - 1);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Ex-14
Program for Linear Probing
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
int hashtable[10], i, x, flag;
for (i = 0; i < 10; i++)
{
hashtable[i] = -1;
}
printf("Enter the numbers to be stored in the hashtable, enter -1 to terminate\n");
while (1)
{
printf("Enter the number to be entered into the hashtable: ");
scanf("%d", &x);
if (x == -1) break;
flag = 0;
int i = x % 10;
while (flag == 0)
{
if (hashtable[i] == -1)
{
hashtable[i] = x;
flag = 1;
}
else
{
i = (i + 1) % 10;
}
}
}
printf("The elements of the hashtable are:\n");
for (i = 0; i < 10; i++)
{
if (hashtable[i] != -1)
{
printf("%d\t", hashtable[i]);
}
}
getch();
}

You might also like