0% found this document useful (0 votes)
21 views

DSA Smol

Uploaded by

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

DSA Smol

Uploaded by

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

1.

CALENDAR (48)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Day {

char* name;

int date;

char* activity;

};

struct Day* calendar[7];

void create() {

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

calendar[i] = (struct Day*)malloc(sizeof(struct Day));

calendar[i]->name = (char*)malloc(20 * sizeof(char)); // Assuming the maximum length of the day


nameis 20

calendar[i]->activity = (char*)malloc(100 * sizeof(char)); // Assuming the maximum length of


theactivity description is 100

void read() {

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

printf("Enter name of day %d: ", i + 1);

scanf("%s", calendar[i]->name);

printf("Enter date of day %d: ", i + 1);

scanf("%d", &calendar[i]->date);

printf("Enter activity for day %d: ", i + 1);

scanf("%s", calendar[i]->activity);

void display() {

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

printf("| %-10s | %-10s | %-30s |\n", "Day", "Date", "Activity");


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

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

printf("| %-10s | %-10d | %-30s |\n", calendar[i]->name, calendar[i]->date, calendar[i]-


>activity);

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

int main() {

create();

printf("Reading Calendar:\n");

read();

printf("Displaying Calendar:\n");

display();

// Free memory

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

free(calendar[i]->name);

free(calendar[i]->activity);

free(calendar[i]);

return 0;

2.STRING REPLACEMENT (30)

#include<stdio.h>

void main()
{
char s[200],pat[100],rep[100],ans[200];
int i,j,k,l,flag;
printf("\nEnter string:");
scanf("%s",s);
printf("\nEnter pattern:");
scanf("%s",pat);
printf("\nEnter replacement:");
scanf("%s",rep);
for(i=0,k=0;s[i]!='\0';i++)
{
flag=1;
for( j=0;pat[ j]!='\0';j++)
if(s[i+j]!=pat[ j])
flag=0;
l=j;
if(flag)
{
for( j=0;rep[ j]!='\0';j++,k++)
ans[k]=rep[ j];
i+=l-1;
}
else
ans[k++]=s[i];
}
ans[k]='\0';
printf("%s",ans);

3.STACK OPERATIONS (72)

#include <stdio.h>
#include <stdlib.h>
int s[5],top=-1;

void push()
{
if(top==4)
printf("\nStack overflow!!!!");
else
{
printf("\nEnter element to insert:");
scanf("%d",&s[++top]);
}
}

void pop()
{
if(top==-1)
printf("\nStack underflow!!!");
else
printf("\nElement popped is: %d",s[top--]);
}
void disp()
{
int t=top;
if(t==-1)
printf("\nStack empty!!");
else
printf("\nStack elements are:\n");
while(t>=0)
printf("%d ",s[t--]);
}
void pali()
{
int num[5],rev[5],i,t;
for(i=0,t=top;t>=0;i++,t--)
num[i]=rev[t]=s[t];
for(i=0;i<=top;i++)
if(num[i]!=rev[i])
break;
/*printf(" num rev\n");
for(t=0;t<=top;t++)
printf("%4d %4d\n",num[t],rev[t]);*///remove /* */ to display num and rev
if(i==top+1)
printf("\nIt is a palindrome");
else
printf("\nIt is not a palindrome");
}

int main()
{
int ch;
do
{
printf("\n...Stack operations.....\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n________________\n");
printf("Enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop();break;
case 3:pali();break;
case 4:disp();break;
case 5:exit(0);
default:printf("\nInvalid choice");
}
}
while(1);
return 0;

}
4.INFIX TO POSTFIX (67)

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

int F(char symbol)


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

int G(char symbol)


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

void infix_postfix(char infix[], char postfix[])


{
int top=-1, j=0, i;
char s[30], symbol;
s[++top] = '#';
for(i=0; i < strlen(infix); i++)
{
symbol = infix[i];
while (F(s[top]) > G(symbol))
{
postfix[ j] = s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
}
while(s[top] != '#')
postfix[ j++] = s[top--];
postfix[ j] = '\0';
}

void main()
{
char infix[20], postfix[20];
printf("\nEnter a valid infix expression\n") ;
scanf ("%s", infix) ;
infix_postfix (infix, postfix);
printf("\nThe infix expression is:\n");
printf ("%s",infix);
printf("\nThe postfix expression is:\n");
printf ("%s",postfix) ;

5a.EVALUATION (41)

#include<stdio.h>

#include<math.h>

#include<string.h>

#include<ctype.h>

float compute(char symbol, float op1, float op2)

switch (symbol)

{
case '+': return op1 + op2;

case '-': return op1 - op2;

case '*': return op1 * op2;

case '/': return op1 / op2;

case '$':

case '^': return pow(op1,op2);

default : return 0;

void main()

float s[20], res, op1, op2;

int top, i;

char postfix[20], symbol;

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

scanf ("%s", postfix);

top=-1;

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

symbol = postfix[i];

if(isdigit(symbol))

s[++top]=symbol - '0';

else

op2 = s[top--];

op1 = s[top--];

res = compute(symbol, op1, op2);

s[++top] = res;

}
res = s[top--];

printf("\nThe result is : %f\n", res);

5b.TOWER OF HANOI (20)

#include<stdio.h>
#include<math.h>
void tower(int n, int source, int temp, int destination);

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\n");
scanf("%d", &n);
printf("\nThe sequence of moves involved in the Tower of Hanoi are\n");
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d\n", (int)pow(2,n)-1);

6.CIRCULAR QUEUES (66)

#include <stdio.h>
#include <stdlib.h>
#define max 5
int q[max],f=-1,r=-1;
void ins()
{
if(f==(r+1)%max)
printf("\nQueue overflow");
else
{
if(f==-1)
f++;
r=(r+1)%max;
printf("\nEnter element to be inserted:");
scanf("%d",&q[r]);
}
}
void del()
{
if(r==-1)
printf("\nQueue underflow");
else
{
printf("\nElemnt deleted is:%d",q[f]);
if(f==r)
f=r=-1;
else
f=(f+1)%max;
}
}
void disp()
{
if(f==-1)
printf("\nQueue empty");
else
{
int i;
printf("\nQueue elements are:\n");
for(i=f;i!=r;i=(i+1)%max)
printf("%d\t",q[i]);
printf("%d",q[i]);
printf("\nFront is at:%d\nRear is at:%d",q[f],q[r]);
}
}
int main()
{
printf("\nCircular Queue operations");
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.Display");
printf("\n4.Exit");
int ch;
do{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:ins();break;
case 2:del();break;
case 3:disp();break;
case 4:exit(0);
default:printf("\nInvalid choice...!");
}
}while(1);
return 0;

7.SLL (109)

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
struct stud
{
char usn[11],name[15],branch[4],phno[11];
int sem;
struct stud *next;
}*f=NULL,*r=NULL,*t=NULL;
void ins(int ch)
{
t=(struct stud*)malloc(sizeof(struct stud));
printf("\nEnter USN:");
scanf("%s",t->usn);
printf("Enter Name:");
scanf("%s",t->name);
printf("Enter Branch:");
scanf("%s",t->branch);
printf("Enter Sem:");
scanf("%d",&t->sem);
printf("Enter Phno:");
scanf("%s",t->phno);
t->next=NULL;
if(!r)
f=r=t;
else
{
if(ch)
{
r->next=t;
r=t;
}
else
{
t->next=f;
f=t;
}
}
}
void del(int ch)
{
if(!f)
printf("\nList Empty");
else
{
struct stud *t1;
if(f==r)
{
t1=f;
f=r=NULL;
}
else if(ch)
{
t1=r;
for(t=f;t->next!=r;t=t->next)
r=t;
r->next=NULL;
}
else
{
t1=f;
f=f->next;
}
printf("\nElement deleted is:\n");
printf("USN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\n",t1->usn,t1->name,t1->branch,t1-
>sem,t1->phno);
free(t1);
}
}
void disp()
{
if(!f)
printf("\nList Empty!!!");
else
printf("\nList elements are:\n");
for(t=f;t;t=t->next)
printf("\nUSN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\n",t->usn,t->name,t->branch,t-
>sem,t->phno);
}
void main()
{
int ch,n,i;
printf("\n........Menu..........,\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert at end\n");
printf("4.Delete at end\n");
printf("5.Insert at beg\n");
printf("6.Delete at beg\n");
printf("7.Exit\n");
while(1)
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter no. of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
ins(0);
break;
case 2:disp();break;
case 3:ins(1);break;
case 4:del(1);break;
case 5:ins(0);break;
case 6:del(0);break;
case 7:exit(0);
default:printf("\nInvalid choice!!!!");
}
}

8.DLL

#include <stdio.h>

#include <stdlib.h>

struct Employee {

char ssn[25], name[25], dept[10], designation[25];

int sal;

long int phNo;

struct Employee *prev, *next;


};

typedef struct Employee *NODE;

NODE createNode() {

NODE newNode = (NODE)malloc(sizeof(struct Employee));

if (newNode == NULL) {

printf("\nMemory allocation failed.");

exit(1);

printf("\nEnter SSN, Name, Department, Designation, Salary, Phone Number: ");

scanf("%s %s %s %s %d %ld", newNode->ssn, newNode->name, newNode->dept, newNode-


>designation, &newNode->sal, &newNode->phNo);

newNode->prev = NULL;

newNode->next = NULL;

return newNode;

NODE insertEnd(NODE head) {

NODE newNode = createNode();

if (head == NULL) {

return newNode;

NODE temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

return head;

}
void display(NODE head) {

if (head == NULL) {

printf("\nDLL is empty.");

return;

NODE temp = head;

int count = 0;

printf("\nEmployee Data in DLL:\n");

while (temp != NULL) {

printf("SSN: %s | Name: %s | Dept: %s | Designation: %s | Salary: %d | Phone: %ld\n",

temp->ssn, temp->name, temp->dept, temp->designation, temp->sal, temp->phNo);

temp = temp->next;

count++;

printf("\nNumber of nodes in DLL: %d\n", count);

NODE deleteEnd(NODE head) {

if (head == NULL) {

printf("\nDLL is empty.");

return NULL;

if (head->next == NULL) {

free(head);

return NULL;

NODE temp = head;

while (temp->next != NULL) {

temp = temp->next;

}
temp->prev->next = NULL;

free(temp);

return head;

NODE insertFront(NODE head) {

NODE newNode = createNode();

if (head == NULL) {

return newNode;

newNode->next = head;

head->prev = newNode;

return newNode;

NODE deleteFront(NODE head) {

if (head == NULL) {

printf("\nDLL is empty.");

return NULL;

if (head->next == NULL) {

free(head);

return NULL;

NODE temp = head;

head = head->next;

head->prev = NULL;

free(temp);

return head;

int main() {
NODE head = NULL;

int choice;

while (1) {

printf("\nMenu:");

printf("\n1. Create DLL of N Employees (End Insertion)");

printf("\n2. Display DLL");

printf("\n3. Insert at End");

printf("\n4. Delete at End");

printf("\n5. Insert at Front");

printf("\n6. Delete at Front");

printf("\n7. Exit");

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

head = insertEnd(head);

break;

case 2:

display(head);

break;

case 3:

head = insertEnd(head);

break;

case 4:

head = deleteEnd(head);

break;

case 5:

head = insertFront(head);

break;

case 6:

head = deleteFront(head);
break;

case 7:

exit(0);

default:

printf("\nInvalid choice. Please try again.");

return 0;

9.SCLL (134)

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
typedef struct polynomial
{
float coeff;
int x,y,z;
struct polynomial *next;
}poly;
poly *p1,*p2,*p3;
poly* readpoly()
{
poly *temp=(poly*)malloc(sizeof(poly));
printf("\nEnter coeff:");
scanf("%f",&temp->coeff);
printf("Enter x expon:");
scanf("%d",&temp->x);
printf("Enter y expon:");
scanf("%d",&temp->y);
printf("Enter z expon:");
scanf("%d",&temp->z);
return temp;
}
poly* create()
{
int n,i;
printf("\nEnter no. of terms:");
scanf("%d",&n);
poly *temp=(poly*)malloc(sizeof(poly)),*t1=temp;
for(i=0;i<n;i++,t1=t1->next)
t1->next=readpoly();
t1->next=temp;
return temp;
}
void evaluate()
{
float sum=0;
int x,y,z;
poly *t=p1->next;
printf("\nEnter x,y&z:\n");
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
while(t!=p1)
{
sum+=t->coeff*pow(x,t->x)*pow(y,t->y)*pow(z,t->z);
t=t->next;
}
printf("\nSum=%f",sum);
}
void display(poly *p)
{
poly *t=p->next;
while(t!=p)
{
if(t!=p->next&&t->coeff>0)
putchar('+');
printf("%.1fx^%dy^%dz^%d",t->coeff,t->x,t->y,t->z);
t=t->next;
}
}
poly* attach(float coeff,int x,int y,int z,poly *p)
{
poly *t=(poly*)malloc(sizeof(poly));
t->coeff=coeff;
t->x=x;
t->y=y;
t->z=z;
p->next=t;
return t;
}
poly* add()
{
printf("\nPolynomial1:\n");
p1=create();
printf("\nPolynomial2:\n");
p2=create();
int flag;
poly *t1=p1->next,*t2=p2->next,*t3;
p3=(poly*)malloc(sizeof(poly));
t3=p3;
while(t1!=p1&&t2!=p2)
{
if(t1->x>t2->x)
flag=1;
else if(t1->y<t2->y)
flag=-1;
else if(t1->z==t2->z)
flag=0;
switch(flag)
{
case 0:t3=attach(t1->coeff+t2->coeff,t1->x,t1->y,t1->z,t3);
t1=t1->next;
t2=t2->next;
break;
case 1:t3=attach(t1->coeff,t1->x,t1->y,t1->z,t3);
t1=t1->next;
break;
case -1:t3=attach(t2->coeff,t2->x,t2->y,t2->z,t3);
t2=t2->next;
break;
}
}
for(;t1!=p1;t1=t1->next)
t3=attach(t1->coeff,t1->x,t1->y,t1->z,t3);
for(;t2!=p2;t2=t2->next)
t3=attach(t2->coeff,t2->x,t2->y,t2->z,t3);
t3->next=p3;
return p3;
}
int main()
{
int ch;
printf("\n1.Represent and evaluate polynomial\n2.Add 2 polynomials\n3.Exit\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:p1=create();
display(p1);
evaluate();
break;
case 2:p3=add();
printf("\nPolynomial1:\n");
display(p1);
printf("\nPolynomial2:\n");
display(p2);
printf("\nP1+P2:\n");
display(p3);
break;
case 3:exit(0);
default:printf("\nInvalid choice...!");
}
return 0;

10.BST

#include<stdio.h>

#include<stdlib.h>

struct BST {

int data;

struct BST *lchild;

struct BST *rchild;


};

typedef struct BST * NODE;

NODE create(int val) {

NODE temp = (NODE)malloc(sizeof(struct BST));

if (temp == NULL) {

printf("\nMemory allocation failed.");

exit(1);

temp->data = val;

temp->lchild = NULL;

temp->rchild = NULL;

return temp;

NODE insert(NODE root, int val) {

if (root == NULL)

return create(val);

if (val < root->data)

root->lchild = insert(root->lchild, val);

else if (val > root->data)

root->rchild = insert(root->rchild, val);

return root;

void inorder(NODE root) {

if (root != NULL) {

inorder(root->lchild);

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

inorder(root->rchild);

void preorder(NODE root) {

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

preorder(root->lchild);

preorder(root->rchild);

void postorder(NODE root) {

if (root != NULL) {

postorder(root->lchild);

postorder(root->rchild);

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

int search(NODE root, int key) {

while (root != NULL) {

if (key == root->data)

return 1;

else if (key < root->data)

root = root->lchild;

else

root = root->rchild;

return 0;

int main() {

NODE root = NULL;

int choice, key, val;

while(1) {

printf("\n~~~~BST MENU~~~~");

printf("\n1. Insert");

printf("\n2. Traversals");

printf("\n3. Search");
printf("\n4. Exit");

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("\nEnter the value to insert: ");

scanf("%d", &val);

root = insert(root, val);

break;

case 2:

if (root == NULL)

printf("\nTree is empty");

else {

printf("\nPreorder traversal: ");

preorder(root);

printf("\nInorder traversal: ");

inorder(root);

printf("\nPostorder traversal: ");

postorder(root);

break;

case 3:

printf("\nEnter the value to search: ");

scanf("%d", &key);

if (search(root, key))

printf("\nElement found in BST");

else

printf("\nElement not found in BST");

break;

case 4:
exit(0);

return 0;

11.GRAPH

#include<stdio.h>

#include<stdlib.h>

int adjMatrix[50][50], n, visited[50];

int q[50], front = -1, rear = -1;

int s[50], top = -1;

void bfs(int v) {

int i, cur;

visited[v] = 1;

q[++rear] = v;

while (front != rear) {

cur = q[++front];

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

if (adjMatrix[cur][i] == 1 && visited[i] == 0) {

q[++rear] = i;

visited[i] = 1;

printf("%d ", i);

}
void dfs(int v) {

int i;

visited[v] = 1;

s[++top] = v;

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

if (adjMatrix[v][i] == 1 && visited[i] == 0) {

printf("%d ", i);

dfs(i);

int main() {

int ch, start, i, j;

printf("\nEnter the number of vertices in graph: ");

scanf("%d", &n);

printf("\nEnter the adjacency matrix:\n");

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

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

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

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

visited[i] = 0;

printf("\nEnter the starting vertex: ");

scanf("%d", &start);

printf("\n==>1. BFS: Print all nodes reachable from a given starting node");

printf("\n==>2. DFS: Print all nodes reachable from a given starting node");

printf("\n==>3: Exit");

printf("\nEnter your choice: ");

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

case 1:

printf("\nNodes reachable from starting vertex %d are: ", start);

bfs(start);

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

if (visited[i] == 0)

printf("\nThe vertex that is not reachable is %d", i);

break;

case 2:

printf("\nNodes reachable from starting vertex %d are:\n", start);

dfs(start);

break;

case 3:

exit(0);

default:

printf("\nPlease enter valid choice:");

return 0;

You might also like