DSA CHALLENGING TASKS
TRISHIT GUPTA
20BIT0374
10) FLAMES
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
int x,y,z,i,j,r,s,g,h;
char name1[22],name2[22],a[11]={' ','f','l','a','m','e','s'},b[11];
printf("Enter the name 1:");
scanf("%s",name1); printf("Enter the name2:");
scanf("%s",name2);
x=strlen(name1);
y=strlen(name2);z=x+y;
for(i=0;i<strlen(name1);i++)
for(j=0;j<strlen(name2);j++)
if(name1[i]==name2[j])
name1[i]=name2[j]=' ';
z=z-2;
break;
for(r=6;r>1;r--)
{
s=z%r;
if(s==0)
s=r;i=1;
else{
a[s]=' ';
i=s+1;
j=1;
while(1)
if(i==s)
break;
b[j]=a[i];
if(i==r)
i=0;}
i++;
j++;
for(i=1;i<=r-1;i++)
a[i]=b[i];
}
printf("\nRelationship status:");
switch(a[1])
case 'f':
printf("FRIENDSHIP");
break;
case 'l':
printf("LOVE");
break;
case 'a':
printf("AFFECTION");
break;
case 'm':
printf("MARRIAGE");
break;
case 'e':
printf("ENMITY");
break;
case 's':
printf("SISTER");
break;
getch();
return 0;
}
11) QUIZ IN 2 LISTS
#include <stdio.h>
#include <stdlib.h>
struct node
char name[20];
int data;
struct node *next;
}*start=NULL,*start1=NULL;
int insertatend()
struct node *temp,*ptr;
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter Name : ");
scanf("%s",temp->name);
printf("Enter marks : ");
scanf("%d",&temp->data);
printf("\n");
if (start==NULL)
temp->next=NULL;
start=temp;
else
temp->next=NULL;
ptr=start;
while(ptr->next!=NULL) ptr=ptr->next;ptr->next=temp;
}
int insertatend1()
struct node *new1,*st;
new1=(struct node*)malloc(sizeof(struct node));
printf("Enter Name : ");
scanf("%s",new1->name);
printf("Enter marks : ");
scanf("%d",&new1->data);
printf("\n\n");
if (start1==NULL)
new1->next=NULL;
start1=new1;
else
new1->next=NULL;
st=start1;
while(st->next!=NULL) st=st->next;
st->next=new1;
void merge1()
struct node *sm,*sm1;
sm=start;
while (sm!=NULL)
printf("%d ",sm->data);
sm=sm->next;
sm1=start1;
while (sm1!=NULL){
printf("%d ",sm1->data);
sm1=sm1->next;
void merge2()
struct node *q,*r,*q1,*r1;
int c,c1;
for(q=start;q->next!=NULL;q=q->next)
for(r=q->next;r!=NULL;r=r->next)
if(q->data > r->data)
c=q->data;
q->data=r->data;
r->data=c;
for(q1=start1;q1->next!=NULL;q1=q1->next)
for(r1=q1->next;r1!=NULL;r1=r1->next)
if(q1->data > r1->data)
{
c1=q1->data;
q1->data=r1->data;
r1->data=c1;
merge1();
int main()
int n,m;
while (m!=3)
printf("1. Insert for list 1\n");
printf("2. Insert for list 2\n");
printf("3. Stop Inserting\n");
scanf("%d",&m);
if (m==1) insertatend();
else if (m==2) insertatend1();
else if (m==3) break;
while(n!=4)
printf("\n1. Merge as per low level\n");
printf("2. Merge as per Middle level\n");
printf("3.Merge as per Higher level\n");
printf("4. to stop\n");
printf("Enter your choice : ");
scanf("%d",&n);
if(n==1){
merge1();
else if(n==2){
merge2();
else if(n==3){
merge2();
else{
break;
return 0;
}
12) PASSPORT OFFICE
#include<iostream>
#include<string>
using namespace std;
struct pass
int id;
string date;
};
int main()
int i,j,min,n;
pass t;
pass a[100],b[100];
cout<<"Enter number of records: ";
cin>>n;
cout<<"Enter records: ";
for(i=0;i<n;i++)
cout<<"\nid: ";
cin>>a[i].id;
cout<<"date: ";
cin>>a[i].date;
for(i=0;i<n;i++)
b[i]=a[i];
}
for (i=1;i<n;i++)
t=a[i];
j=i-1;
while(a[j].id > t.id && j>=0)
a[j+1]=a[j];
j--;
a[j+1]=t;
for (i=0;i<n;i++)
cout<<"\nid: ";
cout<<a[i].id;
cout<<"\ndate: ";
cout<<a[i].date;
cout<<"\n\n SORTED LIST";
for (i=0;i<n;i++)
min=i;
for(j=i+1;j<n;j++)
{
if(b[j].date<b[min].date)
min=j;
t=b[i];
b[i]=b[min];
b[min]=t;
for (i=0;i<n;i++)
cout<<"\ndate: ";
cout<<b[i].date;
cout<<"\nid: ";
cout<<b[i].id;
}
13) HASHING
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 11
typedef struct DataItem {
char* data;
}DataItem;
struct DataItem* hashArray[SIZE];
DataItem* item;
int hashCode(char* data) {
int sum=0;
for(int i=0;i<strlen(data);i++){
int c=(int) data[i]-97;
sum+=c;
//printf("%d\n",c);
}
return sum % SIZE;
// return key % SIZE;
}
void display() {
int i = 0;
for(i = 0; i<SIZE; i++) {
if(hashArray[i] != NULL)
printf(" (%s)",hashArray[i]->data);
else
printf(" * ");
}
printf("\n");
}
void clear(){
for(int i=0;i<SIZE;i++)
hashArray[i]=NULL;
}
void insert(char* data) {
DataItem *item = (DataItem*) malloc(sizeof(DataItem));
item->data = data;
//item->key = key;
//get the hash
int hashIndex = hashCode(data);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] != NULL &&
strcmp(hashArray[hashIndex]->data , "-1")!=0) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
display();
}
void insertQ(char* data) {
DataItem *item = (DataItem*) malloc(sizeof(DataItem));
item->data = data;
int hashIndex = hashCode(data);
int i=1;
while(hashArray[hashIndex] != NULL &&
strcmp(hashArray[hashIndex]->data , "-1")!=0) {
//go to next cell
hashIndex=hashIndex+(i*i);
i++;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
display();
}
int main() {
//dummyItem->key = -1;
printf("Linear probing\n");
insert("best");
insert("true");
insert("hill");
insert("dove");
insert("van");
insert("good");
insert("egg");
insert("lap");
clear();
printf("Quardratic probing\n");
insert("best");
insert("true");
insert("hill");
insert("dove");
insert("van");
insert("good");
insert("egg");
insert("lap");
// item = search(37);
// if(item != NULL) {