Yordanos moges
Yordanos moges
Yordanos moges
LAB Record
1. 11-09-24 3
Linear search
1. 18-09-24 stack 5
4. 25-09-24 queue 14
if (found == 0) {
printf("Target number %d not found in the array.\n", target);
}
return 0;
}
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("NAME yordanos moges AUD 22880\n");
printf("Enter a number");
scanf("%d",&num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
4. Write a program to implement Stack using array. Implement Push, Pop and
display operations. -Lab 2 Date 18-09-24
#include <stdio.h>
int stack[100];
int top = -1;
int pop() {
if (top == -1) {
printf("Error: Stack underflow!\n");
return -1;
}
return stack[top--];
}
int main() {
int choice, value;
while (1) {
printf("NAME yordanos moges AUD 22880\n");
printf("1. Push\n2. Pop\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
value = pop();
if (value != -1) {
printf("Popped value: %d\n", value);
}
break;
case 3:
return 0;
default:
printf("Invalid choice!\n");
}
}
return 0;
}
int main() {
int num_disks;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of disks: ");
scanf("%d", &num_disks);
towerOfHanoi(num_disks, 'A', 'B', 'C');
return 0;
}
6. Write a program to implement Queue using array, which shows insertion and
deletion operations.-Lab 3 Date 25-09-24
#include<stdio.h>
int main() {
int n, front = -1, rear = -1;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("\nEnd of entry\n");
for (int i = 0; i < n; i++) {
printf("\n%d", a[i]);
}
return 0;
}
7. Write a program to sort the given array using insertion sort -Lab 4 Date 09-10-24
#include <stdio.h>
int main() {
int size;
int array[100];
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
for (int i = 1; i < size; i++) {
int key = array[i];
int j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
8. Write a program to sort the given array using selection sort -Lab 4 Date 09-10-24
#include <stdio.h>
int main() {
int size;
int array[100];
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
for (int i = 0; i < size - 1; i++) {
int min_index = i;
for (int j = i + 1; j < size; j++) {
if (array[j] < array[min_index]) {
min_index = j;
}
}
int temp = array[min_index];
array[min_index] = array[i];
array[i] = temp;
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
9. Write a program to sort the given array using bubble sort -Lab 5 Date 1-11-24
#include <stdio.h>
int main() {
int array[100];
int size;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
10. Write a program to sort the given array using QuickSort -Lab 5 Date 1-11-24
#include <stdio.h>
int main() {
int size;
int array[100];
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
int low = 0;
int high = size - 1;
if (size > 1) {
int stack[size];
int top = -1;
stack[++top] = low;
stack[++top] = high;
while (top >= 0) {
high = stack[top--];
low = stack[top--];
int pivot = array[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
int pi = i + 1;
if (pi - 1 > low) {
stack[++top] = low;
stack[++top] = pi - 1;
}
if (pi + 1 < high) {
stack[++top] = pi + 1;
stack[++top] = high;
}
}
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
11. Write a program to sort the given array using MergeSort -Lab 5 Date 1-11-24
#include <stdio.h>
int main() {
int size;
printf("NAME yordanos moges Aud 22880\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
int array[size];
printf("Enter %d elements: ", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]);
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
array[k] = L[i];
i++;
k++;
}
while (j < n2) {
array[k] = R[j];
j++;
k++;
}
}
}
printf("Sorted array: \n");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char item) {
if (top >= MAX - 1) {
printf("Stack overflow.\n");
return;
}
stack[++top] = item;
}
char pop() {
if (top == -1) {
printf("Stack underflow.\n");
return -1;
}
return stack[top--];
}
int precedence(char op)
{
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}
int isOperator(char ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
int isOperand(char ch)
{
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
}
void infixToPostfix(char* infix)
{
char postfix[MAX];
int i = 0, j = 0;
if (isOperand(ch)) {
postfix[j++] = ch;
}
else if (ch == '(') {
push(ch);
}
else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop();
}
else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = pop();
}
push(ch);
}
i++;
}
while (top != -1)
{
postfix[j++] = pop();
}
postfix[j] = '\0';
printf("Postfix Expression: %s\n", postfix);
}
int main()
{
char infix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix);
return 0;
}
#include <stdio.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char item) {
if (top >= MAX - 1) {
printf("Stack overflow.\n");
return;
}
stack[++top] = item;
}
char pop() {
if (top == -1) {
printf("Stack underflow.\n");
return -1;
}
return stack[top--];
}
int precedence(char op)
{
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
if (op == '^') return 3;
return 0;
}
int isOperator(char ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
int isOperand(char ch)
{
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
}
void infixToPostfix(char* infix)
{
char postfix[MAX];
int i = 0, j = 0;
if (isOperand(ch)) {
postfix[j++] = ch;
}
else if (ch == '(') {
push(ch);
}
else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop();
}
else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = pop();
}
push(ch);
}
i++;
}
while (top != -1)
{
postfix[j++] = pop();
}
postfix[j] = '\0';
printf("Postfix Expression: %s\n", postfix);
}
int main()
{
char infix[MAX];
printf("NAME: Yordanos Moges AUD22880\n");
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
char stack[MAX];
int top = -1;
char pop() {
if (top == -1) {
printf("Stack underflow.\n");
return -1;
}
return stack[top--];
}
strcpy(reversedInfix, infix);
reverseString(reversedInfix);
i = 0;
while (reversedInfix[i] != '\0') {
char ch = reversedInfix[i];
if (isOperand(ch)) {
postfix[j++] = ch;
} else if (ch == '(') {
push(ch);
} else if (ch == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop();
} else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = pop();
}
push(ch);
}
i++;
}
postfix[j] = '\0';
reverseString(postfix);
int main() {
char infix[MAX];
printf("NAME: Yordanos Moges AUD22880\n");
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPrefix(infix);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void create();
void insert();
void deleteNode();
void display();
struct node {
int data;
struct node *next;
};
void create() {
cur = (struct node *)malloc(sizeof(struct node));
if (cur == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("Enter the data: ");
scanf("%d", &cur->data);
cur->next = NULL;
if (first == NULL) {
first = cur;
last = cur;
} else {
last->next = cur;
last = cur;
}
}
void insert() {
int pos, i = 1;
cur = (struct node *)malloc(sizeof(struct node));
if (cur == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("Enter the data: ");
scanf("%d", &cur->data);
printf("Enter the position: ");
scanf("%d", &pos);
if (pos == 1) {
cur->next = first;
first = cur;
} else {
prev = first;
while (i < pos - 1 && prev != NULL) {
prev = prev->next;
i++;
}
if (prev != NULL) {
cur->next = prev->next;
prev->next = cur;
} else {
printf("Invalid position\n");
free(cur);
}
}
}
void deleteNode() {
int pos, i = 1;
printf("Enter the position to delete: ");
scanf("%d", &pos);
if (pos == 1) {
cur = first;
if (first != NULL) {
first = first->next;
free(cur);
} else {
printf("List is empty\n");
}
} else {
cur = first;
while (i < pos && cur != NULL) {
prev = cur;
cur = cur->next;
i++;
}
if (cur != NULL) {
prev->next = cur->next;
free(cur);
} else {
printf("Invalid position\n");
}
}
}
void display() {
if (first == NULL) {
printf("List is empty\n");
} else {
cur = first;
printf("The elements in the list are: ");
while (cur != NULL) {
printf("%d -> ", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
}
int main() {
int ch;
printf("NAME yordanos moges Aud 22880\n");
do {
printf("\nMenu:\n");
printf("1. Create\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
create();
break;
case 2:
insert();
break;
case 3:
deleteNode();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (ch != 5);
return 0;
}
15. Write a program for double linked list - Lab 7 Date 21-11-24
#include <stdio.h>
#include <stdlib.h>
void create();
void insert();
void deleteNode();
void display();
struct node {
int data;
struct node *prev;
struct node *next;
};
if (first == NULL) {
first = cur;
last = cur;
} else {
last->next = cur;
cur->prev = last;
last = cur;
}
}
void insert() {
int pos, i = 1;
cur = (struct node *)malloc(sizeof(struct node));
if (cur == NULL) {
printf("Memory allocation failed\n");
return;
}
printf("Enter the data: ");
scanf("%d", &cur->data);
printf("Enter the position: ");
scanf("%d", &pos);
if (pos == 1) {
cur->next = first;
cur->prev = NULL;
if (first != NULL) {
first->prev = cur;
}
first = cur;
if (last == NULL) {
last = cur;
}
} else {
temp = first;
while (i < pos - 1 && temp != NULL) {
temp = temp->next;
i++;
}
if (temp != NULL) {
cur->next = temp->next;
cur->prev = temp;
if (temp->next != NULL) {
temp->next->prev = cur;
}
temp->next = cur;
if (cur->next == NULL) {
last = cur;
}
} else {
printf("Invalid position\n");
free(cur);
}
}
}
void deleteNode() {
int pos, i = 1;
printf("Enter the position to delete: ");
scanf("%d", &pos);
if (first == NULL) {
printf("List is empty\n");
return;
}
if (pos == 1) {
cur = first;
first = first->next;
if (first != NULL) {
first->prev = NULL;
} else {
last = NULL;
}
free(cur);
} else {
cur = first;
while (i < pos && cur != NULL) {
cur = cur->next;
i++;
}
if (cur != NULL) {
if (cur->prev != NULL) {
cur->prev->next = cur->next;
}
if (cur->next != NULL) {
cur->next->prev = cur->prev;
}
if (cur == last) {
last = cur->prev;
}
free(cur);
} else {
printf("Invalid position\n");
}
}
}
void display() {
if (first == NULL) {
printf("List is empty\n");
} else {
cur = first;
printf("The elements in the list are: ");
while (cur != NULL) {
printf("%d <-> ", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
}
int main() {
int ch;
printf("NAME yordanos Moges Aud22880\n");
do {
printf("\nMenu:\n");
printf("1. Create\n");
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
create();
break;
case 2:
insert();
break;
case 3:
deleteNode();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (ch != 5);
return 0;
}
16. write a Program for binary tree lab 8 Date 21-11-24
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
int main() {
struct Node* root = NULL;
int n, value;
printf("NAME yordanos Moges Aud22880\n");
printf("Enter the number of nodes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter value for node %d: ", i + 1);
scanf("%d", &value);
root = insert(root, value);
}
return 0;
}
17. write a Program for Kruskal's Algorithm lab 9 Date 28-11-24
#include <stdio.h>
#include <stdlib.h>
struct Edge {
int src, dest, weight;
};
struct Graph {
int V, E;
struct Edge* edges;
};
struct Subset {
int parent;
int rank;
};
int main() {
int V, E;
printf("NAME yordanos moges AUD22880\n");
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &V, &E);
struct Graph* graph = createGraph(V, E);
printf("Enter the edges (source, destination, weight):\n");
for (int i = 0; i < E; i++) {
scanf("%d %d %d", &graph->edges[i].src, &graph->edges[i].dest, &graph-
>edges[i].weight);
}
KruskalMST(graph);
free(graph->edges);
free(graph);
return 0;
}