1.
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;
};
struct ele cal[max];
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];
void push(int ele){
if(top==MAX)
printf("\nStack Overflow\n");
else
st[++top] = ele;
}
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 palin(char *str){
int count=0;
for(int i=0; str[i]!='\0'; i++)
push(str[i]-'0');
for(int i=0; str[i]!='\0'; i++){
if(str[i]==pop()+'0')
count++;
}
if(count==strlen(str))
printf("\nPalindrome\n");
else
printf("\nNot Palindrome\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;
void push(char c){
s[++top]=c;
}
char pop(){
char ret=s[top];
s[top--]='\0';
return ret;
}
int inp(char c){
switch(c){
case '+':
case '-':
return 1;
case '*':
case '/':
return 3;
case '^': return 6;
case '(': return 9;
case ')': return 0;
case '#': return -1;
default: return 7;
}
}
int stp(char c){
switch(c){
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^': return 5;
case '(': return 0;
case '#': return -1;
default: return 8;
}
}
void conv(char *expn, char *pf){
int i=0,j=0;
push('#');
strcat(expn,"#");
while(expn[i]!='\0'){
if(inp(expn[i])>stp(s[top]))
push(expn[i++]);
else if(inp(expn[i])==stp(s[top])){
s[top--]='\0';
i++;
}
else
pf[j++]=pop();
printf("\nStack: %s\t\tPf: %s",s,pf);
}
}
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;
void push(int ele){
s[++top]=ele;
}
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;
void push(int ele){
s[++top]=ele;
}
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 toh(int n, char s, char a, char d){
if(n==1)
printf("Moving disk 1 from %c to %c\n",s,d);
else{
toh(n-1,s,d,a);
printf("Moving disk %d from %c to %c\n",n,s,d);
toh(n-1,a,s,d);
}
}
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 Ins(int ele){
r=(r+1)%MAX;
q[r]=ele;
count++;
}
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;
};
struct stud GetData(){
struct stud ele;
printf("\nEnter the usn: ");
scanf("%s",&ele.USN);
printf("\nEnter the Name: ");
scanf("%s",&ele.Name);
printf("\nEnter branch: ");
scanf("%s",&ele.Branch);
printf("\nEnter sem: ");
scanf("%s",&ele.Sem);
printf("\nEnter the phone number: ");
scanf("%s",&ele.PhNo);
return ele;
}
struct node* CreateNode(){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = GetData();
temp->next = NULL;
return temp;
}
struct node* Finsert(struct node* head){
struct node* temp = CreateNode();
temp->next=head;
return temp;
}
struct node* Einsert(struct node* head){
struct node* temp=CreateNode(), *ptr=head;
if(head==NULL)
return temp;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
return head;
}
void Countnode(struct node* head){
int count=0;
struct node* ptr=head;
while(ptr!=NULL){
ptr=ptr->next;
count++;
}
if(count==0)
printf("\nList is empty\n");
printf("\nNumber of nodes are %d\n",count);
}
void printdata(struct stud ele){
printf("\n%s\t%s\t%s\t%s\t%s\n", ele.USN, ele.Name, ele.Branch, ele.Sem, ele.PhNo);
}
void display(struct node* head){
struct node* ptr = head;
if (ptr == NULL)
printf("\nList is empty\n");
else{
printf("\nStudent Details in the list\n");
printf("USN\t\tName\t\tBranch\tSem\tPhno\n");
while(ptr!=NULL){
printdata(ptr->data);
ptr = ptr->next;
}
}
}
struct node* Fdelete(struct node* head){
struct node* ptr = head;
if (head == NULL)
printf("\nList is empty");
else {
printf("\nElement Deleted is\n");
printf("USN\t\tName\t\tBranch\tSem\tPhno\n");
printdata(ptr->data);
head = head->next;
free(ptr);
}
return head;
}
struct node* Edelete(struct node* head){
if(head==NULL)
printf("List is empty");
else if(head->next==NULL){
printf("\nDeleted element:\nUSN\tName\tBranch\tSEM\tPhno\n");
printdata(head->data);
free(head);
head=NULL;
}
else{
struct node* ptr=head;
while(ptr->next->next!=NULL)
ptr=ptr->next;
printf("\nDeleted element:\nUSN\tName\tBranch\tSEM\tPhno\n");
printdata(ptr->next->data);
free(ptr->next);
ptr->next=NULL;
}
return head;
}
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;
}
struct node* CreateNode(){
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->prev=temp->next=NULL;
temp->data=Getdata();
return temp;
}
void show(emp ele){
printf("\n%s\t\t%s\t%s\t%s\t%s\t%s\n",ele.ssn,ele.name,ele.dept,ele.desg,ele.sal,ele.phno);
}
struct node* Finsert(struct node* head){
struct node* temp = CreateNode();
temp->next = head;
return temp;
}
struct node* Einsert(struct node* head){
struct node* temp = CreateNode(), *ptr=head;
if(head==NULL)
return temp;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = temp;
temp->prev = ptr;
return head;
}
struct node* Fdel(struct node* head){
if(head==NULL)
printf("List is empty");
else{
struct node* ptr = head;
head=head->next;
if(head!=NULL)
head->prev=NULL;
printf("\nDeleted element:\n");
show(ptr->data);
free(ptr);
}
return head;
}
struct node* Edel(struct node* head){
if(head==NULL)
printf("List is empty");
else{
struct node* ptr=head;
while(ptr->next!=NULL)
ptr=ptr->next;
if(ptr==head)
head=NULL;
else
ptr->prev->next=NULL;
printf("\nDeleted element:\n");
show(ptr->data);
free(ptr);
}
return head;
}
void cnt(struct node* head){
int count=0;
if(head==NULL)
printf("List is empty");
else{
struct node* ptr=head;
while(ptr!=NULL){
ptr=ptr->next;
count++;
}
}
printf("No of nodes: %d",count);
}
void display(struct node* head){
if(head==NULL)
printf("List is empty");
else{
struct node* ptr=head;
while(ptr!=NULL){
show(ptr->data);
ptr=ptr->next;
}
}
}
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* CreateNode(struct term t){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data=t;
temp->next=temp;
return temp;
}
struct node* Mkhead(){
struct term t={0,0,0,0};
struct node* temp = CreateNode(t);
return temp;
}
struct node* Insert(struct node* head, struct term t){
struct node* temp = CreateNode(t);
struct node* ptr = head;
while(ptr->next != head)
ptr=ptr->next;
temp->next=head;
ptr->next=temp;
return head;
}
struct node* Del(struct node* head){
struct node* ptr1 = head;
while(ptr1->next!=head)
ptr1=ptr1->next;
struct node* ptr2 = ptr1->next;
ptr1->next=ptr2->next;
free(ptr2);
return ptr1->next;
}
double Compute(struct node* temp, int x, int y, int z){
double value;
struct term t = temp->data;
value = t.coeff*pow(x,t.expx)*pow(y,t.expy)*pow(z,t.expz);
return value;
}
double Evaluate(struct node* temp, int x, int y, int z){
double sum=0;
struct node* ptr = temp;
do{
sum+=Compute(temp,x,y,z);
temp=temp->next;
}while(temp != ptr);
return sum;
}
void Printpoly(struct node* head){
struct node* temp = head->next;
struct term t;
do{
t=temp->data;
printf("%dx^%dy^%dz^%d",t.coeff,t.expx,t.expy,t.expz);
if(temp->next!=head)
printf(" + ");
temp=temp->next;
}while(temp!=head);
}
void ReadPoly(struct node* head, int n){
struct term t;
for(int i=0; i<n; i++){
printf("\nEnter coeff and power of x,y,z respectively: ");
scanf("%d%d%d%d",&t.coeff,&t.expx,&t.expy,&t.expz);
head = Insert(head,t);
}
Printpoly(head);
}
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;
for(int i=0; i<n+m; i++){
for(int j=i; j<n+m-1; j++){
if(ptr1->data.expx==ptr2->data.expx && ptr1->data.expy==ptr2->data.expy && ptr1-
>data.expz==ptr2->data.expz){
ptr1->data.coeff+=ptr2->data.coeff;
ptr2=Del(ptr2);
}
ptr2=ptr2->next;
}
ptr1=ptr1->next;
ptr2=ptr1->next;
}
return res;
}
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;
};
struct node* Create(int ele){
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=ele;
temp->left=temp->right=NULL;
return temp;
}
struct node* ins(struct node* root, int ele){
if(root==NULL)
return Create(ele);
else if(root->data==ele)
printf("\nAlready exist\n");
else if(ele<root->data)
root->left=ins(root->left,ele);
else
root->right=ins(root->right,ele);
return root;
}
void pre(struct node* root){
if(root!=NULL){
printf("%d\t",root->data);
pre(root->left);
pre(root->right);
}
}
void in(struct node* root){
if(root!=NULL){
in(root->left);
printf("%d\t",root->data);
in(root->right);
}
}
void post(struct node* root){
if(root!=NULL){
post(root->left);
post(root->right);
printf("%d\t",root->data);
}
}
void search(struct node* root, int ele){
if(root==NULL)
printf("\nNot found\n");
else if(root->data==ele)
printf("\nElement found\n");
else if(root->data>ele)
search(root->left,ele);
else
search(root->right,ele);
}
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 create(int a[10][10], int n){
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
scanf("%d",&a[i][j]);
}
void bfs(int a[10][10], int n){
int source, vis[10]={0}, f=0, r=-1, q[n], node;
printf("\nEnter source: ");
scanf("%d",&source);
printf("\nNodes reachable are:\n");
vis[source]=1;
q[++r]=source;
while(f<=r){
node=q[f++];
for(int i=0; i<n; i++){
if(a[node][i]==1 && vis[i]==0){
printf("%d\t",i);
vis[i]=1;
q[++r]=i;
}
}
}
}
int count=0;
void dfs(int a[10][10], int n, int vis[], int node){
count++;
vis[node]=1;
for(int i=0; i<n; i++){
if(a[node][i]==1 && vis[i]!=1){
printf("%d\t",i);
vis[i]=1;
dfs(a,n,vis,i);
}
}
}
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
int create(int num){
int key = num % max;
return key;
}
void display(int a[max]){
printf("\nHash table is... \n\nKey/Index\tEmp id\n");
for(int i=10; i<max; i++)
printf("\n%d\t\t%d\n",i,a[i]);
}
void linProb(int a[max], int key, int num){
int flag=0, count=10;
if(a[key]==-1)
a[key]=num;
else{
for(int i=10; i<max; i++){
if(a[i]!=-1)
count++;
}
if(count==max){
printf("\nHash table is full\n");
display(a);
exit(0);
}
for(int i=key+1; i<max; i++){
if(a[i]==-1){
a[i]=num;
flag=1;
break;
}
}
for(int i=10; i<key && flag==0; i++){
if(a[i]==-1){
a[i]=num;
break;
}
}
}
}
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);
}