dsa 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

PROGRAM – 3

Write a program to sort an array of integers in ascending order using


bubble sort.
#include <stdio.h>
void bubble_sort(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;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:
PROGRAM – 4
Write a program to demonstrate the use of stack (implemented using linear
array) in converting arithmetic expression from infix notation to postfix
notation.

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

#define MAX 100

int precedence(char c) {
if (c == '^')
return 3;
else if (c == '*' || c == '/')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}

void infixToPostfix(char* exp) {


char stack[MAX];
int top = -1;
char postfix[MAX];
int k = 0;
for (int i = 0; i < strlen(exp); i++) {
char c = exp[i];
if (isalpha(c)) {
postfix[k++] = c;
}
else if (c == '(') {
stack[++top] = c;
}
else if (c == ')') {
while (top != -1 && stack[top] != '(') {
postfix[k++] = stack[top--];
}
top--;
}
else {
while (top != -1 && precedence(stack[top]) >= precedence(c)) {
postfix[k++] = stack[top--];
}
stack[++top] = c;
}
}
while (top != -1) {
postfix[k++] = stack[top--];
}
postfix[k] = '\0';
printf("Postfix Expression: %s\n", postfix);
}

int main() {
char infix[] = "(A+(B*C-(D/E^F)*G)*H)";
printf("Infix Expression: %s\n", infix);
infixToPostfix(infix);
return 0;
}

Output:
PROGRAM – 5
Program to demonstrate the use of stack (implemented using linear linked
lists) in evaluating arithmetic expression in postfix notation.

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

typedef struct Node {


int data;
struct Node* next;
} Node;

Node* push(Node* top, int element) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = top;
return newNode;
}

int pop(Node** top) {


int element = (*top)->data;
Node* temp = *top;
*top = (*top)->next;
free(temp);
return element;
}

int evaluatePostfix(char postfix[]) {


Node* top = NULL;
for (int i = 0; postfix[i] != '\0'; i++) {
if (postfix[i] >= '0' && postfix[i] <= '9') {
top = push(top, postfix[i] - '0');
} else {
int operand2 = pop(&top);
int operand1 = pop(&top);
int result;
switch (postfix[i]) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
}
top = push(top, result);
}
}
return pop(&top);
}

int main() {
char postfix[100];
printf("Enter a postfix expression: ");
fgets(postfix, sizeof(postfix), stdin);
postfix[strlen(postfix) - 1] = '\0';
int result = evaluatePostfix(postfix);
printf("Result: %d\n", result);
return 0;
}

OUTPUT:
PROGRAM – 6
Program to demonstration the implementation of various operations on a
linear queue represented using a linear array.

//Enqueue
#include <stdio.h>

int main(){
int que[10]={12,43,75,23,53,85,23};
int front=0;
int rear=6;
if(rear==sizeof(que)-1){
printf("OVERFLOW");
}
else {
rear++;
printf("Enter the no. to Enqueue : ");
scanf("%d",&que[rear]);
}
for(int i=0;i<rear+1;i++){
printf("%d\t",que[i]);
}
return 0;
}

OUTPUT:
//Dequeue
#include <stdio.h>

int main(){
int que[10]={12,43,75,23,53,85,23};
int front=0;
int rear=7;
if(front==-1){
printf("UNDERFLOW");
}
else {
front++;
}
for(int i=front;i<rear;i++){
printf("%d\t",que[i]);
}
return 0;
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy