0% found this document useful (0 votes)
35 views11 pages

Dsa Da 2 22bce0411

Uploaded by

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

Dsa Da 2 22bce0411

Uploaded by

swastik raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

data structures and

algorithms lab
digital assignment 2

Name:- Swastik raj


Register no. :- 22BCE0411
Branch:- Computer science and engineering
core
Faculty name:- Dr. Parveen Sultana H
Slot:- L7+L8
Devise code in C/C++/Java to implement
the following
1. Perform circular queue operations
on a given set comprised of height of n
students.
#include <stdio.h>
#define MAX_SIZE 10

int front = -1, rear = -1;


int data[MAX_SIZE];

void enqueue(int x) {
if ((rear + 1) % MAX_SIZE == front) {
printf("Queue overflow!\n");
return;
}
if (front == -1) {
front = 0;
rear = 0;
}
else {
rear = (rear + 1) % MAX_SIZE;
}
data[rear] = x;
}

void dequeue() {
if (front == -1) {
printf("Queue underflow!\n");
return;
}
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
}
void print_queue() {
if (front == -1) {
printf("Queue is empty!\n");
return;
}
int i;
printf("Queue contents: ");
for (i = front; i != rear; i = (i + 1) %
MAX_SIZE) {
printf("%d ", data[i]);
}
printf("%d\n", data[i]);
}

int main() {
int n, i, x;
printf("Enter the number of students:
");
scanf("%d", &n);
printf("Enter the heights of the
students:\n");
for (i = 0; i < n; i++) {
scanf("%d", &x);
enqueue(x);
}
printf("Initial queue:\n");
print_queue();
printf("Dequeueing first student...\n");
dequeue();
printf("Queue after dequeue:\n");
print_queue();
printf("Adding a new student with
height 170cm...\n");
enqueue(170);
printf("Queue after enqueue:\n");
print_queue();
return 0;
}
2. Apply stack to evaluate an inputted
postfix expression.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_STACK_SIZE 100

typedef struct { int top,


data[MAX_STACK_SIZE]; } Stack;

void push(Stack *s, int value) { s-


>data[++s->top] = value; }
int pop(Stack *s) { return s->data[s->top-
-]; }

int evaluate_postfix_expression(char
*expression) {
Stack s = { .top = -1 };
for (int i = 0; expression[i]; i++) {
if (isdigit(expression[i])) push(&s,
expression[i] - '0');
else { int op2 = pop(&s), op1 =
pop(&s);
switch (expression[i]) {
case '+': push(&s, op1 + op2);
break;
case '-': push(&s, op1 - op2);
break;
case '*': push(&s, op1 * op2);
break;
case '/': push(&s, op1 / op2);
break;
default: exit(EXIT_FAILURE); } } }
int result = pop(&s);
if (s.top >= 0) exit(EXIT_FAILURE);
return result;
}

int main() {
char expression[MAX_STACK_SIZE];
printf("Enter postfix expression: ");
scanf("%s", expression);
printf("Result = %d\n",
evaluate_postfix_expression(expression)
);
return 0;
}
Thank you

You might also like