Can I know the computational complexity and time complexity and space complexity? #include #include #i
Posted: Thu Jun 02, 2022 8:30 am
Can I know the computational complexity and time complexity and
space complexity?
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
// assuming total tickets 400
#define total 400
//structure of node
struct person
{
string name,address;
int ticket;
// pointer to next node
struct person *next;
};
// for head and last ptr
struct person *head=NULL,*last;
// main function
int main()
{ // variables
string name1,address1;
int ticket1,remainingTicket=total;
cout<<"You can exit by entering 0 in name inputs \n";
// LOOP untill quit or HOUSE FULL
while(1)
{ // checking for housefull
if(remainingTicket==0)
{
cout<<"\nHOUSE FULL ";
break;
}
// taking inputs of user
cout<<"\nEnter FIrst Name of person : ";
cin>>name1;
// checking for termination
if(name1[0]=='0' && name1.length()==1)
break;
// to clear the input buffer
cin.ignore();
cout<<"Enter Address of person : ";
getline(cin,address1);
cout<<"Enter how many tickets : ";
cin>>ticket1;
// checking for available no of tickets
while(remainingTicket<ticket1)
{
cout<<ticket1<<" no. of tickets NOT available.";
cout<<"\nAvailable No of tickets :
"<<remainingTicket;
cout<<"\nEnter total tickets <=
"<<remainingTicket<<" : ";
cin>>ticket1;
}
// initial condition
if(head==NULL)
{
// creating dynamically node
struct person *ptr=new(struct person);
// assigining value to the node
ptr->name=name1;
ptr->address=address1;
ptr->ticket=ticket1;
ptr->next=NULL;
head=ptr;
last=ptr;
remainingTicket-=ticket1;
}
else
{
struct person *ptr=new(struct person);
ptr->name=name1;
ptr->address=address1;
ptr->ticket=ticket1;
ptr->next=NULL;
// last ptr points last node
last->next=ptr;
last=ptr;
remainingTicket-=ticket1;
}
}
// showing all booked tickets
struct person *temp;
temp=head;
int i=1;
cout<<endl;
while(temp->next!=NULL)
{
cout<<endl<<i++<<".";
cout<<"\nName of person : "<<temp->name;
cout<<"\nAddress of person :
"<<temp->address;
cout<<"\nBooked Tickets :
"<<temp->ticket<<endl;
temp=temp->next;
}
cout<<endl<<i++<<".";
cout<<"\nName of person : "<<temp->name;
cout<<"\nAddress of person :
"<<temp->address;
cout<<"\nBooked Tickets :
"<<temp->ticket<<endl;
return 0;
}
space complexity?
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cstring>
using namespace std;
// assuming total tickets 400
#define total 400
//structure of node
struct person
{
string name,address;
int ticket;
// pointer to next node
struct person *next;
};
// for head and last ptr
struct person *head=NULL,*last;
// main function
int main()
{ // variables
string name1,address1;
int ticket1,remainingTicket=total;
cout<<"You can exit by entering 0 in name inputs \n";
// LOOP untill quit or HOUSE FULL
while(1)
{ // checking for housefull
if(remainingTicket==0)
{
cout<<"\nHOUSE FULL ";
break;
}
// taking inputs of user
cout<<"\nEnter FIrst Name of person : ";
cin>>name1;
// checking for termination
if(name1[0]=='0' && name1.length()==1)
break;
// to clear the input buffer
cin.ignore();
cout<<"Enter Address of person : ";
getline(cin,address1);
cout<<"Enter how many tickets : ";
cin>>ticket1;
// checking for available no of tickets
while(remainingTicket<ticket1)
{
cout<<ticket1<<" no. of tickets NOT available.";
cout<<"\nAvailable No of tickets :
"<<remainingTicket;
cout<<"\nEnter total tickets <=
"<<remainingTicket<<" : ";
cin>>ticket1;
}
// initial condition
if(head==NULL)
{
// creating dynamically node
struct person *ptr=new(struct person);
// assigining value to the node
ptr->name=name1;
ptr->address=address1;
ptr->ticket=ticket1;
ptr->next=NULL;
head=ptr;
last=ptr;
remainingTicket-=ticket1;
}
else
{
struct person *ptr=new(struct person);
ptr->name=name1;
ptr->address=address1;
ptr->ticket=ticket1;
ptr->next=NULL;
// last ptr points last node
last->next=ptr;
last=ptr;
remainingTicket-=ticket1;
}
}
// showing all booked tickets
struct person *temp;
temp=head;
int i=1;
cout<<endl;
while(temp->next!=NULL)
{
cout<<endl<<i++<<".";
cout<<"\nName of person : "<<temp->name;
cout<<"\nAddress of person :
"<<temp->address;
cout<<"\nBooked Tickets :
"<<temp->ticket<<endl;
temp=temp->next;
}
cout<<endl<<i++<<".";
cout<<"\nName of person : "<<temp->name;
cout<<"\nAddress of person :
"<<temp->address;
cout<<"\nBooked Tickets :
"<<temp->ticket<<endl;
return 0;
}