[go: up one dir, main page]

0% found this document useful (0 votes)
21 views31 pages

Dsaf

The document contains multiple C programs that demonstrate various data structures and algorithms. It includes implementations for a calendar management system, string pattern matching, stack operations, infix to postfix conversion, postfix evaluation, Tower of Hanoi, circular queue operations, singly linked list for student records, and doubly linked list for employee records. Each program provides a menu-driven interface for user interaction.

Uploaded by

jayadev.nishan
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)
21 views31 pages

Dsaf

The document contains multiple C programs that demonstrate various data structures and algorithms. It includes implementations for a calendar management system, string pattern matching, stack operations, infix to postfix conversion, postfix evaluation, Tower of Hanoi, circular queue operations, singly linked list for student records, and doubly linked list for employee records. Each program provides a menu-driven interface for user interaction.

Uploaded by

jayadev.nishan
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/ 31

1

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

struct Day { char


*dayName;

int date;
char *activity;

};

void create(struct Day *day) {


day->dayName = (char *)malloc(sizeof(char) * 20);

day->activity = (char *)malloc(sizeof(char) * 100);


printf("Enter the day name: ");

scanf("%s", day->dayName);

printf("Enter the date: ");

scanf("%d", &day->date);

printf("Enter the activity for the day: ");


scanf(" %[^\n]s", day->activity);

void read(struct Day *calendar, int size) {


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

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


create(&calendar[i]);

}
}

void display(struct Day *calendar, int size) {

printf("\nWeek's Activity Details:\n");


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

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


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

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

printf("Activity: %s\n", calendar[i].activity);


printf("\n");

}
}

void freeMemory(struct Day *calendar, int size) {

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

free(calendar[i].dayName);

free(calendar[i].activity);

}
int main() {

int size;
printf("Enter the number of days in the week: ");

scanf("%d", &size);

struct Day *calendar = (struct Day


*)malloc(sizeof(struct Day) * size);

if (calendar == NULL) {

printf("Memory allocation failed. Exiting


program.\n");

return 1;
}

read(calendar, size);

display(calendar, size);

freeMemory(calendar, size);
free(calendar);

return 0;

2
#include <stdio.h>

char str[50], pat[20], rep[20], res[50];


int c = 0, m = 0, i = 0, j = 0, k, flag = 0;

void stringmatch() {
while (str[c] != '\0') {
if (str[m] == pat[i]) {
i++;
m++;
if (pat[i] == '\0') {
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++) {
res[j] = rep[k];
}
i = 0;
c = m;
}
} else {
res[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
res[j] = '\0';
}

int main() {
printf("Enter the main string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';

printf("Enter the pattern string: ");


fgets(pat, sizeof(pat), stdin);
pat[strcspn(pat, "\n")] = '\0';

printf("Enter the replace string: ");


fgets(rep, sizeof(rep), stdin);
rep[strcspn(rep, "\n")] = '\0';

printf("\nThe string before pattern match is:\n%s", str);


stringmatch();

if (flag == 1)
printf("\nThe string after pattern match and replace is:\n%s", res);
else
printf("\nPattern string not found");

return 0;
}

#include<stdio.h>

#include<stdlib.h>
#define MAX 3

int s[MAX]; int

top = -1;
void push(int item);

int pop(); void

palindrome(); void

display();

void main()
{
int choice, item;

while (1)

{
printf("\n\n\n\n-----------Menu----------- : ");

printf("\n=>1.Push an Element to Stack and Overflow demo ");

printf("\n=>2.Pop an Element from Stack and Underflow demo");

printf("\n=>3.Palindrome demo ");

printf("\n=>4.Display ");

printf("\n=>5.Exit");

printf("\nEnter your choice: ");

scanf("%d", & choice);

switch (choice)

case 1:
printf("\nEnter an element to be pushed: ");

scanf("%d", & item);

push(item);

break;

case 2:

item = pop();

if (item != -1)

printf("\nElement popped is: %d", item);

break;

case 3:

palindrome();
break;
case 4:

display();

break;

case 5:

exit(1);

default:

printf("\nPlease enter valid choice ");

break;

}
}

}
void push(int item)

if (top == MAX - 1)
{

printf("\n-----------Stack overflow-----------");

return;

}
top = top + 1;

s[top] = item;

int pop()
{ int item;

if (top == -1)

{
printf("\n-----------Stack underflow-----------");

return -1;

}
item = s[top];

top = top - 1;

return item;

void display()
{ int I;

if (top == -1)

printf("\n-----------Stack is empty-----------");

return;

}
printf("\nStack elements are:\n ");

for (i = top; i >= 0; i--)


printf("| %d |\n", s[i]);

}
void palindrome()

{ int flag = 1 i;

printf("\nStack content are:\n");

for (i = top; i >= 0; i--)

printf("| %d |\n", s[i]);

printf("\nReverse of stack content are:\n");


for (i = 0; i <= top; i++)

printf("| %d |\n", s[i]);

for (i = 0; i <= top / 2; i++)

{
if (s[i] != s[top - i])

{
flag = 0;

break;

}
if (flag == 1)

printf("\nIt is palindrome number");


}

else
{
printf("\nIt is not a palindrome number");

4
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char c) {
stack[++top] = c;
}
char pop() {
return stack[top--];
}
int precedence(char op) {
switch (op) {
case '^': return 3;
case '*': case '/': case '%': return 2;
case '+': case '-': return 1;
default: return 0;
}
}
void infixToPostfix(char *exp) {
char postfix[SIZE];
int i = 0, j = 0;

while (exp[i] != '\0') {


if (isalnum(exp[i])) {
postfix[j++] = exp[i];
} else if (exp[i] == '(') {
push(exp[i]);
} else if (exp[i] == ')') {
while (top != -1 && stack[top] != '(') {
postfix[j++] = pop();
}
pop(); // Remove '(' from stack
} else {
while (top != -1 && precedence(stack[top]) >= precedence(exp[i])) {
postfix[j++] = pop();
}
push(exp[i]);
}
i++;
}

while (top != -1) {


postfix[j++] = pop();
}
postfix[j] = '\0';

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


}
int main() {
char exp[SIZE];

printf("Enter an infix expression: ");


fgets(exp, SIZE, stdin);
exp[strcspn(exp, "\n")] = '\0';

infixToPostfix(exp);

return 0;
}

5a
#include <stdio.h>
#include <ctype.h>
#include <math.h>

#define SIZE 100

int stack[SIZE];
int top = -1;

void push(int n) {
stack[++top] = n;
}

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

int evaluatePostfix(char *exp) {


int i = 0, op1, op2;

while (exp[i] != '\0') {


if (isdigit(exp[i])) {
push(exp[i] - '0');
} else {
op2 = pop();
op1 = pop();
switch (exp[i]) {
case '+': push(op1 + op2); break;
case '-': push(op1 - op2); break;
case '*': push(op1 * op2); break;
case '/': push(op1 / op2); break;
case '%': push(op1 % op2); break;
case '^': push((int)pow(op1, op2)); break;
}
}
i++;
}
return pop();
}

int main() {
char exp[SIZE];

printf("Enter a postfix expression: ");


fgets(exp, SIZE, stdin);
exp[strcspn(exp, "\n")] = '\0';

printf("Result: %d\n", evaluatePostfix(exp));

return 0;
}

5b

#include <stdio.h>

void tower(int n, int source, int temp, int


destination)

{ if(n==0)

return;

tower(n - 1, source, destination, temp); printf("\nMove disc

%d from %c to %c", n, source, destination); tower(n - 1, temp,

source, destination);

void main()

int n;

printf("\nEnter the number of discs: \n");

scanf("%d", & n);

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

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

6
#include<stdio.h>

#include<stdlib.h>
#define MAX 5

char circular_queue[MAX];

int front = -1, rear = -1;


int isEmpty()

{
if (front == -1 && rear == -1)
return 1;

else

return 0;

int isFull()
{

if ((rear + 1) % MAX == front)

return 1;
else

return 0;

void insertElement(char element) {


if(isFull())

{
printf("Circular Queue Overflow\n");

return;

}
else if (isEmpty())

{
front = rear = 0;

}
else

rear = (rear + 1) % MAX;


}

circular_queue[rear] = element;
}

void deleteElement()

if(isEmpty())

printf("Circular Queue Underflow\n");

return;
}
else if (front == rear)
{

front = rear = -1;

}
else

{
front = (front + 1) % MAX;

}}

void display()

Int i;

if (isEmpty())
{

printf("Circular Queue is empty\n");


return;

}
printf("Circular Queue elements: ");

i = front;

do

{
printf("%c ", circular_queue[i]);

i = (i + 1) % MAX;

while (i != (rear + 1) % MAX);

printf("\n");

}
int main()

{ int choice;

char element;

do

{
printf("\n\n---- Circular Queue Menu ----\n");
printf("1. Insert an Element\n");

printf("2. Delete an Element\n");

printf("3. Display Circular

Queue\n");printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Enter element to be inserted: ");

scanf(" %c", &element);

insertElement(element);

break;

case 2:

deleteElement();

break;
case 3:

display();

break;

case 4:
printf("Exiting...\n");

break;

default:

printf("Invalid choice! Please enter a valid option.\n");

}
}

while(choice != 4);

return 0;

7)
#include <stdio.h>

#include <stdlib.h>
#include <string.h>
struct Student {

char usn[20], name[50], programme[50],


phone[15];

int sem;
struct Student *next;

};

typedef struct Student *Node;

Node createNode() {

Node newNode = (Node)malloc(sizeof(struct


Student));
if (!newNode) {

printf("Memory allocation failed!\n");


exit(1);

}
printf("Enter USN: ");

scanf("%s", newNode->usn);

printf("Enter Name: ");

scanf("%s", newNode->name);

printf("Enter Programme: ");


scanf("%s", newNode->programme);

printf("Enter Semester: ");

scanf("%d", &newNode->sem);

printf("Enter Phone Number: ");

scanf("%s", newNode->phone);

newNode->next = NULL;

return newNode;

}
Node insertFront(Node head) {

Node newNode = createNode();

newNode->next = head;

return newNode;
}

Node insertEnd(Node head) {

Node newNode = createNode();

if (!head) return newNode;

Node temp = head;

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


temp->next = newNode;

return head;

}
Node deleteFront(Node head) {

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

return NULL;

}
Node temp = head;

head = head->next;

free(temp);

printf("Deleted from front.\n");

return head;
}

Node deleteEnd(Node head) {

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

return NULL;

}
if (!head->next) {

free(head);
printf("Deleted from end.\n");

return NULL;

}
Node temp = head;

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


free(temp->next); temp->next

= NULL; printf("Deleted from

end.\n");

return head;

void display(Node head) {

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

return;
}

Node temp = head; int

count = 0;

printf("Student List:\n");

while (temp) {
printf("USN: %s | Name: %s | Programme: %s |
Sem: %d | Phone: %s\n",

temp->usn, temp->name, temp-


>programme, temp->sem, temp->phone);

temp = temp->next;

count++;
}

printf("Total Students: %d\n", count);


}

int main() {

Node head = NULL;


int choice;

while (1) {
printf("\n--- MENU ---\n");

printf("1. Insert at Front\n");


printf("2. Display and Count\n");

printf("3. Insert at End\n");

printf("4. Delete from Front\n");

printf("5. Delete from End\n");

printf("6. Exit\n");

printf("Enter your choice: ");


scanf("%d", &choice);

switch (choice) {

case 1: head = insertFront(head); break;


case 2: display(head); break;

case 3: head = insertEnd(head); break;

case 4: head = deleteFront(head); break;

case 5: head = deleteEnd(head); break;

case 6: exit(0);
default: printf("Invalid choice!\n");

}
}

return 0;

8)
#include <stdio.h>

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

struct Employee {

char ssn[20], name[50], dept[50], designation[50],


phone[15];

float sal;

struct Employee *prev, *next;


};

typedef struct Employee *Node;

Node createNode() {
Node newNode = (Node)malloc(sizeof(struct
Employee));
if (!newNode) {

printf("Memory allocation failed!\n");


exit(1);

printf("Enter SSN: ");

scanf("%s", newNode->ssn);

printf("Enter Name: ");

scanf("%s", newNode->name);

printf("Enter Dept: ");

scanf("%s", newNode->dept);

printf("Enter Designation: ");

scanf("%s", newNode->designation);
printf("Enter Salary: ");

scanf("%f", &newNode->sal);

printf("Enter Phone Number: ");

scanf("%s", newNode->phone);

newNode->prev = newNode->next = NULL;


return newNode;

}
Node insertEnd(Node head) {

Node newNode = createNode();

if (!head) return newNode;

Node temp = head;

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


temp->next = newNode;

newNode->prev = temp;

return head;
}

Node insertFront(Node head) {


Node newNode = createNode();

if (!head) return newNode;

newNode->next = head;

head->prev = newNode;
return newNode;

}
Node deleteFront(Node head) {

if (!head) {

printf("List is empty!\n");
return NULL;

}
Node temp = head;

head = head->next;

if (head) head->prev = NULL;


free(temp);

printf("Deleted from front.\n");


return head;

Node deleteEnd(Node head) {


if (!head) {

printf("List is empty!\n");
return NULL;

}
if (!head->next) {

free(head);

printf("Deleted from end.\n");


return NULL;

}
Node temp = head;

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

temp->prev->next = NULL;
free(temp);

printf("Deleted from end.\n");


return head;

}
void display(Node head) {

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

return;

}
Node temp = head; int

count = 0;

printf("Employee List:\n");

while (temp) {

printf("SSN: %s | Name: %s | Dept: %s | Desig:


%s | Sal: %.2f | Phone: %s\n",

temp->ssn, temp->name, temp->dept,


temp->designation, temp->sal, temp->phone);
temp = temp->next;

count++;
}

printf("Total Employees: %d\n", count);

}
int main() {

Node head = NULL;


int choice;

while (1) {

printf("\n--- MENU ---\n");

printf("1. Insert at End\n");

printf("2. Display and Count\n");

printf("3. Insert at Front\n");

printf("4. Delete from Front\n");


printf("5. Delete from End\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1: head = insertEnd(head); break;


case 2: display(head); break;

case 3: head = insertFront(head); break;

case 4: head = deleteFront(head); break;

case 5: head = deleteEnd(head); break;

case 6: exit(0);
default: printf("Invalid choice!\n");

}
return 0;

9)

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

#include <math.h>
struct Term {

int coeff, x, y, z;
struct Term *next;

};

typedef struct Term *Node;


Node createTerm(int coeff, int x, int y, int z) {

Node newTerm = (Node)malloc(sizeof(struct


Term));
newTerm->coeff = coeff;

newTerm->x = x;

newTerm->y = y;
newTerm->z = z;

newTerm->next = newTerm;
return newTerm;

}
Node insertEnd(Node head, int coeff, int x, int y, int
z) {
Node newTerm = createTerm(coeff, x, y, z);
if (!head) return newTerm;

Node temp = head;


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

temp->next = newTerm;

newTerm->next = head;

return head;

}
void display(Node head) {

if (!head) {

printf("Polynomial is empty!\n");
return;

}
Node temp = head;

do {
printf("%+dx^%dy^%dz^%d ", temp->coeff,
temp->x, temp->y, temp->z);
temp = temp->next;
} while (temp != head);

printf("\n");

}
int evaluate(Node head, int x, int y, int z) {

int result = 0;
Node temp = head;

do {
result += temp->coeff * pow(x, temp->x) *
pow(y, temp->y) * pow(z, temp->z);
temp = temp->next;

} while (temp != head);


return result;

Node addPolynomials(Node p1, Node p2) {

Node sum = NULL, t1 = p1;

do {
sum = insertEnd(sum, t1->coeff, t1->x, t1->y,
t1>z);
t1 = t1->next;
} while (t1 != p1);

Node t2 = p2;

do {
sum = insertEnd(sum, t2->coeff, t2->x, t2->y, t2-
>z);

t2 = t2->next;

} while (t2 != p2);

return sum;

int main() {
Node poly1 = NULL, poly2 = NULL, polySum=NULL;

int n, coeff, x, y, z;

printf("Enter number of terms for POLY1: ");


scanf("%d", &n);

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


printf("Enter coeff, x, y, z: ");

scanf("%d %d %d %d", &coeff, &x, &y, &z);


poly1 = insertEnd(poly1, coeff, x, y, z);

printf("POLY1: ");
display(poly1);

printf("Enter number of terms for POLY2: ");


scanf("%d", &n);

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


printf("Enter coeff, x, y, z: ");

scanf("%d %d %d %d", &coeff, &x, &y, &z);


poly2 = insertEnd(poly2, coeff, x, y, z);

printf("POLY2: ");
display(poly2);

polySum = addPolynomials(poly1, poly2);


printf("POLYSUM: ");

display(polySum);

printf("Enter values for x, y, z to evaluate POLY1:");


scanf("%d %d %d", &x, &y, &z);

int result = evaluate(poly1, x, y, z);


printf("Evaluation Result: %d\n", result);

return 0;

10)
#include <stdio.h>

#include <stdlib.h>
struct Node {

int data;

struct Node *left, *right;


};

struct Node* createNode(int data) {


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

newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;

}
struct Node* insert(struct Node* root, int data) {

if (root == NULL) return createNode(data);


if (data < root->data)

root->left = insert(root->left, data);


else if (data > root->data)

root->right = insert(root->right, data);

return root;
}

void inorder(struct Node* root) {


if (root) {

inorder(root->left);

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

inorder(root->right);

}
}

void preorder(struct Node* root) {

if (root) {
printf("%d ", root->data);

preorder(root->left);

preorder(root->right);

}
}

void postorder(struct Node* root) {

if (root) {
postorder(root->left);

postorder(root->right);

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

}
struct Node* search(struct Node* root, int key) {

if (!root || root->data == key)


return root;

if (key < root->data)


return search(root->left, key);

return search(root->right, key);


}

int main() {

struct Node* root = NULL;


int choice, key, n;

int elements[] = {6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2};


while (1) {

printf("\n--- MENU ---\n");

printf("1. Create BST\n");

printf("2. Inorder Traversal\n");

printf("3. Preorder Traversal\n");

printf("4. Postorder Traversal\n");

printf("5. Search an Element\n");

printf("6. Exit\n");
printf("Enter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1:
for (int i = 0; i <
sizeof(elements)/sizeof(elements[0]); i++)

root = insert(root, elements[i]);


printf("BST Created.\n");

break;

case 2:

printf("Inorder: ");

inorder(root);
printf("\n");

break;
case 3:

printf("Preorder: ");

preorder(root);

printf("\n");
break;

case 4:

printf("Postorder: ");

postorder(root);

printf("\n");
break;

case 5:

printf("Enter element to search: ");


scanf("%d", &key);

if (search(root, key))
printf("Element %d found in the BST.\n", key);

else
printf("Element %d not found in the BST.\n", key);

break;

case 6:
exit(0);

default:

printf("Invalid choice!\n");

}
return 0;

11)

#include <stdio.h>
#include <stdlib.h>
#define MAX 10

int adj[MAX][MAX], visited[MAX], n;


void createGraph() {

int i, j;
printf("Enter the number of cities: ");

scanf("%d", &n);
printf("Enter the adjacency matrix (0 for no edge,
1 for edge):\n");

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


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

scanf("%d", &adj[i][j]);

}
}

}
void DFS(int v) {

printf("%d ", v);

visited[v] = 1;
for (int i = 0; i < n; i++) {

if (adj[v][i] == 1 && !visited[i])

DFS(i);

}
}

void BFS(int start) {

int queue[MAX], front = 0, rear = 0;


printf("%d ", start);

visited[start] = 1;

queue[rear++] = start;

while (front < rear) {

int v = queue[front++];
for (int i = 0; i < n; i++) {

if (adj[v][i] == 1 && !visited[i]) {


printf("%d ", i);

visited[i] = 1;
queue[rear++] = i;

int main() {

int choice, start;


while (1) {

printf("\n--- MENU ---\n");

printf("1. Create Graph\n");

printf("2. DFS Traversal\n");

printf("3. BFS Traversal\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

createGraph();

break;
case 2:

printf("Enter starting node for DFS: ");


scanf("%d", &start);

for (int i = 0; i < n; i++) visited[i] = 0;

printf("DFS Traversal: ");

DFS(start);

printf("\n");
break;

case 3:

printf("Enter starting node for BFS: ");


scanf("%d", &start);

for (int i = 0; i < n; i++) visited[i] = 0;


printf("BFS Traversal: ");

BFS(start);
printf("\n");

break;
case 4:

exit(0);

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

return 0;

}
---------------------------------------------------------------------

12)
#include<stdio.h>

#include<stdlib.h>

int key[20], n, m;

int * ht, index;

int count = 0;
void insert(int key)

{
index = key % m;

while (ht[index] != -1)

{
index = (index + 1) % m;

}
ht[index] = 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]);

display();
}

You might also like