dsa short prs
dsa short prs
#include <stdio.h>
#include <stdlib.h>
struct DAY
{
char *dayname;
int date;
char *activity;
};
printf("Enter the name of the day, date for the day, activity for
the day\n");
scanf("%s%d%s", day->dayname,&day->date,day->activity);
}
int main()
{
int size;
printf("Enter the number of days in a week\n");
scanf("%d", &size);
read(calendar, size);
display(calendar, size);
return 0;
}
Pr2
#include <stdio.h>
char str[100], pat[50], rep[50], ans[100];
int i, j, c, m, k, flag = 0;
void stringmatch()
{
i = m = c = j = 0;
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++)
ans[j] = rep[k];
i = 0;
c = m;
}
}
else
{
ans[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
ans[j] = '\0';
}
void main()
{
printf("\nEnter a main string & pattern string & replace string\
n");
scanf("%s%s%s",str,pat,rep);
stringmatch();
if (flag == 1)
printf("\nThe resultant string is\n %s", ans);
else
printf("\nPattern string NOT found\n");
}
Pr 3
#include <stdio.h>
#define MAX 4
int s[MAX], item;
int ch, top = -1,status=0;
}
}
else
printf("Not a palindrome");
}
void display(int stack[])
{
int i;
if (top == -1)
printf("Stack is Empty");
else
{
printf("stack contents are\n");
for (i = top; i >= 0; i--)
printf("%d\n", stack[i]);
}
}
void main()
{
printf("Enter 2 elements to be pushed:\n");
scanf("%d", &item);
push(s, item);
scanf("%d", &item);
push(s, item);
display(s);
pop(s);
display(s);
}
Pr4
#include <stdio.h>
#include <string.h>
int stkpre(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;
}
}
int inpre(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;
}
}
void infix_postfix(char infix[], char postfix[])
{
int top, j, i;
char s[30], symbol;
top = -1;
s[++top] = '#';
j = 0;
for (i = 0; i < strlen(infix); i++)
{
symbol = infix[i];
while (stkpre(s[top]) > inpre(symbol))
{
postfix[j] = s[top--];
j++;
}
if (stkpre(s[top]) != inpre(symbol))
s[++top] = symbol;
else
top--;
}
while (s[top] != '#')
{
postfix[j++] = s[top--];
}
postfix[j] = '\0';
}
void main()
{
char infix[20], postfix[20];
printf("Enter a valid infix expression\n");
scanf("%s",&infix);
infix_postfix(infix, postfix);
printf("The postfix expression is:\n");
printf("%s", postfix);
}
Pr5.A
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
Pr5.B
#include <stdio.h>
#include <math.h>
int count = 0;
void tower(int n, int src, int temp, int dest)
{
if (n == 0)
return;
tower(n-1, src, dest, temp);
printf("Move disc %d from %c to %c",n,src,dest);
count++;
tower(n-1, temp, src, dest);
}
void main()
{
int n;
printf("Enter the number of discs:\n");
scanf("%d",&n);
tower(n,'A','B','C');
printf("\n total number of moves = %d",count);
}
Pr6
#include <stdio.h>
#define MAX 4
int ch, front = 0, rear = -1, count = 0, q[MAX], item;
void insert(int item, int *rear, int *q, int *count)
{
if (*count == MAX)
printf("Circular Queue is Full\n");
else
{
*rear = (*rear + 1) % MAX;
q[*rear] = item;
(*count)++;
}
}
Pr7
#include <stdio.h>
#include <stdlib.h>
int MAX = 4, count = 0;
struct student {
char usn[10];
char name[30];
char branch[5];
int sem;
char phno[15];
struct student *next;
};
typedef struct student NODE;
NODE *getnode() {
NODE *newnode = (NODE *)malloc(sizeof(NODE));
if (!newnode) {
printf("Memory allocation failed!\n");
exit(1);
}
printf("Enter Student Details (USN, Name, Branch, Sem, Phone): ");
scanf("%s%s%s%d%s", newnode->usn, newnode->name, newnode->branch,
&newnode->sem, newnode->phno);
newnode->next = NULL;
return newnode;
}
int main() {
int ch;
NODE *head = NULL;
switch (ch) {
case 1:
head = insert_front(head);
break;
case 2:
head = insert_rear(head);
break;
case 3:
head = delete_front(head);
break;
case 4:
head = delete_rear(head);
break;
case 5:
display(head);
break;
case 6:
free_list(head);
printf("Exiting Program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (ch != 6);
return 0;
}
Pr8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{
int ssn;
char name[20];
char dept[10];
char desig[15];
int sal;
char phno[10];
struct emp *left;
struct emp *right;
};
typedef struct emp NODE;
NODE *getnode()
{
NODE *newnode = (NODE *)malloc(sizeof(NODE));
if (!newnode)
{
printf("Memory allocation failed.\n");
exit(1);
}
newnode->left = newnode->right = NULL;
printf("\nEnter SSN, Name, Dept, Designation, Salary, Phone
Number:\n");
scanf("%d %s %s %s %d %s", &newnode->ssn, newnode->name, newnode-
>dept, newnode->desig, &newnode->sal, newnode->phno);
return newnode;
}
if (!head)
return newnode;
NODE *p = head;
while (p->right)
p = p->right;
p->right = newnode;
newnode->left = p;
return head;
}
NODE *insert_front(NODE *head)
{
if (count == MAX)
{
printf("\nList is Full!\n");
return head;
}
if (!head)
return newnode;
newnode->right = head;
head->left = newnode;
return newnode;
}
if (q)
q->right = NULL;
else
head = NULL; // If only one node exists.
free(p);
count--;
printf("\nLast (end) node deleted.\n");
return head;
}
void main()
{
int ch;
NODE *head = NULL;
do
{
printf("\n1. Insert Front\n2. Insert End\n3. Delete Front\n4.
Delete End\n5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
head = insert_front(head);
break;
case 2:
head = insertEnd(head);
break;
case 3:
head = delete_front(head);
break;
case 4:
head = delete_end(head);
break;
case 5:
head = display(head);
break;
case 6:
free_list(head);
break;
default:
printf("\nInvalid choice!\n");
}
} while (ch != 6);
}
Pr9
#include <stdio.h>
#include <math.h>
struct node
{
int cf, px, py, pz;
int flag;
struct node *link;
};
typedef struct node NODE;
NODE *getnode()
{
NODE *x;
x = (NODE *)malloc(sizeof(NODE));
if (x == NULL)
{
printf("Insufficient memory\n");
exit(0);
}
return x;
}
void display(NODE *head)
{
NODE *temp;
if (head->link == head)
{
printf("Polynomial does not exist\n");
return;
}
temp = head->link;
printf("\n");
while (temp != head)
{
printf("%d*x^%d*y^%d*z^%d", temp->cf, temp->px, temp->py, temp->pz);
if(temp->link != head)
printf(" + ");
temp=temp->link;
}
printf("\n");
}
NODE *insert_rear(int cf, int x, int y, int z, NODE *head)
{
NODE *temp, *cur;
temp = getnode();
temp->cf = cf;
temp->px = x;
temp->py = y;
temp->pz = z;
cur = head->link;
while (cur->link != head)
{
cur = cur->link;
}
cur->link = temp;
temp->link = head;
return head;
}
NODE *read_poly(NODE *head)
{
int px, py, pz, cf, ch;
printf("\nEnter coeff: ");
scanf("%d", &cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
head = insert_rear(cf, px, py, pz, head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
scanf("%d", &ch);
while (ch != 0)
{
printf("\nEnter coeff: ");
scanf("%d", &cf);
printf("\nEnter x, y, z powers(0-indiacate NO term): ");
scanf("%d%d%d", &px, &py, &pz);
head = insert_rear(cf, px, py, pz, head);
printf("\nIf you wish to continue press 1 otherwise 0: ");
scanf("%d", &ch);
}
return head;
}
NODE *add_poly(NODE *h1, NODE *h2, NODE *h3)
{
NODE *p1, *p2;
int x1, x2, y1, y2, z1, z2, cf1, cf2, cf;
p1 = h1->link;
while (p1 != h1)
{
x1 = p1->px;
y1 = p1->py;
z1 = p1->pz;
cf1 = p1->cf;
p2 = h2->link;
while (p2 != h2)
{
x2 = p2->px;
y2 = p2->py;
z2 = p2->pz;
cf2 = p2->cf;
if (x1 == x2 && y1 == y2 && z1 == z2)
break;
p2 = p2->link;
}
if (p2 != h2)
{
cf = cf1 + cf2;
p2->flag = 1;
if (cf != 0)
h3 = insert_rear(cf, x1, y1, z1, h3);
}
else
h3 = insert_rear(cf1, x1, y1, z1, h3);
p1 = p1->link;
}
p2 = h2->link;
while (p2 != h2)
{
if (p2->flag == 0)
h3 = insert_rear(p2->cf, p2->px, p2->py, p2->pz, h3);
p2 = p2->link;
}
return h3;
}
void evaluate(NODE *h)
{
NODE *head;
int x, y, z;
float result = 0.0;
head = h;
printf("\nEnter x, y, z, terms to evaluate:\n");
scanf("%d%d%d", &x, &y, &z);
while (h->link != head)
{
result = result + (h->cf * pow(x, h->px) * pow(y, h->py) *
pow(z, h->pz));
h = h->link;
}
result = result + (h->cf * pow(x, h->px) * pow(y, h->py) *
pow(z, h->pz));
printf("\nPolynomial result is: %f", result);
}
void main()
{
NODE *h, *h1, *h2, *h3;
int ch;
while (1)
{
printf("\n\n1.Evaluate polynomial\n2.Add two polynomials\
n3.Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter polynomial to evaluate:\n");
h = getnode();
h->link = h;
h = read_poly(h);
display(h);
evaluate(h);
break;
case 2:
printf("\nEnter the first polynomial:");
h1 = getnode();
h1->link = h1;
h1 = read_poly(h1);
printf("\nEnter the second polynomial:");
h2 = getnode();
h2->link = h2;
h2 = read_poly(h2);
h3 = getnode();
h3->link = h3;
h3 = add_poly(h1, h2, h3);
printf("\nFirst polynomial is: ");
display(h1);
printf("\nSecond polynomial is: ");
display(h2);
printf("\nThe sum of 2 polynomials is: ");
display(h3);
break;
case 3:
exit(0);
break;
default:
printf("\nInvalid entry");
break;
}
}
}
Pr10
#include <stdio.h>
#include <stdlib.h>
int main() {
NODE *root = NULL;
int ch, key;
while (1) {
printf("\n1.Construct BST 2.Preorder 3.Inorder 4.Postorder
5.Search 6.Exit\nEnter choice: ");
scanf("%d", &ch);
if (ch == 6) break;
switch (ch) {
case 1: root = construct_BST(root); break;
case 2: preorder(root); break;
case 3: inorder(root); break;
case 4: postorder(root); break;
case 5: printf("Enter key: "); scanf("%d", &key);
printf(search_element(root, key) ? "Key found\n" :
"Not found\n"); break;
default: printf("Wrong choice\n");
}
}
return 0;
}
Pr11.A
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n, a[10][10], source, i, j;
printf("Enter the number of nodes:");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);
}
}
for (;;)
{
printf(" enter the source vertex\n");
scanf("%d", &source);
bfs(a, n, source);
}
}
Pr11.B
#include <stdio.h>
#include <stdlib.h>
int visited[10];
int a[10][10];
int n;
void readadjmatrix();
void dfs(int);
void main()
{
int start;
clrscr();
readadjmatrix();
printf("Enter the starting vertex:\n");
scanf("%d", &start);
dfs(start);
}
void readadjmatrix()
{
int i, j;
printf("Enter the number of vertices:\n");
scanf("%d", &n);
printf("Enter adjacency matrix\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
}
void dfs(int v)
{
int w;
visited[v] = 1;
for (w = 1; w <= n; w++)
{
if (visited[w] == 0 && a[v][w] == 1)
{
printf("%d", w);
dfs(w);
}
}
}
Pr12
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_SIZE 100
int main() {
EMPLOYEE a[HASH_SIZE];
char name[20];
int id, choice;
initialize_hash_table(a);
while (1) {
printf("1:Insert 2:Display 3:Exit\nEnter choice: ");
scanf("%d", &choice);
if (choice == 3) break;
switch (choice) {
case 1: printf("Enter emp id: "); scanf("%d", &id);
printf("Enter name: "); scanf("%s", name);
insert_hash_table(id, name, a);
break;
case 2: printf("Hash table contents:\n");
display_hash_table(a);
break;
default: printf("Invalid choice\n");
}
}
return 0;
}