0% found this document useful (0 votes)
1 views17 pages

PracticalFile Final

The document contains source code for various programming tasks in C, including matrix transposition, stack operations, postfix expression evaluation, queue operations, circular queue implementation, singly linked list operations, sorting algorithms, and binary search. Each task is accompanied by sample output demonstrating its functionality. The author of the code is Lavi Singh, a student in BCA 2nd Sem.

Uploaded by

lavosingh209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views17 pages

PracticalFile Final

The document contains source code for various programming tasks in C, including matrix transposition, stack operations, postfix expression evaluation, queue operations, circular queue implementation, singly linked list operations, sorting algorithms, and binary search. Each task is accompanied by sample output demonstrating its functionality. The author of the code is Lavi Singh, a student in BCA 2nd Sem.

Uploaded by

lavosingh209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

WAP to Transpose a Matrix using 2D Arrays

Source code:-

#include <stdio.h>

int main() {
int matrix[3][3], transpose[3][3];
int i, j;

printf("Enter elements of 3x3 matrix:\n");


for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &matrix[i][j]);

for(i = 0; i < 3; i++)


for(j = 0; j < 3; j++)
transpose[j][i] = matrix[i][j];

printf("\nOriginal Matrix:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}

printf("\nTranspose Matrix:\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%4d", transpose[i][j]);
printf("\n");
}

printf("\nName: Lavi Singh\n");


printf("Class: BCA 2nd Sem\n");

return 0;
}

Sample Output:-

Enter elements of 3x3 matrix:


123456789

Original Matrix:
1 2 3
4 5 6
7 8 9

Transpose Matrix:
1 4 7
2 5 8
3 6 9

Name: Lavi Singh


Class: BCA 2nd Sem
2. WAP to Push and Pop Operation on Stack using Array

Source code:-

#include <stdio.h>
#define MAX 5

int stack[MAX], top = -1;

void push(int val) {


if(top == MAX - 1)
printf("Stack Overflow!\n");
else {
top++;
stack[top] = val;
printf("%d pushed to stack\n", val);
}
}

void pop() {
if(top == -1)
printf("Stack Underflow!\n");
else {
printf("%d popped from stack\n", stack[top]);
top--;
}
}

void display() {
int i;
if(top == -1) { printf("Stack is empty\n"); return; }
printf("Stack: ");
for(i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}

int main() {
push(10); push(20); push(30);
display();
pop();
display();
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-

10 pushed to stack
20 pushed to stack
30 pushed to stack
Stack: 30 20 10
30 popped from stack
Stack: 20 10

Name: Lavi Singh


Class: BCA 2nd Sem
3. WAP to Evaluate Postfix Expression using Stack

Source code:-

#include <stdio.h>
#include <ctype.h>
#define MAX 20

int stack[MAX], top = -1;

void push(int val) { stack[++top] = val; }


int pop() { return stack[top--]; }

int evaluatePostfix(char* expr) {


int i, op1, op2;
for(i = 0; expr[i] != '\0'; i++) {
if(isdigit(expr[i]))
push(expr[i] - '0');
else {
op2 = pop();
op1 = pop();
switch(expr[i]) {
case '+': push(op1 + op2); break;
case '-': push(op1 - op2); break;
case '*': push(op1 * op2); break;
case '/': push(op1 / op2); break;
}
}
}
return pop();
}

int main() {
char expr[] = "53+82-*";
printf("Postfix Expression: %s\n", expr);
printf("Result: %d\n", evaluatePostfix(expr));
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-

Postfix Expression: 53+82-*


Result: 48

Name: Lavi Singh


Class: BCA 2nd Sem
4. WAP for Queue Operations - Enqueue and Dequeue

Source code:-

#include <stdio.h>
#define MAX 5

int queue[MAX], front = -1, rear = -1;

void enqueue(int val) {


if(rear == MAX - 1)
printf("Queue is Full!\n");
else {
if(front == -1) front = 0;
rear++;
queue[rear] = val;
printf("%d enqueued\n", val);
}
}

void dequeue() {
if(front == -1 || front > rear)
printf("Queue is Empty!\n");
else {
printf("%d dequeued\n", queue[front]);
front++;
}
}

void display() {
int i;
if(front == -1 || front > rear) { printf("Queue Empty\n"); return; }
printf("Queue: ");
for(i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}

int main() {
enqueue(10); enqueue(20); enqueue(30);
display();
dequeue();
display();
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-

10 enqueued
20 enqueued
30 enqueued
Queue: 10 20 30
10 dequeued
Queue: 20 30

Name: Lavi Singh


Class: BCA 2nd Sem
5. WAP to Implement Circular Queue

Source code:-

#include <stdio.h>
#define MAX 5

int cq[MAX], front = -1, rear = -1;

void enqueue(int val) {


if((rear + 1) % MAX == front)
printf("Circular Queue is Full!\n");
else {
if(front == -1) front = 0;
rear = (rear + 1) % MAX;
cq[rear] = val;
printf("%d inserted\n", val);
}
}

void dequeue() {
if(front == -1)
printf("Circular Queue is Empty!\n");
else {
printf("%d deleted\n", cq[front]);
if(front == rear) { front = rear = -1; }
else front = (front + 1) % MAX;
}
}

void display() {
int i;
if(front == -1) { printf("Queue Empty\n"); return; }
printf("Queue: ");
i = front;
while(1) {
printf("%d ", cq[i]);
if(i == rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}

int main() {
enqueue(10); enqueue(20); enqueue(30); enqueue(40);
display();
dequeue();
display();
enqueue(50);
display();
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-

10 inserted
20 inserted
30 inserted
40 inserted
Queue: 10 20 30 40
10 deleted
Queue: 20 30 40
50 inserted
Queue: 20 30 40 50

Name: Lavi Singh


Class: BCA 2nd Sem
6. WAP to Implement Singly Linked List

Source code:-

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

struct Node {
int data;
struct Node* next;
};

struct Node* head = NULL;

void insert(int val) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if(head == NULL) { head = newNode; return; }
struct Node* temp = head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
}

void display() {
struct Node* temp = head;
printf("Linked List: ");
while(temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

void deleteNode(int val) {


struct Node *temp = head, *prev = NULL;
if(temp != NULL && temp->data == val) { head = temp->next;
free(temp); return; }
while(temp != NULL && temp->data != val) { prev = temp; temp = temp-
>next; }
if(temp == NULL) { printf("Not found\n"); return; }
prev->next = temp->next;
free(temp);
printf("%d deleted\n", val);
}

int main() {
insert(10); insert(20); insert(30); insert(40);
display();
deleteNode(20);
display();
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-

Linked List: 10 -> 20 -> 30 -> 40 -> NULL


20 deleted
Linked List: 10 -> 30 -> 40 -> NULL

Name: Lavi Singh


Class: BCA 2nd Sem
7. WAP to Insert a Node at Beginning and End in Singly Linked List

Source code:-

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

struct Node {
int data;
struct Node* next;
};

struct Node* head = NULL;

void insertAtBeginning(int val) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head;
head = newNode;
printf("%d inserted at beginning\n", val);
}

void insertAtEnd(int val) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if(head == NULL) { head = newNode; printf("%d inserted at end\n",
val); return; }
struct Node* temp = head;
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
printf("%d inserted at end\n", val);
}

void display() {
struct Node* temp = head;
printf("List: ");
while(temp != NULL) { printf("%d -> ", temp->data); temp = temp-
>next; }
printf("NULL\n");
}

int main() {
insertAtEnd(20);
insertAtEnd(30);
insertAtBeginning(10);
insertAtEnd(40);
insertAtBeginning(5);
display();
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}
Sample Output:-

20 inserted at end
30 inserted at end
10 inserted at beginning
40 inserted at end
5 inserted at beginning
List: 5 -> 10 -> 20 -> 30 -> 40 -> NULL

Name: Lavi Singh


Class: BCA 2nd Sem
8. WAP to Implement Bubble Sort

Source code:-

#include <stdio.h>

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


int i, j, temp;
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

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


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

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = 7;
printf("Original Array: ");
printArray(arr, n);
bubbleSort(arr, n);
printf("Sorted Array: ");
printArray(arr, n);
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-

Original Array: 64 34 25 12 22 11 90
Sorted Array: 11 12 22 25 34 64 90

Name: Lavi Singh


Class: BCA 2nd Sem
9. WAP to Implement Binary Search

Source code:-

#include <stdio.h>

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1, mid;
while(low <= high) {
mid = (low + high) / 2;
if(arr[mid] == key)
return mid;
else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

int main() {
int arr[] = {5, 10, 15, 20, 25, 30, 35};
int n = 7, key = 25;
printf("Array: 5 10 15 20 25 30 35\n");
printf("Searching for: %d\n", key);
int result = binarySearch(arr, n, key);
if(result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-

Array: 5 10 15 20 25 30 35
Searching for: 25
Element found at index 4

Name: Lavi Singh


Class: BCA 2nd Sem
10. WAP to Implement Merge Sort

Source code:-

#include <stdio.h>

void merge(int arr[], int l, int m, int r) {


int i, j, k;
int n1 = m - l + 1, n2 = r - m;
int L[n1], R[n2];
for(i = 0; i < n1; i++) L[i] = arr[l + i];
for(j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
i = 0; j = 0; k = l;
while(i < n1 && j < n2) {
if(L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while(i < n1) arr[k++] = L[i++];
while(j < n2) arr[k++] = R[j++];
}

void mergeSort(int arr[], int l, int r) {


if(l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

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


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

int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = 7;
printf("Original Array: ");
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printf("Sorted Array: ");
printArray(arr, n);
printf("\nName: Lavi Singh\n");
printf("Class: BCA 2nd Sem\n");
return 0;
}

Sample Output:-
Original Array: 38 27 43 3 9 82 10
Sorted Array: 3 9 10 27 38 43 82

Name: Lavi Singh


Class: BCA 2nd Sem

You might also like