Program_print -CPDS lab
Program_print -CPDS lab
#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;
}
#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;
}
#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();
}