Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Name of the Course: Data structures (DS)

Assignment – 1
1602-19-733-080
M. Meghana
26-09-2020

SET IV ( 1602-19-733-080 to 1602-19-733-085):

1. Shortest Man on the Team locked ProblemThere are n people who want to
apply for the selections of the basketball team. But the coach has decided to take
only n- 1 people. Irrespective of the skill of the players, the coach believes that the
height of the player is the most important factor. Therefore, by default the
shortest of the applicants will be removed. From amongst the line of players (who
are in random order), the coach finds it difficult to find the shortest person.
Fortunately, you, the person in the front of the line knows programming. He asks
you to go find the shortest person in the line and switch places with him.
Formally, swap the first element of the array with the smallest element of the array.
INPUT
The first line of input is n (1≤n≤100000), the number of applicants for the basketball
team The second line of input is the heights of the n players (positive numbers) each
separated by a space.
OUTPUT
Print the heights of the players in a line after the shortest person has swapped places
with the first person.
//Sample Input 0
5
38259
//Sample Output 0
28359
Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Enter the number of applicants for basketball team: \n");
int n,i;
scanf("%d",&n);
printf("Enter the heights of the applicants\n");
int *hei=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
scanf("%d",(hei+i));
printf("Person in the front line finds the shortest person in the line and switch
places with him so that coach removes the shortest person\n");
for(i=1;i<n;i++)
{
if(hei[i]<hei[0])
{
int temp=hei[0];
hei[0]=hei[i];
hei[i]=temp;
}
}
printf("After swapping, the positions of applicants are\n");
for(i=0;i<n;i++)
{
printf("%d ",hei[i]);
}
printf("\n");
printf("Now the coach removes the first person");
}
Output:

2. Develop a C program to create a menu driven Supermarket Billing system that has
product information like product no, product name, price, qty, tax, discount. A
customer can purchase product and his invoice generated. The super market
wants to keep track of the items which are purchased in more
quantities. Also design a C function which displays the customers items whose
quantity is more than 5 and compute the time complexity for the designed
function. Use linked list to implement the solution.
Program:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define quantcount 5
int n1,n2;
struct supermarket
{
int id;
char item[20];
int price;
int tax;
int discount;
int quantity;
struct supermarket *link;
}*customer,*q,*store,*l,*morequan;
int n,no;
void insert_items(int idx,char itemx[],int pricex, int taxx, int discountx, int quantityx)
{
if(store==NULL)
{
struct supermarket *temp=(struct supermarket*)malloc(sizeof(struct
supermarket));
temp->id=idx;
strcpy(temp->item,itemx);
temp->price=pricex;
temp->tax=taxx;
temp->discount=discountx;
temp->quantity=quantityx;
temp->link=store;
store=temp;

}
else
{
q=store;
struct supermarket *temp=(struct supermarket*)malloc(sizeof(struct
supermarket));
temp->id=idx;
strcpy(temp->item,itemx);
temp->price=pricex;
temp->tax=taxx;
temp->discount=discountx;
temp->quantity=quantityx;
temp->link=store;
temp->link=NULL;
while(q->link!=NULL)
{
q=q->link;
}
q->link=temp;
}
}
void insert_customer(int idx, int quantityx)
{
if(customer==NULL)
{
q=store;
struct supermarket *temp=(struct supermarket*)malloc(sizeof(struct
supermarket));
temp->id=idx;
temp->quantity=quantityx;
while(q!=NULL)
{
if(q->id==temp->id)
{
strcpy(temp->item,q->item);
temp->price=q->price;
temp->tax=q->tax;
temp->discount=q->discount;
temp->quantity=quantityx;
}
q=q->link;
}
temp->link=customer;
customer=temp;

}
else
{
l=customer;
q=store;
struct supermarket *temp=(struct supermarket*)malloc(sizeof(struct
supermarket));
temp->id=idx;
temp->quantity=quantityx;
while(q!=NULL)
{
if(q->id==temp->id)
{
strcpy(temp->item,q->item);
temp->price=q->price;
temp->tax=q->tax;
temp->discount=q->discount;
temp->quantity=quantityx;
}
q=q->link;
}
temp->link=customer;
temp->link=NULL;
while(l->link!=NULL)
{
l=l->link;
}
l->link=temp;
}
}
void display_store()
{
printf("List of items:\n");
q=store;
while(q!=NULL)
{
printf("Product No: %d Product Name: %s Price: Rs.%d Tax: Rs.%d Discount:
%d%% Quantity: %d\n",q->id,q->item,q->price,q->tax,q->discount,q->quantity);
q=q->link;
}
}
void morequant(int idx,int quantityx)
{
if(morequan==NULL)
{
q=store;
struct supermarket *temp=(struct supermarket*)malloc(sizeof(struct
supermarket));
temp->id=idx;
temp->quantity=quantityx;
while(q!=NULL)
{
if(q->id==temp->id)
{
strcpy(temp->item,q->item);
temp->price=q->price;
temp->tax=q->tax;
temp->discount=q->discount;
temp->quantity=quantityx;
}
q=q->link;
}
temp->link=morequan;
morequan=temp;

}
else
{
l=morequan;
q=store;
struct supermarket *temp=(struct supermarket*)malloc(sizeof(struct
supermarket));
temp->id=idx;
temp->quantity=quantityx;
while(q!=NULL)
{
if(q->id==temp->id)
{
strcpy(temp->item,q->item);
temp->price=q->price;
temp->tax=q->tax;
temp->discount=q->discount;
temp->quantity=quantityx;
}
q=q->link;
}
temp->link=morequan;
temp->link=NULL;
while(l->link!=NULL)
{
l=l->link;
}
l->link=temp;
}
}
void display_morequant()
{
q=morequan;
while(q!=NULL)
{
int cost=q->price*q->quantity+q->tax-((q->price*q->discount*q->quantity)/100);
printf("Product No: %d Procduct Name: %s Price: Rs.%d Tax: Rs.%d Discount:
%d%% Quantity: %d Cost= Rs.%d\n",q->id,q->item,q->price,q->tax,q->discount,q-
>quantity,cost);
q=q->link;
}
}
void display_customer()
{
printf("List of items the customer buyed:\n");
q=customer;
int TotalAmount=0;
while(q!=NULL)
{
int cost=q->price*q->quantity+q->tax-((q->price*q->discount*q->quantity)/100);
TotalAmount+=cost;
printf("Product No: %d Procduct Name: %s Price: Rs.%d Tax: Rs.%d Discount:
%d%% Quantity: %d Cost= Rs.%d\n",q->id,q->item,q->price,q->tax,q->discount,q-
>quantity,cost);
q=q->link;
}
printf("Total Amount : Rs.%d\n",TotalAmount);
}
int main()
{
store=NULL;
int idx;
char itemx[20];
int pricex;
int taxx;
int discountx;
int quantityx;
printf("Enter number of products in the super market\n");
scanf("%d",&n);
printf("Enter product number, product name,price, tax, discount, quantity of the
products in the super market\n");
int i=0;
while(i!=n)
{
scanf("%d %s %d %d %d %d",&idx,itemx,&pricex,&taxx,&discountx,&quantityx);
insert_items(idx,itemx,pricex,taxx,discountx,quantityx);
i++;
}
display_store();
printf("Enter number of products the customer want to buy\n");
scanf("%d",&no);
i=0;
printf("Enter product no and quantity the customer want to buy\n");
while(i!=no)
{
scanf("%d %d",&idx,&quantityx);
insert_customer(idx,quantityx);
if(quantityx>quantcount)
morequant(idx,quantityx);
i++;
}
display_customer();
printf("The information of products the customer bought which has quantity more than
%d\n",quantcount);
display_morequant();
}
Output:

The time complexity of function which displays the customers items whose quantity
is more than 5:
void display_morequant()
{
Count=0;
q=morequan;//count+=1
while(q!=NULL)
{
count+=1
int cost=q->price*q->quantity+q->tax-((q->price*q->discount*q->quantity)/100);
//count+=1
printf("Product No: %d Procduct Name: %s Price: Rs.%d Tax: Rs.%d Discount: %d%%
Quantity: %d Cost= Rs.%d\n",q->id,q->item,q->price,q->tax,q->discount,q->quantity,cost);
//count+=1
q=q->link; //count+=1
}
}
Time complexity of above function is O(n).(1+n+n+n+n+1=4n+2)
Statement s/e Frequency Total Steps
void display_morequant() 0 0 0
{ 0 0 0
q=morequan 1 1 1
while 1 n+1 n+1
cost 1 n n
printf 1 n n
q=q->link; 1 n n
Total 4n+2

3. Given two numbers represented by two lists, develop a function that returns sum
list. The sum list is list representation of addition of two input numbers. Eg: If
List1: 5->6->3 (number is 365) List2: 8->4->2 (number is 248)
Output: Result list: 6->1->3 (number is 613)
Input:
5
6
3
5- >6->3
8
4
2
8- >4->2
Output:
6- >1->3
Program:
#include <stdio.h>
#include<stdlib.h>
int n1,n2;
struct node
{
int data;
struct node *link;
}*l1,*l2,*s,*q1,*q2;
void insert_list1(int x)
{
if(l1==NULL)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=l1;
l1=temp;
}
else
{
q1=l1;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
while(q1->link!=NULL)
{
q1=q1->link;
}
q1->link=temp;
}
}
void insert_list2(int x)
{
if(l2==NULL)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=l2;
l2=temp;

}
else
{
q2=l2;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
while(q2->link!=NULL)
{
q2=q2->link;
}
q2->link=temp;
}
}
void insert_sumlist(int x)
{
if(s==NULL)
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=NULL;
s=temp;
}
else
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->link=s;
s=temp;
}
}
void display_sum()
{
printf("Sum list is\n");
q1=s;
while(q1!=NULL)
{
if(q1->link!=NULL)
printf("%d ->",q1->data);
else
printf("%d",q1->data);
q1=q1->link;
}
printf("\n");
}
void sumlist()
{
int sum=0;
int carry=0;
while(l1!=NULL || l2!=NULL)
{
sum=carry+l1->data+l2->data;
carry=sum/10;
sum=sum%10;
insert_sumlist(sum);
sum=carry;
l1=l1->link;
l2=l2->link;
}
if(carry!=0)
insert_sumlist(carry);
}
void display()
{
printf("List1 is\n");
q1=l1;
while(q1!=NULL)
{
if(q1->link!=NULL)
printf("%d ->",q1->data);
else
printf("%d",q1->data);
q1=q1->link;
}
printf("\n");
printf("List2 is\n");
q2=l2;
while(q2!=NULL)
{ if(q2->link!=NULL)
printf("%d ->",q2->data);
else
printf("%d",q2->data);
q2=q2->link;
}
printf("\n");
}
int main()
{
int i,num;
printf("enter no. of data elements in list1\n");
scanf("%d",&n1);
printf("Enter elemnts of list1\n");
i=0;
while(i!=n1)
{
scanf("%d",&num);
insert_list1(num);
i++;
}
printf("enter no. of data elements in list2\n");
scanf("%d",&n2);
printf("Enter elemnts of list2\n");
i=0;
while(i!=n2)
{
scanf("%d",&num);
insert_list2(num);
i++;
}
display();
sumlist();
display_sum();
}
Output:

You might also like