DS Lab quiz 1
There are n cars at a gas station .Every time a new car arrive it joins the line from the end.the car came from first served fist.implement a linked list based solution in order to record the information.Store the following properties of car in your linked list.
- Car plate No
- Car Model
- Car's position in the line
your solution must be contain following functions.
- Add the information of a car.
- update car info
- display the total size.
Source Code:
#include <iostream>
using namespace std;
struct car{
string CplateNo;
int model;
int carposition;
car*next;
};car *head=NULL;
car* temp=NULL;
string cno;
int mod,cp=0,sizecount=0;
//add
void add(string Carno,int carmod)
{
bool flag=false;
car* c=new car;
c->carposition=cp++;
c->CplateNo=Carno;
c->model =carmod;
c->next=NULL;
if(head==NULL)
{
sizecount++;
head=c;
cout<<"Information is entered"<<endl;
}
else{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=c;
cout<<"Information is entered"<<endl;
sizecount++;
}
}
// update car info
void update(int carpos)
{
bool flag=false;
if(head==NULL)
{
cout<<"LInked list is empty"<<endl;
}
else
{
temp=head;
while(temp->next!=NULL)
{
if(temp->carposition==carpos)
{
cout<<"Enter Car plate no: ";
cin>>cno;
cout<<"ENter Model";
cin>>mod;
temp->CplateNo=cno;
temp->model=mod;
break;
}
temp=temp->next;
}
}
if(flag==true)
{
cout<<"Value is updated "<<endl;
}
else
cout<<"Invalid position"<<endl;
}
int main()
{ int ch;
do{
cout<<"1: Add The information of Car /n"<<endl;
cout<<"2: Update the car info /n"<<endl;
cout<<"3: Display total size /n"<<endl;
cout<<"4: exit /n"<<endl;
cout<<"ENter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"Enter Car Plate NO: ";
cin>>cno;
cout<<"ENter MOdel: ";
cin>>mod;
add(cno,mod);
break;
}
case 2:
{
cout<<"Enter car position: ";
cin>>cp;
update(cp);
break;
}
case 3:
{
cout<<" The size of cars are "<<sizecount<<endl;
break;
}
case 4:
{
break;
}
default:
cout<<"invalid output /n"<<endl;
}
}while(ch!=4);
return 0;
}
Comments
Post a Comment