Skip to content

Instantly share code, notes, and snippets.

@bitwiser
Created February 23, 2014 11:55
Show Gist options
  • Save bitwiser/9170428 to your computer and use it in GitHub Desktop.
Save bitwiser/9170428 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<fstream>
using namespace std;
struct car{
int num;
float price,mileage,distance;
int year;
string make,model,color;
struct car *next;
};
car *head = NULL;
int cnt = 0;
car *newNode(float price,float mileage,float distance,int year,string make,string model,string color,bool n){
car *c = new car;
if(n)
c->num = cnt++;
else
c->num = -1;
c->price = price;
c->mileage = mileage;
c->distance = distance;
c->year = year;
c->make = make;
c->model = model;
c->color = color;
c->next = NULL;
return c;
}
void printNode(car *t){
cout<<"Vehicle #"<<t->num<<"\n";
cout<<"-------------------\n";
cout<<"Price: "<<t->price<<endl;
cout<<"Year: "<<t->year<<endl;
cout<<"Miles: "<<t->mileage<<endl;
cout<<"Make: "<<t->make<<endl;
cout<<"Model: "<<t->model<<endl;
cout<<"Color: "<<t->color<<endl;
cout<<"Dist: "<<t->distance<<endl<<endl;
}
car *findNode(car *t){
if(head==NULL){
return NULL;
}
cout<<"Finding\n";
car *c = head;
while(c!=NULL){
if(t->price==c->price && t->mileage==c->mileage && t->distance==c->distance && t->year==c->year && t->make.compare(c->make)==0 && t->model.compare(c->model)==0 && t->color.compare(c->color)==0){
printNode(c);
return c;
}
c = c->next;
}
return c;
}
void add(car *h){
if(head==NULL){
head = h;
}else{
car *t = head;
while(t->next!=NULL){
t = t->next;
}
t->next = h;
}
}
void removeNode(car *t){
car *c = findNode(t);
car *c1;
if(c==NULL){
cout<<"Car Not Found to remove\n\n";
}else{
c1 = c;
c = c->next;
delete c1;
}
}
void display(){
if(head==NULL){
cout<<"Nothing to display.\n";
return;
}
cout<<"Listing all vehicles:\n";
cout<<"---------------------------------\n";
car *t = head;
while(t!=NULL){
printNode(t);
t= t->next;
}
}
int main(){
ifstream in;
in.open("in.txt");
char ch;
float price,mileage,distance;
int year;
string make,model,color;
car *temp = NULL;
while(in>>ch){
//cout<<ch<<endl;
if(ch!='L'){
//cout<<ch<<endl;
in>>price>>year>>mileage>>make>>model>>color>>distance;
//cout<<price<<" "<<year<<" "<<mileage<<" "<<make<<" "<<model<<" "<<color<<" "<<distance<<endl;
switch(ch){
case 'A':
temp = newNode(price,mileage,distance,year,make,model,color,true);
add(temp);
temp = NULL;
break;
case 'R':
in>>price>>year>>mileage>>make>>model>>color>>distance;
temp = newNode(price,mileage,distance,year,make,model,color,false);
//printNode(temp);
removeNode(temp);
delete temp;
temp = NULL;
break;
case 'P':
/*in>>price>>year>>mileage>>make>>model>>color>>distance;
temp = newNode(price,mileage,distance,year,make,model,color,false);
car *t;
t = findNode(temp);
printNode(t);
if(t!=NULL){
printNode(t);
}else{
delete temp;
temp = NULL;
}*/
break;
default: break;
}
}else if(ch=='L')
display();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment