Ds Corrected Code Lab Vtu
Ds Corrected Code Lab Vtu
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define null 0
// Function declarations
void create();
void insert_end();
void del_front();
void disp();
int main() {
int ch;
while(1) {
printf("\nMain Menu\n");
printf("1: Create\n2: Display\n3: Insert at End\n4: Delete Front\n5: Exit\
n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch) {
case 1: create(); break;
case 2: disp(); break;
case 3: insert_end(); break;
case 4: del_front(); break;
case 5: exit(0);
default: printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
p->link = start;
start = p;
}
}
if (t == NULL) {
printf("List is empty.\n");
return;
}
printf("\nStudent Details:\n");
while(t) {
cnt++;
printf("%s\t%s\t%s\t%d\t%s ->\n", t->usn, t->name, t->branch, t->sem, t-
>phno);
t = t->link;
}
printf("Total number of nodes = %d\n", cnt);
}
p->link = NULL;
if (start == NULL) {
start = p;
} else {
r = start;
while(r->link != NULL) {
r = r->link;
}
r->link = p;
}
}
if (start == NULL) {
printf("List is empty\n");
return;
}
q = start;
printf("Deleted node is: %s\n", q->usn);
start = start->link;
free(q);
}
program 6
#include <stdio.h>
#include <stdlib.h>
#define MAX 4
// Queue declarations
int front = 0, rear = -1, count = 0;
char q[MAX], item;
int main() {
int ch;
do {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter the choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the character / item to be inserted: ");
scanf(" %c", &item); // Read a character
insert(item);
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
} while (ch != 4);
return 0;
}
program 5
a.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int main() {
double s[20], res, op1, op2;
int top = -1, i;
char postfix[20], symbol;
if(isdigit(symbol)) {
s[++top] = symbol - '0'; // Push the operand onto the stack
} else {
// Pop two operands
op2 = s[top--];
op1 = s[top--];
// Compute the result and push it back onto the stack
res = compute(symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--]; // Final result
printf("The result is: %f\n", res);
return 0;
}
b.
#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("Enter the number of discs: ");
scanf("%d", &n);
return 0;
}
program 4.
#include <stdio.h>
#include <string.h>
if(F(s[top]) != G(symbol)) {
s[++top] = symbol;
} else {
top--; // Pop if there's a matching parenthesis
}
}
while(s[top] != '#') {
postfix[j++] = s[top--];
}
int main() {
char infix[20], postfix[20];
infix_postfix(infix, postfix);
return 0;
}
prog 3
#include <stdio.h>
#include <string.h>
#define MAX 5
int st[MAX], top = -1;
// Main function
int main() {
int ch, k, item;
while (1) {
printf("\nMAIN MENU\n");
printf("1: Push\n2: Pop\n3: Display\n4: Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter an item to push: ");
scanf("%d", &item);
push(item);
break;
case 2:
k = pop();
if (k != 0) { // Ensuring underflow doesn't print a false item
printf("Popped element is %d\n", k);
}
break;
case 3:
disp();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}
program 2
#include <stdio.h>
#include <string.h>
// Declarations
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag = 0;
int main() {
printf("Enter the main string:\n");
scanf(" %[^\n]", str);
if (flag == 1)
printf("\nThe resultant string is:\n%s\n", ans);
else
printf("\nPattern string NOT found\n");
return 0;
}
program 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
return day;
}
return 0;
}