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

Shortened_DSA_Programs_Full

Shorted DSA lab programs..

Uploaded by

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

Shortened_DSA_Programs_Full

Shorted DSA lab programs..

Uploaded by

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

Experiment 1: Weekly Calendar

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

char dayName[10];

int date;

char activity[30];

} Day;

void create(Day *calendar, int size) {

for (int i = 0; i < size; i++) {

printf("Enter details for Day %d:\n", i + 1);

printf("Day Name: "); scanf("%s", calendar[i].dayName);

printf("Date: "); scanf("%d", &calendar[i].date);

printf("Activity: "); scanf(" %[^

]", calendar[i].activity);

void display(Day *calendar, int size) {

printf("\nWeekly Schedule:\n");

for (int i = 0; i < size; i++) {

printf("%s (%d): %s\n", calendar[i].dayName, calendar[i].date,

calendar[i].activity);

}
}

int main() {

Day calendar[7];

create(calendar, 7);

display(calendar, 7);

return 0;

Experiment 2: String Pattern Matching


#include <stdio.h>

#include <string.h>

void replacePattern(char *str, char *pat, char *rep) {

char result[100] = "";

char *pos;

while ((pos = strstr(str, pat)) != NULL) {

strncat(result, str, pos - str);

strcat(result, rep);

str = pos + strlen(pat);

strcat(result, str);

strcpy(str, result);

int main() {

char str[100], pat[20], rep[20];


printf("Enter main string: "); gets(str);

printf("Enter pattern: "); gets(pat);

printf("Enter replacement: "); gets(rep);

replacePattern(str, pat, rep);

printf("Result: %s\n", str);

return 0;

Experiment 3: Stack Operations


#include <stdio.h>

#define MAX 5

int stack[MAX], top = -1;

void push(int val) {

if (top == MAX - 1) printf("Stack Overflow\n");

else stack[++top] = val;

void pop() {

if (top == -1) printf("Stack Underflow\n");

else printf("Popped: %d\n", stack[top--]);

void display() {

if (top == -1) printf("Stack is Empty\n");

else for (int i = top; i >= 0; i--) printf("%d\n", stack[i]);


}

int main() {

int choice, val;

while (1) {

printf("1. Push 2. Pop 3. Display 4. Exit\n");

scanf("%d", &choice);

switch (choice) {

case 1: printf("Enter value: "); scanf("%d", &val); push(val); break;

case 2: pop(); break;

case 3: display(); break;

case 4: return 0;

default: printf("Invalid choice\n");

Experiment 4: Infix to Postfix


#include <stdio.h>

#include <ctype.h>

char stack[50];

int top = -1;

void push(char c) { stack[++top] = c; }

char pop() { return stack[top--]; }

int precedence(char op) {


if (op == '+' || op == '-') return 1;

if (op == '*' || op == '/') return 2;

return 0;

void infixToPostfix(char *infix, char *postfix) {

int i = 0, j = 0;

char c;

while ((c = infix[i++]) != '\0') {

if (isalnum(c)) postfix[j++] = c;

else if (c == '(') push(c);

else if (c == ')') while (stack[top] != '(') postfix[j++] = pop();

else {

while (top != -1 && precedence(stack[top]) >= precedence(c)) postfix[j++] =

pop();

push(c);

while (top != -1) postfix[j++] = pop();

postfix[j] = '\0';

int main() {

char infix[50], postfix[50];

printf("Enter infix expression: "); scanf("%s", infix);

infixToPostfix(infix, postfix);

printf("Postfix: %s\n", postfix);


return 0;

Experiment 5A: Postfix Expression Evaluation


#include <stdio.h>

#include <ctype.h>

float stack[20];

int top = -1;

void push(float val) { stack[++top] = val; }

float pop() { return stack[top--]; }

float evaluatePostfix(char *expr) {

for (int i = 0; expr[i] != '\0'; i++) {

if (isdigit(expr[i])) push(expr[i] - '0');

else {

float op2 = pop(), op1 = pop();

switch (expr[i]) {

case '+': push(op1 + op2); break;

case '-': push(op1 - op2); break;

case '*': push(op1 * op2); break;

case '/': push(op1 / op2); break;

return pop();
}

int main() {

char expr[20];

printf("Enter postfix expression: ");

scanf("%s", expr);

printf("Result: %.2f\n", evaluatePostfix(expr));

return 0;

Experiment 5B: Tower of Hanoi


#include <stdio.h>

void hanoi(int n, char src, char aux, char dest) {

if (n == 0) return;

hanoi(n - 1, src, dest, aux);

printf("Move disk %d from %c to %c\n", n, src, dest);

hanoi(n - 1, aux, src, dest);

int main() {

int n;

printf("Enter number of disks: ");

scanf("%d", &n);

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

return 0;

}
Experiment 6: Circular Queue
#include <stdio.h>

#define SIZE 5

int queue[SIZE], front = -1, rear = -1;

int isFull() { return (rear + 1) % SIZE == front; }

int isEmpty() { return front == -1; }

void enqueue(int val) {

if (isFull()) printf("Queue Overflow\n");

else {

if (isEmpty()) front = 0;

rear = (rear + 1) % SIZE;

queue[rear] = val;

void dequeue() {

if (isEmpty()) printf("Queue Underflow\n");

else {

printf("Dequeued: %d\n", queue[front]);

if (front == rear) front = rear = -1;

else front = (front + 1) % SIZE;

}
void display() {

if (isEmpty()) printf("Queue is Empty\n");

else {

printf("Queue: ");

for (int i = front; i != rear; i = (i + 1) % SIZE) printf("%d ", queue[i]);

printf("%d\n", queue[rear]);

int main() {

int choice, val;

while (1) {

printf("1. Enqueue 2. Dequeue 3. Display 4. Exit\n");

scanf("%d", &choice);

switch (choice) {

case 1: printf("Enter value: "); scanf("%d", &val); enqueue(val); break;

case 2: dequeue(); break;

case 3: display(); break;

case 4: return 0;

default: printf("Invalid choice\n");

Experiment 7: Singly Linked List


#include <stdio.h>

#include <stdlib.h>
typedef struct Node {

int data;

struct Node *next;

} Node;

Node *head = NULL;

void insertAtFront(int val) {

Node *newNode = (Node *)malloc(sizeof(Node));

newNode->data = val;

newNode->next = head;

head = newNode;

void insertAtEnd(int val) {

Node *newNode = (Node *)malloc(sizeof(Node));

newNode->data = val;

newNode->next = NULL;

if (head == NULL) head = newNode;

else {

Node *temp = head;

while (temp->next) temp = temp->next;

temp->next = newNode;

}
void deleteAtFront() {

if (head == NULL) printf("List is Empty\n");

else {

Node *temp = head;

head = head->next;

printf("Deleted: %d\n", temp->data);

free(temp);

void display() {

Node *temp = head;

if (!temp) printf("List is Empty\n");

else while (temp) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

int choice, val;

while (1) {

printf("1. Insert Front 2. Insert End 3. Delete Front 4. Display 5. Exit\n");

scanf("%d", &choice);

switch (choice) {

case 1: printf("Enter value: "); scanf("%d", &val); insertAtFront(val);


break;

case 2: printf("Enter value: "); scanf("%d", &val); insertAtEnd(val); break;

case 3: deleteAtFront(); break;

case 4: display(); break;

case 5: return 0;

default: printf("Invalid choice\n");

Experiment 8: Doubly Linked List


#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node *prev, *next;

} Node;

Node *head = NULL, *tail = NULL;

void insertAtFront(int val) {

Node *newNode = (Node *)malloc(sizeof(Node));

newNode->data = val;

newNode->prev = NULL;

newNode->next = head;

if (head) head->prev = newNode;


else tail = newNode;

head = newNode;

void insertAtEnd(int val) {

Node *newNode = (Node *)malloc(sizeof(Node));

newNode->data = val;

newNode->next = NULL;

newNode->prev = tail;

if (tail) tail->next = newNode;

else head = newNode;

tail = newNode;

void deleteAtFront() {

if (!head) printf("List is Empty\n");

else {

Node *temp = head;

head = head->next;

if (head) head->prev = NULL;

else tail = NULL;

printf("Deleted: %d\n", temp->data);

free(temp);

void deleteAtEnd() {
if (!tail) printf("List is Empty\n");

else {

Node *temp = tail;

tail = tail->prev;

if (tail) tail->next = NULL;

else head = NULL;

printf("Deleted: %d\n", temp->data);

free(temp);

void display() {

Node *temp = head;

if (!temp) printf("List is Empty\n");

else {

printf("List: ");

while (temp) {

printf("%d <-> ", temp->data);

temp = temp->next;

printf("NULL\n");

int main() {

int choice, val;

while (1) {
printf("1. Insert Front 2. Insert End 3. Delete Front 4. Delete End 5. Display

6. Exit\n");

scanf("%d", &choice);

switch (choice) {

case 1: printf("Enter value: "); scanf("%d", &val); insertAtFront(val);

break;

case 2: printf("Enter value: "); scanf("%d", &val); insertAtEnd(val); break;

case 3: deleteAtFront(); break;

case 4: deleteAtEnd(); break;

case 5: display(); break;

case 6: return 0;

default: printf("Invalid choice\n");

You might also like