DSA programs
DSA programs
h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUM_DAYS_IN_WEEK 7
typedef struct {
char *acDayName;
int iDate;
char *acActivity;
} DAYTYPE;
void fnFreeCal(DAYTYPE *);
void fnDispCal(DAYTYPE *);
void fnReadCal(DAYTYPE *);
DAYTYPE *fnCreateCal();
int main() {
// Create the calendar
DAYTYPE *weeklyCalendar = fnCreateCal();
// Read data from the keyboard
fnReadCal(weeklyCalendar);
// Display the week's activity details
fnDispCal(weeklyCalendar);
// Free allocated memory
fnFreeCal(weeklyCalendar);
return 0;
}
DAYTYPE *fnCreateCal()
{ int i;
DAYTYPE *calendar = (DAYTYPE *)malloc(NUM_DAYS_IN_WEEK *
sizeof(DAYTYPE));
for (i = 0; i < NUM_DAYS_IN_WEEK; i++) {
calendar[i].acDayName = NULL;
calendar[i].iDate = 0;
calendar[i].acActivity = NULL;
}
return calendar;
}
void fnReadCal(DAYTYPE *calendar)
{ int i;
char name[10],act[50];
char cChoice;
for (i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Do you want to enter details for day %d [Y/N]: ", i + 1);
scanf("%c", &cChoice); // Add a space before %c to consume any whitespace
characters
PROGRAM-1
getchar(); // Consume the newline character left in the input buffer
if (tolower(cChoice) == 'n')
continue;
printf("Day Name: ");
// char name[50];
fgets(name, sizeof(name), stdin);
name[strcspn(name, "\n")] = 0;
calendar[i].acDayName = strdup(name);
printf("Date: ");
scanf("%d", &calendar[i].iDate);
getchar(); // Consume the newline character left in the input buffer
printf("Activity: ");
// char act[50];
fgets(act, sizeof(act), stdin);
act[strcspn(act, "\n")] = 0;
calendar[i].acActivity = strdup(act);
printf("\n");
}
}
void fnDispCal(DAYTYPE *calendar)
{ int i;
printf("\nWeek's Activity Details:\n");
for (i = 0; i < NUM_DAYS_IN_WEEK; i++) {
printf("Day %d:\n", i + 1);
if (calendar[i].iDate == 0) {
printf("No Activity\n\n");
continue;
}
printf(" Day Name: %s\n", calendar[i].acDayName);
printf(" Date: %d\n", calendar[i].iDate);
printf(" Activity: %s\n\n", calendar[i].acActivity);
}
}
void fnFreeCal(DAYTYPE *calendar)
{ int i;
for (i = 0; i < NUM_DAYS_IN_WEEK; i++) {
free(calendar[i].acDayName);
free(calendar[i].acActivity);
}
free(calendar);
}
2 #include<stdio.h>
#include<conio.h>
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag=0;
void stringmatch()
{
i = m = c = j = 0;
while(str[c]!= '\0')
{
if(str[m]==pat[i]) // ...... matching
{
i++; m++;
if(pat[i]=='\0') //.....found occurrences.
{
flag = 1;
//.... copy replace string in ans string.
for(k = 0; rep[k] !='\0'; k++, j++)
{
ans[j] = rep[k];
i = 0;
c = m;
}
}
}// if ends.
else //... mismatch
{
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}//else
}
//end of while
ans[j] ='\0';
} //end stringmatch()
void main()
{
printf("\nEnter a main string\n");
gets(str);
printf("\nEnter a pattern string\n");
gets(pat);
printf("\nEnter a replace string\n");
gets(rep);
stringmatch();
if(flag==1)
printf("The resultant string is : %s\n",ans);
else
printf("Pattern string NOT found\n");
3 #include <stdio.h>
#include <stdlib.h>
#define MAX 4
int stack[MAX], item;
int ch, top = -1, count = 0, status = 0;
/* PUSH FUNCTION */
void push(int stack[], int item) {
if (top == (MAX - 1))
printf("\nStack is Overflow");
else {
stack[++top] = item;
status++;
}
}
/* POP FUNCTION */
int pop(int stack[]) {
int ret;
if (top == -1)
printf("\n\nStack is Underflow");
else {
ret = stack[top--];
status--;
printf("\nPopped element is %d\n", ret);
}
return ret;
}
/* FUNCTION TO CHECK STACK IS PALINDROME OR NOT */
void palindrome(int stack[]) {
int i, temp;
temp = status;
for (i = 0; i < temp; i++) {
if (stack[i] == pop(stack))
count++;
}
if (temp == count)
printf("\nStack contents are Palindrome");
else
printf("\nStack contents are not palindrome");
}
/* FUNCTION TO DISPLAY STACK */
void display(int stack[]) {
int i;
printf("\nThe stack contents are:");
if (top == -1)
printf("\nStack is Empty");
else {
for (i = top; i >= 0; i--) {
printf("\n ------\n| %d |", stack[i]);
}
PROGRAM-3
printf("\n");
}
}
/* MAIN PROGRAM */
void main() {
do {
printf("\n\n----MAIN MENU----\n");
printf("1. PUSH (Insert) in the Stack\n");
printf("2. POP (Delete) from the Stack\n");
printf("3. PALINDROME check using Stack\n");
printf("4. Exit (End the Execution)\n");
printf("Enter Your Choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter an element to be pushed: ");
scanf("%d", &item);
push(stack, item);
display(stack);
break;
case 2:
item = pop(stack);
display(stack);
break;
case 3:
palindrome(stack);
break;
case 4:
exit(0);
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (1); // Infinite loop, exit using case 4
}
5A #include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
double compute(char symbol, double op1, double op2) {
switch (symbol) {
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '$':
case '^': return pow(op1, op2);
default: return 0;
}
}
int main() {
double s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;
printf("Enter the postfix expression:\n");
fgets(postfix, sizeof(postfix), stdin);
top = -1;
for (i = 0; i < strlen(postfix); i++) {
symbol = postfix[i];
if (isdigit(symbol)) {
s[++top] = symbol - '0';
} else if (symbol != ' ' && symbol != '\n') {
op2 = s[top--];
op1 = s[top--];
res = compute(symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf("The result is: %f\n", res);
return 0;
}
5B #include <stdio.h>
#include <stdlib.h>
void tower(int n, char source, char temp, char destination) {
if (n == 0)
return;
6 #include <stdio.h>
#define MAX 4
int ch, front = 0, rear = -1, count = 0;
char q[MAX], item;
void insert() {
if (count == MAX)
printf("\nQueue is Full");
else {
rear = (rear + 1) % MAX;
q[rear] = item;
count++;
}
}
void del() {
if (count == 0)
printf("\nQueue is Empty");
else {
item = q[front];
printf("\nDeleted item is: %c", item);
front = (front + 1) % MAX;
count--;
}
}
void display() {
int i, f = front, r = rear;
if (count == 0)
printf("\nQueue is Empty");
else {
printf("\nContents of Queue is:\n");
for (i = 0; i < count; i++) {
printf("%c\t", q[f]);
f = (f + 1) % MAX;
}}}
int main() {
do {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
getchar(); // To consume the newline character left in the input buffer
switch (ch) {
case 1:
printf("\nEnter the character (item) to be inserted: ");
scanf(" %c", &item); // Add a space before %c to consume
whitespace/newline
insert();
break;
case 2:
PROGRAM-6
del();
break;
case 3:
display();
break;
case 4:
printf("\nExiting program...");
break;
default:
printf("\nInvalid choice! Please enter a valid option.");
} } while (ch != 4);
return 0;
}
7 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to represent a student
typedef struct Student {
char usn[10];
char name[10];
char programme[5];
int sem;
char phNo[10];
struct Student *next;
} *NODE;
// Function prototypes
NODE createNode();
NODE insertFront(NODE head);
void display(NODE head);
int countNodes(NODE head);
NODE insertEnd(NODE head);
NODE deleteFront(NODE head);
NODE deleteEnd(NODE head);
// Function to create a new node
NODE createNode() {
NODE newNode = (NODE)malloc(sizeof(struct Student));
if (newNode == NULL) {
printf("\nMemory allocation failed.");
exit(1);
}
printf("\n Enter USN,Name,Programme,Semester,PhNo\n");
scanf("%s%s%s%d%s",newNode->usn,newNode->name,newNode-
>programme,&newNode
>sem,newNode->phNo);
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the front of the list
NODE insertFront(NODE head) {
NODE newNode = createNode();
newNode->next = head;
return newNode;
}
// Function to display the linked list
void display(NODE head) {
if (head == NULL) {
printf("\nList is empty.");
return;
}
NODE temp = head;
printf("\n---- Student Data ----\n");
while (temp != NULL) {
printf("\nUSN: %s \nName: %s \nProgramme: %s\n Sem: %d\n PhNo: %s\n",
temp->usn,
temp->name, temp->programme, temp->sem, temp->phNo);
temp = temp->next;
}
} int countNodes(NODE head) {
int count = 0;
NODE temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
// Function to insert a node at the end of the list
NODE insertEnd(NODE head) {
NODE newNode = createNode();
if (head == NULL) {
return newNode;
}
NODE temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
}
// Function to delete the first node of the list
NODE deleteFront(NODE head) {
if (head == NULL) {
printf("\nList is empty. Cannot delete.");
return head;
}
NODE temp = head;
head = head->next;
free(temp);
printf("\nFirst node deleted.");
return head;
}
// Function to delete the last node of the list
NODE deleteEnd(NODE head) {
if (head == NULL) {
printf("\nList is empty. Cannot delete.");
return head;
}
if (head->next == NULL) {
free(head);
printf("\nLast node deleted.");
return NULL;
}
NODE prev = NULL;
NODE temp = head;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
free(temp);
prev->next = NULL;
printf("\nLast node deleted.");
return head;
}
int main() {
NODE head = NULL;
int choice;
do {
printf("\nMenu:");
printf("\n1. Create a SLL of N Students Data by using front insertion");
printf("\n2. Display the status of SLL and count the number of nodes in it");
printf("\n3. Perform Insertion at End of SLL");
printf("\n4. Perform Deletion at End of SLL");
printf("\n5. Perform Insertion at Front of SLL (Demonstration of stack)");
printf("\n6. Perform Deletion at Front of SLL (Demonstration of stack)");
printf("\n7. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
head = insertFront(head);
break;
case 2:
display(head);
printf("\nNumber of nodes: %d", countNodes(head));
break;
case 3:
head = insertEnd(head);
break;
case 4:
head = deleteEnd(head);
break;
case 5:
head = insertFront(head);
break;
case 6:
head = deleteFront(head);
break;
case 7:
printf("\nExiting program...");
break;
default:
printf("\nInvalid choice! Please enter a valid option.");
}
} while(choice != 7);
return 0;
}
10 #include <stdio.h>
#include <stdlib.h>
typedef struct BST {
int data;
struct BST *left;
struct BST *right;
} *NODE;
NODE createtree(NODE node, int data) {
if (node == NULL) {
NODE temp = (NODE)malloc(sizeof(struct BST));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
if (data < (node->data)) {
node->left = createtree(node->left, data);
} else if (data > node->data) {
node->right = createtree(node->right, data);
}
return node;
}
NODE search(NODE node, int data) {
if (node == NULL) {
printf("\nElement not found");
} else if (data < node->data) {
node->left = search(node->left, data);
} else if (data > node->data) {
node->right = search(node->right, data);
} else {
printf("\nElement found is: %d", node->data);
}
return node;
}
void inorder(NODE node) {
if (node != NULL) {
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
}
}
void preorder(NODE node) {
if (node != NULL) {
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
}
}void postorder(NODE node) {
if (node != NULL) {
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
}
}
int main() {
int data, ch, i, n;
NODE root = NULL;
while (1) {
printf("\n1.Create BST\n2.Search Element in BST\n3.Inorder\n4.Preorder\
n5.Postorder\n6.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter N value: ");
scanf("%d", &n);
printf("\nEnter the values to create BST (e.g., 6 9 5 2 8 15 24
14 7 8 5 2):\n");
for (i = 0; i < n; i++) {
scanf("%d", &data);
root = createtree(root, data);
}
break;
case 2:
printf("\nEnter the element to search: ");
scanf("%d", &data);
search(root, data);
break;
case 3:
printf("\nInorder Traversal: ");
inorder(root);
break;
case 4:
printf("\nPreorder Traversal: ");
preorder(root);
break;
case 5:
printf("\nPostorder Traversal: ");
postorder(root);
break;
case 6:
exit(0);
default:
printf("\nWrong option");
break;
}
}
return 0;
}