Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <cstring>
- using namespace std;
- struct Product{
- int code;
- int value;
- int price;
- char* name;
- Product* next;
- };
- Product* head=NULL;
- Product* tmp=NULL;
- /////////////////////////////////////
- int addProduct(int code, int value, int price,const char* name){
- cout<<"Adding: \n"<<code<<"\t"<<value<<"\t"<<price<<"\t"<<name<<" \n";
- //Product* n = new Product;
- //n->code=code;
- //n->value=value;
- //n->price=price;
- //
- //char* c=new char[strlen(name)+1];
- //strcpy(c,name);
- //n->name=c;
- //
- //n->next=NULL;
- //n->name=name;
- ////
- /*
- * We can add a new product:
- * -to the empty list (if head==NULL)
- * -at the beggining (if && tmp->next->code > code)
- * -in the middle (if tmp->next->code > code && tmp->code < code) then insert between tmp and tmp->next
- * -at the end (if tmp->code < code && tmp->next==NULL)
- *
- *
- */
- //WORKING ADDING ELEMENTS IN A SORTED WAY////// {
- /*if (head == NULL || head->code >= n->code){
- n->next = head;
- head = n;
- }
- else{
- tmp = head;
- while (tmp->next!=NULL && tmp->next->code < n->code){
- tmp = tmp->next;
- }
- n->next = tmp->next;
- tmp->next = n;
- }*/
- ////////////////////////////////////////////// }
- if (head == NULL || head->code > code){
- Product* n = new Product;
- n->code=code;
- n->value=value;
- n->price=price;
- char* c=new char[strlen(name)+1];
- strcpy(c,name);
- n->name=c;
- n->next = head;
- head = n;
- }
- else{
- tmp = head;
- while (tmp->next!=NULL && tmp->next->code < code){
- tmp = tmp->next;
- }
- //cout<<tmp->code<<endl;
- if (tmp->code == code){ //if there is a product of this code
- if(!strcmp(tmp->name,name)){ //if code and name is the same change value
- tmp->value=value;
- return 1;
- }
- else{ //if only code is the same update everything
- tmp->value=value;
- tmp->price=price;
- strcpy(tmp->name,name);
- return 0;
- }
- }
- else{
- Product* n = new Product;
- n->code=code;
- n->value=value;
- n->price=price;
- char* c=new char[strlen(name)+1];
- strcpy(c,name);
- n->name=c;
- n->next = tmp->next;
- tmp->next = n;
- }
- }
- /////
- // if (head!=NULL){ //CTRL + SHIFT + C
- // tmp=head;
- // while(tmp->next!=NULL){
- // if (tmp->code==code){
- // if(tmp->value==value && tmp->price==price && !strcmp(tmp->name,name)/*tmp->name==name*/){
- // return 1;
- // }
- // tmp->code=code;
- // tmp->value+=value;
- // tmp->price=price;
- // //tmp->name=name;
- // tmp->name=c;
- // if(tmp->value==value && tmp->price==price && !strcmp(tmp->name,name)/*tmp->name==name*/){
- // return 1;
- // }
- // else
- // return 0;
- // }
- // tmp=tmp->next;
- //
- // if (tmp->code<code && tmp->next->code>code){ ///inserting new product in the sorted way is not working yet
- // Product* curr=tmp;
- // tmp=tmp->next;
- // curr->next=n;
- // n->next=tmp;
- // }
- //
- // }
- // tmp->next=n;
- // }
- // else
- // head=n;
- //cout << "Adding function ended\n";
- }
- ////////////////////////////////////
- int removeProduct(int code){
- Product* curr;
- Product* del=NULL;
- tmp=head;
- curr=head;
- while(curr!=NULL && curr->code!=code){
- tmp=curr;
- curr=curr->next;
- }
- if(curr==NULL){
- cout<<"Product was not in the list\n";
- return 0;
- }
- else{
- del=curr;
- curr=curr->next;
- tmp->next=curr;
- if(del==head){
- head=head->next;
- tmp=NULL;
- }
- cout<<"Product " << del->code << " was deleted\n";
- delete[] del->name;
- delete del;
- return 1;
- }
- }
- ////////////////////////////////////
- void print(){
- tmp=head;
- cout<<"Code\t"<<"Value\t"<<"Price\t"<<"Name\t\n";
- while(tmp!=NULL){
- cout<<tmp->code<<"\t"<<tmp->value<<"\t"<<tmp->price<<"\t"<<tmp->name<<" \n";
- tmp=tmp->next;
- }
- }
- int main(){
- print(); ///printing empty list
- removeProduct(2); /// removing product from an empty list
- addProduct(1,5,6,"Test1");
- addProduct(6,5,6,"Test6");
- print();
- removeProduct(7);
- addProduct(1,5,6,"Test1"); ///adding exact same product
- addProduct(1,5,6,"Test1_2"); ///adding different product with the same code
- addProduct(1,5,6,"Test1_2");
- addProduct(1,6,6,"Test1_2");
- addProduct(2,5,6,"Test2");
- print();
- removeProduct(2);
- removeProduct(1); ///removing actual product from the list
- print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement