DSA
DSA
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Day {
};
scanf("%s", calendar[i].name);
scanf("%d", &calendar[i].date);
scanf("%s", calendar[i].description);
printf("\n");
}
}
int main() {
create(calendar);
read(calendar);
display(calendar);
free(calendar[i].name);
free(calendar[i].description);
return 0;
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
Support the program with functions for each of the above operations. Don't use
Built-in functions. */
#include<stdio.h>
int length(char[]);
void main()
// local declarations
// Read strings
gets(str);
gets(pat);
ls = length(str);
lp = length(pat);
lr = length(rep);
for(j=0; j<lp; j++) // copy the first chunk form main string of
pattern length
for(l=0;l<lr;l++)
d[l1++]=rep[l];
i=i+(lp-1);
else
d[l1++]=str[i];
len++;
return len;
}
// function to compare two strings
int i=0;
i++;
return 1;
return -1;
else
int c, d, e;
e = c;
if (pattern[d] == text[e])
e++;
else
break;
if (d == lp)
f. Exit.
Support the program with appropriate functions for each of the above operations
*/
#include<stdio.h>
#define MAX 5
int stack[MAX];
int top=-1;
void push()
int item;
if(top==(MAX-1))
else
scanf("%d",&item);
stack[++top]=item;
void pop()
else
void display()
int i;
if(top==-1)
else
for(i=top;i>=0;i--)
void palindrome()
int i,count=0;
for(i=0; i<=(top/2); i++)
if(stack[i] == stack[top-i])
count++;
if((top/2+1)==count)
else
void main()
int choice;
while(1)
scanf("%d",&choice);
switch(choice)
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:palindrome();
break;
case 5:return;
#include<stdio.h>
void infix_to_postfix();
void push(char);
char pop();
int priority(char);
char infix[30], postfix[30],stack[30];
int top=-1;
void main()
scanf("%s",infix);
infix_to_postfix();
stack[++top]=item;
char pop()
return stack[top--];
int p;
switch(symb)
case '+':
break;
case '*':
case '/':
break;
case '^':
break;
case '(':
break;
break;
} // end of switch
return p;
void infix_to_postfix()
int i=0,j=0;
char symb,temp;
push('#');
for(i=0;infix[i]!='\0';i++)
symb=infix[i];
switch(symb)
break;
postfix[j++]=temp;
temp=pop();
} // end of while
break;
case'+':
case'-':
case'*':
case'/':
case'%':
case'^':
temp=pop();
postfix[j++]=temp;
push(symb);
break;
default: postfix[j++]=symb;
} // end of switch
} // end of for
temp=pop();
postfix[j++]=temp;
} // end of while
*/
#include<stdio.h>
#include<math.h>
void push(float);
float pop();
void evaluate(char[]);
float stack[20];
int top=-1;
void main()
int choice,n;
char postfix[100];
scanf("%d", &choice);
switch(choice)
scanf("%s",postfix);
evaluate(postfix);
break;
scanf("%d",&n);
tower(n,'A','C','B');
break;
case 3 : return;
} // end of switch
} // end of menu
} // end of main
stack[++top]=item;
} // end of push
float pop()
return stack[top--];
} // end of pop
int i;
char symb;
symb=postfix[i];
switch(symb)
case '+':op2=pop();
op1=pop();
res=op1+op2;
push(res);
break;
case '-':op2=pop();
op1=pop();
res=op1-op2;
push(res);
break;
case '*':op2=pop();
op1=pop();
res=op1*op2;
push(res);
break;
case '/':op2=pop();
op1=pop();
if(op2==0)
return;
res=op1/op2;
push(res);
break;
if(op2==0)
return;
res=(int)op1%(int)op2;
push(res);
break;
case '^':op2=pop();
op1=pop();
res=pow(op1,op2);
push(res);
break;
} // end of for
if(top==-1)
else
top=-1;
}
} // end of evaluate function
if(n==0)
return;
tower(n-1,source,aux,destination);
tower(n-1,aux,destination,source);
MAX)
e. Exit
Support the program with appropriate functions for each of the above operations
*/
#include<stdio.h>
#define MAX 6
int front=0,rear=0;
char q[MAX];
void insert()
char item;
return;
rear=(rear+1)%MAX;
fflush(stdin);
scanf("%c",&item);
q[rear]=item;
} // end of insert
return;
front=(front+1)%MAX;
} // end of delete
void display()
int i;
if(front==rear)
return;
printf("%c\t",q[i]);
printf("%c\t",q[i]);
} // end of display
void main()
int choice;
while(1)
scanf("%d",&choice);
switch(choice)
case 1:
insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:return;
/*7. Design, Develop and Implement a menu driven Program in C for the
following operations on Singly Linked List (SLL) of Student Data with the
fields: USN, Name, Branch, Sem, PhNo
e. Exit .
*/
#include<stdio.h>
#include<stdlib.h>
struct node
char usn[20],name[10],branch[5];
int sem;
};
typedef struct node * NODE;
NODE temp,FIRST=NULL;
NODE getnode()
NODE x;
x->link=NULL; //by node default link is set to NULL to avoid dangling pointer
return x;
void read()
temp=getnode();
scanf("%s",temp->usn);
scanf("%s",temp->name);
scanf("%s",temp->branch);
scanf("%d",&temp->sem);
void create_SLL()
int n,i;
scanf("%d",&n);
else{
}
//Display the status of SLL and count the number of nodes in it
void display_count()
int count=1;
temp=FIRST;
printf("Student details:\n");
else
printf("\nUSN\tNAME\tBRANCH\tPHNO\tSEMESTER\n");
count++;
printf("%s\t%s\t%s\t%llu\t%d\n",temp->usn,temp->name,temp-
>branch,temp->phno,temp->sem);
printf("%s\t%s\t%s\t%llu\t%d",temp->usn,temp->name,temp->branch,temp-
>phno,temp->sem);
return;
}
// Perform Insertion at front of SLL
void insert_front()
read();
if(FIRST==NULL)
FIRST=temp;
else{
temp->link=FIRST;
FIRST=temp;
NODE insert_end()
NODE last=FIRST;
read();
if(FIRST==NULL)
FIRST=temp;
else
last=last->link;
last->link=temp; // connect new node to last node link
return FIRST;
void delete_front()
temp=FIRST;
printf("List is empty\n");
else
return;
void delete_end()
NODE last=NULL;
temp=FIRST;
printf("List is empty\n");
free(FIRST);
FIRST=NULL;
else
while(temp->link!=NULL)
last=temp;
temp=temp->link;
last->link=NULL;
return;
}
void main()
int choice;
while(1)
scanf("%d",&choice);
switch(choice)
case 1:create_SLL();
break;
case 2:display_count();
break;
case 3:insert_front();
break;
case 4:insert_end();
break;
case 5:delete_front();
break;
case 6:delete_end();
break;
case 7:return;
default:printf("\nInvalid choice\n");
}
/*8. Design, Develop and Implement a menu driven Program in C for the
following operations on Doubly Linked List (DLL) of Employee Data with the
fields: SSN, Name, Dept, Designation, Sal, PhNo
f. Exit. */
#include<stdio.h>
char ssn[12],name[20],dept[25],desig[20];
float sal;
};
NODE x;
x=(NODE)malloc(sizeof(struct node));
x->prev=NULL;
x->next=NULL;
return x;
temp=getnode();
printf("Enter SSN:");
scanf("%s",temp->ssn);
printf("Enter Name:");
scanf("%s",temp->name);
printf("Enter Dept:");
scanf("%s",temp->dept);
printf("Enter Designation:");
scanf("%s",temp->desig);
printf("Enter Phno:");
scanf("%llu",&temp->phno);
printf("Enter Salary:");
scanf("%f",&temp->sal);
return;
int n,i=1;
scanf("%d",&n);
while(i<=n)
read();
FIRST=temp;
END=temp;
else //otherwise find the last node and insert the new node
END->next=temp;
temp->prev=END;
END=temp;
} //e nd of while
} // end of create()
void display_count() //Display the status of DLL and count the number of nodes
in it
temp=FIRST;
int count=0;
else
printf("Employee details:\n");
count++;
printf("\n%s\t%s\t%s\t%s\t%llu\t%0.2f",temp->ssn, temp-
>name, temp->dept,temp->desig,temp->phno,temp->sal);
temp=temp->next;
}
} // end of else
return;
} // end of display()
read();
FIRST=temp;
END=temp;
temp->next=FIRST;
FIRST->prev=temp;
FIRST=temp;
read();
FIRST=temp;
END=temp;
else // otherwise find the last node and insert the new node
END->next=temp;
temp->prev=END;
END=temp;
return ;
temp=FIRST;
printf("List is empty\n");
{
printf("deleted element is %s\n", temp->ssn);
FIRST=NULL;
END=NULL;
free(temp);
FIRST =FIRST->next;
FIRST->prev=NULL;
free(temp);
return;
} // end of deletefront
temp = END;
printf("List is empty\n");
FIRST=NULL;
END=NULL;
free(temp);
END=END->prev;
END->next=NULL;
free(temp);
return ;
} // end of deletionend
void main()
int choice;
while(1)
scanf("%d",&choice);
switch(choice)
{
case 1 : Create_DLL();
break;
case 2 : display_count();
break;
case 3 : Insertionfront();
break;
case 4 : Insertionend();
break;
case 5 : Deletionfront();
break;
case 6 : Deletionend();
break;
case 7 : return;
} //end of switch
} // end of while
} // end of main
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store
the result in
POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operation
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int coef;
int x,y,z;
};
NODE x;
x=(NODE)malloc(sizeof(struct node));
return x;
} // end of getnode
NODE readpoly()
NODE temp,head,cur;
char ch;
head=getnode(); // create a head node and set all values to -1 it is similar
to FIRST in SLL program
head->coef=-1;
head->x=-1;
head->y=-1;
head->z=-1;
do
scanf("%d%d%d%d",&temp->coef,&temp->x,&temp->y,&temp-
>z );
cur=head;
cur=cur->link;
scanf("%c",&ch);
} // end of readpoly
int compare(NODE a,NODE b) // function to compare the A and B polynomial
nodes
return 1;
return -1;
return 1;
return -1;
return 1;
return -1;
return 0;
} // end of compare
void attach(int cf,int x1,int y1, int z1, NODE *ptr) // function to attach the A
and B polynomial node to C Polynomial
NODE temp;
temp=getnode();
temp->coef=cf;
temp->x=x1;
temp->y=y1;
temp->z=z1;
(*ptr)->link=temp;
*ptr=temp;
} // end of attach
int sum,done=0;
starta=a;
a=a->link;
b=b->link;
c->coef=-1;
c->x=-1;
c->y=-1;
c->z=-1;
lastc=c;
do{
switch(compare(a,b))
case -1:attach(b->coef,b->x,b->y,b->z,&lastc);
b=b->link;
break;
else{
sum=a->coef+b->coef;
if(sum)
attach(sum,a->x, a->y,a->z,&lastc);
a=a->link;b=b->link;
break;
attach(a->coef,a->x, a->y,a->z,&lastc);
a=a->link;
break;
NODE cur;
cur=ptr->link;
while(cur!=ptr) // To print from HEAD node till END node
printf("%d*x^%d*y^%d*z^%d",cur->coef,cur->x, cur->y,
cur->z);
if (cur!=ptr)
printf(" + ");
} // end of print
int res=0;
NODE cur;
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &z);
ex=cur->x; // exponent of x
ey=cur->y; // exponent of y
ez=cur->z; // exponent of z
cof=cur->coef; // coefficient
printf("\nresult: %d",res);
} // end of evaluate
void main(void)
int i, ch;
NODE a=NULL,b,c;
while(1)
printf("\n7: Exit");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the elements of the polynomial A");
a=readpoly();
break;
b= readpoly();
break;
break;
break;
printf("\n");
break;
break;
case 7: return;
} //end of switch
} // end of while
} // end of main
c. Search the BST for a given element (KEY) and report the appropriate
message
e. Exit.
#include<stdio.h>
struct node // DLL node for Tree with left_link value and right_link
int value;
};
NODE x;
x->ltree=NULL;
x->rtree=NULL;
return x;
} // end of getnode
NODE create(int item, NODE root) // create a BST for the given values
{
NODE temp,cur,prev;
int i , flag=0;
root=temp;
return root;
prev=NULL;
cur=root;
prev=cur;
if(temp->value==cur->value)
flag=1;
break;
cur=(temp->value<cur->value)?cur->ltree:cur->rtree; //
condition to choose left or right subtree
if(flag==0)
{
prev->ltree=temp;
prev->rtree=temp;
return root;
printf("%d\t",IN->value); // Root
{
if(PRE!=NULL) // if tree is not empty then traverse
printf("%d\t",PRE->value); // Root
printf("%d\t",POST->value); // Root
void search(NODE root) // Binary Search on BST for given Key element
NODE cur;
printf("tree is empty\n");
return;
cur=root;
cur=cur->ltree;
cur=cur->rtree;
} // end of while
} // end of search
void main()
NODE root=NULL;
while(1)
printf("\n4. Exit");
scanf("%d",&choice);
switch(choice)
scanf("%d",&n);
scanf("%d",&item);
root=create(item,root);
break;
case 2: if(root==NULL) // check for empty tree
printf("Tree is empty\n");
else
printf("\n\nPREORDER traversal\n");
pre(root);
printf("\n\nINORDER traversal\n");
in(root);
printf("\n\nPOSTORDER traversal\n");
post(root);
break;
case 3: search(root);
break;
case 4: return;
} // end of switch
} // end of while
} // end of main
#include<stdio.h>
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
return;
for(i=1;i<=n;i++)
void main()
int v, choice;
while(1)
printf("\n2. Print all the nodes reachable from a given starting node in a
digraph using BFS method") ;
printf("\n3. Exit");
scanf("%d",&choice);
switch(choice)
case 1: create_graph();
break;
scanf("%d",&v);
visited[i]=0;
printf("node %d\n",i);
break;
case 3:return;
default:printf("\nInvalid Choice");
} // end of switch
} // end of while
} // end of main
#include <stdio.h>
#define MAX 10
char ans;
do
flag=0;
count=0;
printf("\nCollision Detected...!!!\n");
i=0;
if (a[i]!=-1)
count++; // count empty location in HT
i++;
} // end of while
display(a); // Display HT
return;
} // end of if
if(a[i] == -1)
a[i] = key;
flag =1;
break;
} // end of if
i=0;
if(a[i] == -1)
a[i] = key;
flag=1;
break;
} // end of if
i++;
} // end of while
} // end of else
fflush(stdin);
scanf("%c",&ans);
int i;
void main()
while(1)
printf("\n3. Exit");
scanf("%d",&choice);
switch (choice)
case 1: linear_prob(a);
break;
case 2: display(a);
break;
case 3: return;
} // end of switch
} // end of while
} // end of main