Ds Practical Qs
Ds Practical Qs
1) Implement Stack ADT using an array. Include push and display opera on
2) Implement Stack ADT using an array. Include push and pop opera on
#include <stdio.h>
#define MAX 5
typedef struct{
int arr[MAX];
int top;
}Stack;
int main(){
Stack stack;
initStack (&stack);
int choice , value;
while(1){
prin ("choosen an opera on \n");
prin ("Select 1 for push \n");
prin ("Select 2 for pop \n");
prin ("Select 3 for display \n");
prin ("Select 4 for exit \n");
scanf("%d", &choice);
switch(choice){
case 1:
prin ("Enter the value to push: ");
scanf("%d", &value);
push(&stack, value); // Call push func on
break;
case 2:
pop(&stack);
case 3:
display(&stack);
break;
case 4:
prin ("Exi ng Program..");
return 0;
default:
prin ("Please select correct choice of code:");
}
}
return 0;
}
3) Find the square root of the number using binary search technique.
#include <stdio.h>
double squareroot(double num){
double low = 0 , high = num , mid;
double precision = 0.00001;
while((high-low)>precision){
mid = (high + low) /2;
if(mid*mid == num){
return mid;
}else if(mid * mid < num){
low = mid;
}else if(mid*mid > num){
high = mid;
}
}
return (low+high)/2;
}
int main(){
double num;
prin ("Enter the number to find it's square root: ");
scanf("%lf",&num);
if(num == 0 ){
*head = newNode;
prin ("Inserted %d at the front of the list.\n", value);
}
// Insert at the end of the linked list
void insertAtEnd(Node** head, int value) {
// Main func on
int main() {
Node* head = NULL; // Ini alize an empty list
switch (choice) {
case 1:
prin ("Enter the value to insert at the front: ");
scanf("%d", &value);
insertAtFront(&head, value);
break;
case 2:
prin ("Enter the value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
displayList(head);
break;
case 4:
// Dequeue opera on
void dequeue(CircularQueue* queue) {
if (queue->front == -1) {
prin ("Queue Underflow! The queue is empty.\n");
return;
}
prin ("Dequeued value: %d\n", queue->arr[queue->front]);
if (queue->front == queue->rear) {
// If the queue becomes empty
queue->front = -1;
queue->rear = -1;
} else {
queue->front = (queue->front + 1) % MAX;
}
}
// Display opera on
void display(CircularQueue* queue) {
if (queue->front == -1) {
prin ("The queue is empty.\n");
return;
}
break;
case 2:
dequeue(&queue);
break;
case 3:
display(&queue);
break;
case 4:
prin ("Exi ng the program.\n");
return 0;
default:
prin ("Invalid choice! Please try again.\n");
}
}
return 0;
}
9) Implement Doubly Linked List. Perform inser on at the end and display the list
10) Implement Doubly Linked List. Perform inser on at the begin and display the list
#include <stdio.h>
#include <stdlib.h>
// Node structure
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
// Func on to create a new node
return;
}
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
prin ("Inserted %d at the end of the list.\n", value);
}
int main() {
Node* head = NULL; // Ini alize an empty list
int choice, value;
while (1) {
prin ("\nChoose an opera on:\n");
prin ("1. Insert at the beginning\n");
prin ("2. Insert at the end\n");
prin ("3. Display the list\n");
prin ("4. Exit\n");
prin ("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
prin ("Enter the value to insert at the beginning: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
prin ("Enter the value to insert at the end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
displayList(head);
break;
case 4:
prin ("Exi ng the program.\n");
return 0;
default:
prin ("Invalid choice. Please try again.\n");
}
}
return 0;
}
11) Pos ix evalua on
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX 100
// Stack structure
typedef struct {
int arr[MAX];
int top;
} Stack;
if (isdigit(*token)) {
push(&stack, *token - '0');
}
// If the token is an operator, pop two elements, apply the operator, and push the result
else {
int operand2 = pop(&stack);
int operand1 = pop(&stack);
int result;
switch (*token) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
prin ("Division by zero error!\n");
exit(1);
}
result = operand1 / operand2;
break;
case '^':
result = pow(operand1, operand2);
break;
default:
prin ("Invalid operator: %c\n", *token);
exit(1);
}
push(&stack, result);
}
token++;
}
// Final result should be the only element in the stack
return pop(&stack);
}
// Main func on
int main() {
char expression[MAX];
prin ("Enter a pos ix expression: ");
scanf("%s", expression);
int result = evaluatePos ix(expression);
prin ("The result of the pos ix expression is: %d\n", result);
return 0;
}
12) Infix to pos ix
#include <stdio.h>
#include <ctype.h> // For isalpha() and isdigit()
#include <string.h> // For strlen()
typedef struct {
char arr[MAX];
int top;
} Stack;
int main() {
char infix[MAX], pos ix[MAX];
return 0;
}