Exciting Dsa
Exciting Dsa
CALENDER (main function mein create() ko comment kar dena, uska koi use
nahi hai examiner ko bewakoof banane ke liye hai)
#include<stdio.h>
#include<stdlib.h>
#define max 7
struct ele{
char day[10], act[10];
int date;
};
void create(){
for(int i=0; i<max; i++){
cal[i].day=(char*)malloc(10*sizeof(char));
cal[i].act=(char*)malloc(100*sizeof(char));
}
}
void read(){
for(int i=0; i<max; i++){
printf("\nEnter day: ");
scanf("%s",&cal[i].day);
printf("\nEnter date: ");
scanf("%d",&cal[i].date);
printf("\nEnter act: ");
scanf("%s",&cal[i].act);
}
}
void display(){
printf("\nCALENDER\n\nDAY\tDATE\tACTIVITY\n");
for(int i=0; i<max; i++)
printf("\n%s\t%d\t%s\n",cal[i].day, cal[i].date, cal[i].act);
}
void main(){
create(); //execute karte time isko comment kar dena, ye bs show ke liye hai.
read();
display();
}
2. String Rep (isme ek loophole hai, input dete time ‘pat’ aur ‘rep’ ka length same
rakhna)
#include<stdio.h>
void Rep(char *str, char *pat, char *rep){
int i=0;
while(str[i]!='\0'){
for(int j=0; pat[j]!='\0'; j++){
if(str[i]!=pat[j]){
i++;
break;
}
str[i++]=rep[j];
}
}
printf("\nNew str: %s",str);
}
void main(){
fflush(stdin);
char str[10]="", pat[10]="", rep[10]="";
printf("\nEnter str: ");
gets(str);
printf("\nEnter pat: ");
gets(pat);
printf("\nEnter rep: ");
gets(rep);
Rep(str,pat,rep);
}
3. STACK
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10
int top=-1, st[MAX];
int pop(){
if(top==-1)
printf("\nStack Underflow\n");
else
return(st[top--]);
return 0;
}
void display(){
if(top!=-1){
printf("\nStack elements:\n");
for(int i=0; i<=top; i++)
printf("%d\t",st[i]);
}
else
printf("\nStack Underflow\n");
}
void main(){
char str[10]="";
int ele, ch;
while(1){
printf("\n\nSTACK OPERATIONS\n1 PUSH\n2 POP\n3 CHECK PALINDROME\n4 DISPLAY\n5
EXIT\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:printf("\nEnter element to be pushed: ");
scanf("%d",&ele);
push(ele);
break;
case 2:ele=pop();
printf("\nDeleted element: %d",ele);
break;
case 3:printf("Enter string: ");
scanf("%s",&str);
palin(str);
break;
case 4:display();
break;
default:exit(0);
}
}
}
4. Infix to postfix (best version)
#include<stdio.h>
#include<string.h>
char s[100];
int top=-1;
char pop(){
char ret=s[top];
s[top--]='\0';
return ret;
}
void main(){
char expn[20]="",pf[20]="";
printf("\nEnter expn:\n");
scanf("%s",&expn);
conv(expn,pf);
printf("\n\nPf: %s\n",pf);
}
5.a. Suffix expression (iske liye mere pass 2 code hai jo easy lage ratt lo)
CODE 1
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
int top=-1, s[50];
char pf[50], symb;
int pop(){
return s[top--];
}
void main(){
printf("\nEnter postfix exprn: ");
scanf("%s",&pf);
for(int i=0; pf[i]!='\0'; i++){
symb = pf[i];
if(isdigit(symb))
push(symb - '0');
else{
int n2 = pop();
int n1 = pop();
switch(symb){
case'+':push(n1+n2);
printf("\n%d + %d = %d",n1,n2,n1+n2);
break;
case'-':push(n1-n2);
printf("\n%d - %d = %d",n1,n2,n1-n2);
break;
case'*':push(n1*n2);
printf("\n%d * %d = %d",n1,n2,n1*n2);
break;
case'/':push(n1/n2);
printf("\n%d / %d = %d",n1,n2,n1/n2);
break;
case'%':push(n1%n2);
printf("\n%d %% %d = %d",n1,n2,n1%n2);
break;
case'$':
case'^':push(pow(n1,n2));
printf("\n%d ^ %d = %d",n1,n2,pow(n1,n2));
break;
default:push(0);
}
}
}
printf("After eval: %d",pop());
}
CODE 2
#include<stdio.h>
#include<math.h>
int s[50], top=-1;
int pop(){
return s[top--];
}
void main(){
char expn[20]="", sym;
int n1,n2;
printf("\nEnter expn:\n");
gets(expn);
for(int i=0; expn[i]!='\0'; i++){
sym=expn[i];
switch(sym){
case '+': n2=pop(), n1=pop();
push(n1+n2);
printf("\n%d + %d = %d\n",n1,n2,n1+n2);
break;
case '-': n2=pop(), n1=pop();
push(n1-n2);
printf("\n%d - %d = %d\n",n1,n2,n1-n2);
break;
case '*': n2=pop(), n1=pop();
push(n1*n2);
printf("\n%d * %d = %d\n",n1,n2,n1*n2);
break;
case '/': n2=pop(), n1=pop();
push(n1/n2);
printf("\n%d / %d = %d\n",n1,n2,n1/n2);
break;
case '%': n2=pop(), n1=pop();
push(n1%n2);
printf("\n%d %% %d = %d\n",n1,n2,n1%n2);
break;
case '$':
case '^': n2=pop(), n1=pop();
push(pow(n1,n2));
printf("\n%d ^ %d = %.f\n",n1,n2,pow(n1,n2));
break;
default: push(sym-'0');
}
}
printf("\nAfter eval: %d\n\n",pop());
}
5.b. Tower of Hanoi
#include<stdio.h>
void main(){
int n;
printf("Enter the number of disks: ");
scanf("%d",&n);
printf("\n\nSource A, Auxilary B, Destination C\n\n");
toh(n,'A','B','C');
printf("\n\nMoved %d disks from A to C\n\n",n);
}
6. Queue (easy hai karlo)
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int count=0, f=0, r=-1, q[MAX];
int Qempty(){
return count==0;
}
int Qfull(){
return count==MAX;
}
void Del(){
printf("\nElement deleted: %d",q[f]);
f=(f+1)%MAX;
count--;
}
void display(){
int j=f;
if(Qempty())
printf("\nQueue is empty\n");
else{
printf("\nQUEUE ELEMENTS:\n");
for(int i=0; i<count; i++){
printf("%d\t",q[j]);
j=(j+1)%MAX;
}
}
}
void main(){
int ch, ele;
while(1){
printf("\nCIRCULAR QUEUE\n1 INSERT\n2 DELETE\n3 DISPLAY\n4 EXIT\n\nEnter your choice:
");
scanf("%d",&ch);
switch(ch){
case 1:
if(Qfull())
printf("\nQueue is full\n");
else{
printf("\nEnter element: ");
scanf("%d",&ele);
Ins(ele);
}
break;
case 2:
if(Qempty())
printf("\nQueue is empty\n");
else
Del();
break;
case 3:display();
break;
default:exit(0);
}
}
}
7. Singly linked list (longest program, mujhe yahi mila tha external mein)
#include <stdio.h>
#include <stdlib.h>
struct stud{
char USN[20], Name[20], Branch[20], Sem[20], PhNo[20];
};
struct node{
struct stud data;
struct node* next;
};
return ele;
}
void main(){
struct node* head=NULL;
int ch;
while(1){
printf("\nMENU\n\n1 Front Insert\n2 End Insert\n3 Front Delete\n4 End Delete\n5 Count nodes\n6
Display\n7 Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch){
case 1:
head=Finsert(head);
break;
case 2:
head=Einsert(head);
break;
case 3:
head=Fdelete(head);
break;
case 4:
head=Edelete(head);
break;
case 5:
Countnode(head);
break;
case 6:
display(head);
break;
default:exit(0);
}
}
}
8. Doubly linked list (SLL ka chhota bhai)
#include<stdio.h>
#include<stdlib.h>
typedef struct{
char ssn[10], name[20], dept[10], desg[10], sal[10], phno[10];
}emp;
struct node{
emp data;
struct node *prev, *next;
};
emp Getdata(){
emp ele;
fflush(stdin);
printf("\nEnter SSN: ");
gets(ele.ssn);
printf("\nEnter name: ");
gets(ele.name);
printf("\nEnter dept: ");
gets(ele.dept);
printf("\nEnter desg: ");
gets(ele.desg);
printf("\nEnter sal: ");
gets(ele.sal);
printf("\nEnter phno: ");
gets(ele.phno);
return ele;
}
void main(){
struct node* head=NULL;
int ch;
while(1){
printf("\nMENU\n\n1 Front Insert\n2 End Insert\n3 Front Delete\n4 End Delete\n5 Count nodes\n6
Display\n7 Exit\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch){
case 1:
head=Finsert(head);
break;
case 2:
head=Einsert(head);
break;
case 3:
head=Fdel(head);
break;
case 4:
head=Edel(head);
break;
case 5:
cnt(head);
break;
case 6:
display(head);
break;
default:exit(0);
}
}
}
9. Circular linked list (toughest program, jo lab manual mein diya hai wo run nahi
karta. But dw mera wala run karta hai)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct term{
int coeff, expx, expy, expz;
};
struct node{
struct term data;
struct node* next;
};
struct node* Addpoly(struct node* p1, int n, struct node* p2, int m){
struct node* res = Mkhead(), *ptr1=p1, *ptr2=p2;
for(int i=0; i<n; i++){
ptr1=ptr1->next;
res=Insert(res,ptr1->data);
}
for(int i=0; i<m; i++){
ptr2=ptr2->next;
res=Insert(res,ptr2->data);
}
ptr1=res->next;
ptr2=ptr1->next;
void main(){
int n,m,x,y,z,ch;
while(1){
struct node* p1 = Mkhead(), *p2 = Mkhead(), *res = Mkhead();
printf("\n\n1 Evaluate\n2 Add poly\n3 Exit\n Enter choice: ");
scanf("%d",&ch);
switch(ch){
case 1:printf("\nEnter the no of terms in poly: ");
scanf("%d",&n);
ReadPoly(p1,n);
printf("\nEnter values of x,y,z: ");
scanf("%d%d%d",&x,&y,&z);
printf("\nAfter Evaluation= %.f\n",Evaluate(p1,x,y,z));
break;
case 2:printf("\nEnter the no of terms in poly1: ");
scanf("%d",&n);
ReadPoly(p1,n);
printf("\n\nEnter the no of terms in poly2: ");
scanf("%d",&m);
ReadPoly(p2,m);
printf("\n\nSum of polys is:\n\n");
Printpoly(Addpoly(p1,n,p2,m));
break;
default:exit(0);
}
}
}
10. Binary search tree (ye karlo easy hai)
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* left, *right;
};
void main(){
struct node* root = NULL;
int ele, ch;
while(1){
printf("\nMENU\n1 Insert\n2 Traversal\n3 Search key\n4 Exit\nEnter ur choice: ");
scanf("%d",&ch);
switch(ch){
case 1: printf("\nEnter element: ");
scanf("%d",&ele);
root=ins(root,ele);
break;
case 2:
if(root==NULL){
printf("\nTree empty\n");
break;
}
printf("\n1 Preorder\n2 Inorder\n3 Postorder\nEnter choice: ");
scanf("%d",&ch);
switch(ch){
case 1:pre(root);
break;
case 2:in(root);
break;
case 3:post(root);
break;
}
break;
case 3:printf("\nEnter key: ");
scanf("%d",&ele);
search(root,ele);
break;
default:exit(0);
}
}
}
11. Graph (isse better program nahi milega graph ka)
#include<stdio.h>
#include<stdlib.h>
void main(){
int a[10][10], vis[10]={0}, n, ch;
while(1){
printf("\n\nMENU\n1 Create\n2 BFS\n3 DFS and Check connectivity\n5 Exit\n\nEnter choice: ");
scanf("%d",&ch);
switch(ch){
case 1:printf("\nEnter no of nodes: ");
scanf("%d",&n);
printf("\nEnter adjacency matrix:\n");
create(a,n);
break;
case 2:bfs(a,n);
break;
case 3:printf("\nNodes reachable are:\n");
dfs(a,n,vis,0);
if(count==n)
printf("\nGraph connected\n");
else
printf("\nGraph not connected\n");
break;
default:exit(0);
}
}
}
12. Hash Table
#include<stdio.h>
#include<stdlib.h>
#define max 15
void main(){
int a[max], ans, num, key;
for(int i=10; i<max; i++)
a[i]=-1;
do{
printf("\nEnter the number: \n");
scanf("%d",&num);
key=create(num);
linProb(a,key,num);
printf("\nDo you want to continue? (1 for yes, 0 for no): ");
scanf("%d",&ans);
}while(ans==1);
display(a);
}