DSA Lab Programs Easy and Small For SEM III LAB Externals @vtunetwork
DSA Lab Programs Easy and Small For SEM III LAB Externals @vtunetwork
DSA Lab Programs Easy and Small For SEM III LAB Externals @vtunetwork
#include <stdio.h>
#define MAX_DAY_NAME_LENGTH 20
struct day{
char dayName[MAX_DAY_NAME_LENGTH];
int date;
char dayDes[MAX_DES_LENGTH];
};
struct day weekcalender[7];
void read(){
for(int i=0;i<7;++i){
printf("ENTER THE DAY NAME OF DAY %d: \n",i+1);
scanf("%s",weekcalender[i].dayName);
printf("Enter the date for day %d :",i+1);
scanf("%d",&weekcalender[i].date);
printf("Enter Activity Description for day %d:",i+1);
scanf("%[^\n]s",weekcalender[i].dayDes);
}
}
void display(){
printf("\n --calender--\n");
for(int i=0;i<7;++i){
printf("DAY %d :%s Date :%d
ActivityDescription:%s\n",i+1,weekcalender[i].dayName,weekcalender[i].date,weekcalender[i].dayDes );
}
}
int main()
{
printf("ENTER THE DETAILS OF EACH DAY\n");
read();
display;
return 0;
}
2. Develop a Program in C for the following operations on Strings. a) Read a main String (STR), a
Pattern String (PAT) and a Replace String (REP) b) Perform Pattern Matching Operation: Find and
Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report suitable messages in case
PAT does not exist in STR Support the program with functions for each of the above operations.
Don't use Built-in functions
#include <stdio.h>
int main() {
char str[100], pat[20], rep[20];
printf("Enter the string: ");
read(str);
printf("Enter the pattern: ");
read(pat);
printf("Enter the string to be replaced: ");
read(rep);
if (matchnreplace(str, pat, rep))
printf("The string after replacement: %s", str);
else
printf("The pattern is not found");
return 0;
}
3. Develop a menu driven Program in C for the following operations on STACK of Integers (Array
Implementation of Stack with maximum size MAX) 1. Push an Element on to Stack 2. Pop an Element
from Stack 3. Demonstrate how Stack can be used to check Palindrome 4. Demonstrate Overflow
and Underflow situations on Stack 5. Display the status of Stack 6. Exit Support the program with a
#include <stdio.h>
#include <stdlib.h>
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
return;
}
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
int main() {
int choice, element;
char str[MAX];
while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Check Palindrome\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to push: ");
scanf("%d", &element);
push(element);
break;
case 2:
element = pop();
if (element != -1)
printf("Popped element: %d\n", element);
break;
case 3:
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str))
printf("The entered string is a palindrome.\n");
else
printf("The entered string is not a palindrome.\n");
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, %
(Remainder), ^ (Power) and alphanumeric operands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char stack[MAX];
int top = -1;
int main() {
char infix[MAX], postfix[MAX];
printf("Enter infix expression: ");
fgets(infix, MAX, stdin); // Read infix expression from user
infixToPostfix(infix, postfix); // Convert infix to postfix
printf("Postfix expression: %s\n", postfix); // Print postfix expression
return 0;
}
5.Develop a Program in C for the following Stack Applications a. Evaluation of Suffix expression with
single digit operands and operators: +, -, *, /, %, ^ a. Solving Tower of Hanoi problem with n disks
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 50
typedef struct {
int top;
int items[MAX];
} Stack;
int main() {
char exp[MAX];
printf("Enter the postfix expression: ");
scanf("%s", exp);
printf("The result of evaluation is : %d\n", evaluatePostfix(exp));
return 0;
}
TOWER OF HONOI
#include <stdio.h>
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
char queue[MAX];
int front = -1, rear = -1;
int isFull() {
return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
}
int isEmpty() {
return (front == -1);
}
char dequeue() {
char item;
if (isEmpty()) {
printf("Queue is empty\n");
JOIN OUR NAMMA VTU COMMUNITY CLICK HERE TO JOIN
ನಮ್ಮ ವಿಟಿಯು
exit(1);
}
item = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % MAX;
}
return item;
}
void display() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}
int i = front;
printf("Queue elements: ");
do {
printf("%c ", queue[i]);
i = (i + 1) % MAX;
} while (i != (rear + 1) % MAX);
printf("\n");
}
int main() {
int choice;
char item;
while (1) {
printf("\nCircular Queue Operations\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to insert: ");
scanf(" %c", &item);
enqueue(item);
break;
case 2:
printf("Deleted element: %c\n", isEmpty() ? ' ' : dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char SSN[10];
char Name[10];
char Branch[10];
char Des[10];
char sal[10];
char phone[10];
};
typedef struct Employee EMP;
struct node {
EMP info;
struct node *lptr, *rptr;
};
typedef struct node* Node;
Node front = NULL, rear = NULL;
EMP GetRec() {
EMP temp;
printf("Enter the SSN: ");
fflush(stdin);
gets(temp.SSN);
printf("Enter the Name:");
fflush(stdin);
gets(temp.Name);
printf("Enter branch:");
fflush(stdin);
gets(temp.Branch);
return temp;
}
void Finsert(EMP ele) {
Node temp = Create(ele);
if (front == NULL) {
front = temp;
rear = temp;
} else {
temp->rptr = front;
front->lptr = temp;
front = temp;
}
}
void Einsert(EMP ele) {
Node temp = Create(ele);
if (rear == NULL) {
front = temp;
rear = temp;
} else {
rear->rptr = temp;
temp->lptr = rear;
rear = temp;
}
}
void Fdelete()
{
Node temp = front;
if (front == NULL)
printf("\nList is empty");
else {
temp = front;
printf("\nDeleted Employee Records \n");
DispRec(temp->info);
front = front->rptr;
front->lptr = NULL;
if (front == NULL) rear = NULL;
free(temp);
}
}
void Edelete() {
Node temp = rear, t;
if (rear == NULL)
printf("\nList is empty");
else if (rear->lptr == NULL) {
printf("\nDeleted Employee Records \n");
DispRec(rear->info);
front = rear = NULL;
free(temp);
} else {
rear = rear->lptr;
rear->rptr = NULL;
printf("\nDeleted Employee Records \n");
DispRec(temp->info);
free(temp);
}
}
void display(Node front) {
Node temp = front;
if (temp == NULL)
printf("\nList is empty\n");
else {
printf("\nEmployee Records list\n");
while (temp != NULL) {
DispRec(temp->info);
temp = temp->rptr;
}
}
}
int main() {
EMP ele;
int flag = 1, ch;
while (flag) {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char USN[15];
char Name[50];
char Programme[50];
JOIN OUR NAMMA VTU COMMUNITY CLICK HERE TO JOIN
ನಮ್ಮ ವಿಟಿಯು
int Sem;
long int PhNo;
struct Student* next;
};
int main() {
struct Student* head = NULL;
int choice;
do {
switch (choice) {
case 1:
createStudent(&head);
break;
case 2:
displayStudents(head);
printf("Total students: %d\n", countStudents(head));
break;
case 3:
insertAtFront(&head);
break;
case 4:
deleteAtFront(&head);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a number between 1 and
5.\n");
}
} while (choice != 5);
return 0;
}
#include <stdio.h>
JOIN OUR NAMMA VTU COMMUNITY CLICK HERE TO JOIN
ನಮ್ಮ ವಿಟಿಯು
#include <stdlib.h>
struct Term {
int coeff, x_exp, y_exp, z_exp;
struct Term* next;
};
void insert(struct Term** poly, int coeff, int x_exp, int y_exp, int z_exp) {
struct Term* newTerm = malloc(sizeof(struct Term));
newTerm->coeff = coeff;
newTerm->x_exp = x_exp;
newTerm->y_exp = y_exp;
newTerm->z_exp = z_exp;
newTerm->next = *poly;
*poly = newTerm;
}
void add(struct Term* poly1, struct Term* poly2, struct Term** result) {
while (poly1) {
insert(result, poly1->coeff, poly1->x_exp, poly1->y_exp, poly1->z_exp);
poly1 = poly1->next;
}
while (poly2) {
insert(result, poly2->coeff, poly2->x_exp, poly2->y_exp, poly2->z_exp);
poly2 = poly2->next;
}
}
int main() {
struct Term* POLY1 = NULL;
struct Term* POLY2 = NULL;
struct Term* POLYSUM = NULL;
insert(&POLY1, 6, 2, 2, 1);
insert(&POLY1, -4, 0, 1, 5);
insert(&POLY1, 3, 3, 1, 1);
insert(&POLY2, 2, 1, 5, 1);
insert(&POLY2, -2, 1, 1, 3);
printf("POLY1(x,y,z) = ");
display(POLY1);
printf("POLY2(x,y,z) = ");
display(POLY2);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
int main() {
struct Node* root = NULL;
int choice, key;
int elements[] = {6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2};
int n = sizeof(elements) / sizeof(elements[0]);
for (int i = 0; i < n; i++) root = insert(root, elements[i]);
do {
printf("\n----- MENU -----\n");
switch (choice) {
case 1: printf("Inorder Traversal: "); inorder(root); printf("\n");
break;
case 2: printf("Preorder Traversal: "); preorder(root);
printf("\n"); break;
case 3: printf("Postorder Traversal: "); postorder(root);
printf("\n"); break;
case 4: printf("Enter element to search: "); scanf("%d", &key);
if (search(root, key)) printf("Element found in the
BST.\n");
else printf("Element not found in the BST.\n");
break;
case 5: printf("Exiting...\n"); break;
default: printf("Invalid choice! Please enter a number between 1 and
5.\n");
}
} while (choice != 5);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
struct Graph {
int vertices;
bool adj[MAX_CITIES][MAX_CITIES];
};
visited[start] = true;
queue[rear++] = start;
int main() {
struct Graph graph;
initGraph(&graph);
int start;
printf("Enter starting city: ");
scanf("%d", &start);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int key[20], n, m;
int *ht, indX;
int count = 0;
void insert(int key) {
indX = key % m;
while (ht[indX] != -1) {
indX = (indX + 1) % m;
}
ht[indX] = key;
count++;
}
void display() {
int i;
if (count == 0) {
printf("\nHash Table is empty");
return;
}
printf("\nHash Table contents are:\n ");
for (i = 0; i < m; i++) printf("\n T[%d] --> %d ", i, ht[i]);
}
void main() {
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);
ht = (int *)malloc(m * sizeof(int));
for (i = 0; i < m; i++) ht[i] = -1;
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for (i = 0; i < n; i++) scanf("%d", &key[i]);
for (i = 0; i < n; i++) {
if (count == m) {
printf(
"\n~~~Hash table is full. Cannot insert the record %d key~~~",
i + 1);
break;
}
insert(key[i]);
}
// Displaying Keys inserted into hash table
display();
}