0% found this document useful (0 votes)
19 views10 pages

Ds Corrected Code Lab Vtu

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

Ds Corrected Code Lab Vtu

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

program 7

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

#define null 0

// Structure to store student details


struct student {
char usn[15], name[20], branch[10];
int sem;
char phno[20];
struct student *link;
};

typedef struct student node;


node *start = NULL;

// 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;
}

// Function to create a linked list of students


void create() {
int i, n;
node *p;

printf("Enter the number of students: ");


scanf("%d", &n);

for(i = 0; i < n; i++) {


p = (node*)malloc(sizeof(node));
printf("Enter the student USN, NAME, BRANCH, SEM, PHNO: ");
scanf("%s %s %s %d %s", p->usn, p->name, p->branch, &p->sem, p->phno);

p->link = start;
start = p;
}
}

// Function to display the linked list of students


void disp() {
int cnt = 0;
node *t = start;

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);
}

// Function to insert a student at the end of the linked list


void insert_end() {
node *p, *r;
p = (node*)malloc(sizeof(node));

printf("Enter the student USN, NAME, BRANCH, SEM, PHNO: ");


scanf("%s %s %s %d %s", p->usn, p->name, p->branch, &p->sem, p->phno);

p->link = NULL;

if (start == NULL) {
start = p;
} else {
r = start;
while(r->link != NULL) {
r = r->link;
}
r->link = p;
}
}

// Function to delete the front node from the linked list


void del_front() {
node *q;

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;

// Function to insert an item into the queue


void insert(char item) {
if (count == MAX) {
printf("\nQueue is Full\n");
return;
}
rear = (rear + 1) % MAX; // Increment rear in a circular manner
q[rear] = item; // Insert the item
count++; // Increase count of elements
}

// Function to delete an item from the queue


void del() {
if (count == 0) {
printf("\nQueue is Empty\n");
return;
}
item = q[front]; // Get the item at the front
printf("\nDeleted item is: %c\n", item);
front = (front + 1) % MAX; // Move front in a circular manner
count--; // Decrease count

// Reset queue if it becomes empty


if (count == 0) {
front = 0;
rear = -1;
}
}

// Function to display the queue


void display() {
if (count == 0) {
printf("\nQueue is Empty\n");
return;
}
printf("\nContents of Queue:\n");
int f = front;
for (int i = 0; i < count; i++) { // Loop through the number of elements
printf("%c\t", q[f]);
f = (f + 1) % MAX; // Move in a circular manner
}
printf("\n");
}

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>

// Function to perform operations based on the symbol


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 '$': // both '$' and '^' are considered as power operators
case '^': return pow(op1, op2);
default: return 0;
}
}

int main() {
double s[20], res, op1, op2;
int top = -1, i;
char postfix[20], symbol;

printf("Enter the postfix expression:\n");


scanf("%s", postfix);

for(i = 0; i < strlen(postfix); i++) {


symbol = postfix[i];

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>

// Recursive function to solve the Tower of Hanoi problem


void tower(int n, char source, char temp, char destination) {
if(n == 0) return;

tower(n - 1, source, destination, temp);


printf("Move disc %d from %c to %c\n", n, source, destination);
tower(n - 1, temp, source, destination);
}

int main() {
int n;
printf("Enter the number of discs: ");
scanf("%d", &n);

tower(n, 'A', 'B', 'C');


printf("\nTotal Number of moves are: %d\n", (int)pow(2, n) - 1);

return 0;
}

program 4.
#include <stdio.h>
#include <string.h>

// Function to get precedence in the stack


int F(char symbol) {
switch(symbol) {
case '+':
case '-': return 2;
case '*':
case '/': return 4;
case '^':
case '$': return 5;
case '(': return 0;
case '#': return -1;
default: return 8;
}
}

// Function to get precedence in the input expression


int G(char symbol) {
switch(symbol) {
case '+':
case '-': return 1;
case '*':
case '/': return 3;
case '^':
case '$': return 6;
case '(': return 9;
case ')': return 0;
default: return 7;
}
}

// Function to convert infix expression to postfix


void infix_postfix(char infix[], char postfix[]) {
int top = -1, j = 0, i;
char s[30], symbol;

s[++top] = '#'; // Initialize the stack with '#'

for(i = 0; i < strlen(infix); i++) {


symbol = infix[i];

while(F(s[top]) > G(symbol)) {


postfix[j++] = s[top--];
}

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--];
}

postfix[j] = '\0'; // Null-terminate the postfix expression


}

int main() {
char infix[20], postfix[20];

printf("Enter a valid infix expression: ");


scanf("%s", infix);

infix_postfix(infix, postfix);

printf("\nThe infix expression is: %s", infix);


printf("\nThe postfix expression is: %s\n", postfix);

return 0;
}

prog 3
#include <stdio.h>
#include <string.h>
#define MAX 5
int st[MAX], top = -1;

// Function to push an item to the stack


void push(int item) {
if (top == MAX - 1) {
printf("Stack overflow\n");
return;
}
top = top + 1;
st[top] = item;
}

// Function to pop an item from the stack


int pop() {
if (top == -1) {
printf("Stack underflow\n");
return 0;
}
int poppedItem = st[top];
top = top - 1;
return poppedItem;
}

// Function to display stack contents


void disp() {
int i;
if (top == -1) {
printf("Stack is empty\n");
return;
}
printf("Stack contents:\n");
for (i = top; i >= 0; i--) {
printf("|%d|\n", st[i]);
}
}

// 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;

// Function to perform string matching and replacement


void stringmatch() {
i = m = c = j = 0;
while (str[c] != '\0') {
if (str[m] == pat[i]) { // Matching characters
i++;
m++;
if (pat[i] == '\0') { // Pattern fully matched
flag = 1;
// Copy replacement string to ans
for (k = 0; rep[k] != '\0'; k++, j++) {
ans[j] = rep[k];
}
i = 0;
c = m;
}
} else { // Mismatch case
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
ans[j] = '\0'; // Null-terminate the result
}

int main() {
printf("Enter the main string:\n");
scanf(" %[^\n]", str);

printf("Enter the pattern string:\n");


scanf(" %[^\n]", pat);

printf("Enter the replacement string:\n");


scanf(" %[^\n]", rep);
stringmatch();

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>

// Structure to represent a day in the calendar


struct Day {
char *name;
int date;
char *activity;
};

// Function to create a day in the calendar


struct Day create() {
struct Day day;
day.name = (char *)malloc(20 * sizeof(char)); // Allocating memory for the
name
day.activity = (char *)malloc(100 * sizeof(char)); // Allocating memory for the
activity

printf("Enter the day name: ");


scanf("%s", day.name);
printf("Enter the date: ");
scanf("%d", &day.date);
printf("Enter the activity for the day: ");
scanf(" %[^\n]", day.activity);

return day;
}

// Function to read data for the calendar


void read(struct Day calendar[], int size) {
for (int i = 0; i < size; i++) {
calendar[i] = create();
}
}

// Function to display the weekly activity details report


void display(struct Day calendar[], int size) {
printf("\nWeekly Activity Details:\n");
for (int i = 0; i < size; i++) {
printf("Day %d: %s\n", i + 1, calendar[i].name);
printf("Date: %d\n", calendar[i].date);
printf("Activity: %s\n", calendar[i].activity);
printf("\n");
}
}
int main() {
int weekSize = 7;
struct Day calendar[weekSize];

// Create the calendar


read(calendar, weekSize);

// Display the weekly activity details


display(calendar, weekSize);

// Free dynamically allocated memory


for (int i = 0; i < weekSize; i++) {
free(calendar[i].name);
free(calendar[i].activity);
}

return 0;
}

You might also like