Advertisement
A0D1MQ

lab1 - linked list

Mar 12th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. struct Product{
  8.     int code;
  9.     int value;
  10.     int price;
  11.     char* name;
  12.     Product* next;
  13. };
  14.  
  15. Product* head=NULL;
  16. Product* tmp=NULL;
  17.  
  18. /////////////////////////////////////
  19. int addProduct(int code, int value, int price,const char* name){
  20.     cout<<"Adding: \n"<<code<<"\t"<<value<<"\t"<<price<<"\t"<<name<<" \n";
  21.    
  22. //Product* n = new Product;
  23. //n->code=code;
  24. //n->value=value;
  25. //n->price=price;  
  26. //
  27. //char* c=new char[strlen(name)+1];
  28. //strcpy(c,name);
  29. //n->name=c;  
  30. //
  31. //n->next=NULL;
  32.    
  33.     //n->name=name;
  34.     ////
  35.     /*
  36.      * We can add a new product:
  37.      * -to the empty list   (if head==NULL)
  38.      * -at the beggining    (if  && tmp->next->code > code)
  39.      * -in the middle       (if tmp->next->code > code && tmp->code < code) then insert between tmp and tmp->next
  40.      * -at the end          (if tmp->code < code && tmp->next==NULL)
  41.      *
  42.      *
  43.      */
  44.    
  45.     //WORKING ADDING ELEMENTS IN A SORTED WAY//////   {
  46.     /*if (head == NULL || head->code >= n->code){
  47.         n->next = head;
  48.         head = n;
  49.     }
  50.     else{
  51.         tmp = head;
  52.         while (tmp->next!=NULL && tmp->next->code < n->code){
  53.             tmp = tmp->next;
  54.         }
  55.         n->next = tmp->next;
  56.         tmp->next = n;
  57.     }*/
  58.     //////////////////////////////////////////////   }
  59.    
  60.    
  61.     if (head == NULL || head->code > code){
  62.         Product* n = new Product;
  63.         n->code=code;
  64.         n->value=value;
  65.         n->price=price;  
  66.  
  67.         char* c=new char[strlen(name)+1];
  68.         strcpy(c,name);
  69.         n->name=c;  
  70.  
  71.        
  72.         n->next = head;
  73.         head = n;
  74.     }
  75.     else{
  76.         tmp = head;
  77.         while (tmp->next!=NULL && tmp->next->code < code){
  78.             tmp = tmp->next;
  79.         }
  80.         //cout<<tmp->code<<endl;
  81.         if (tmp->code == code){             //if there is a product of this code
  82.            
  83.             if(!strcmp(tmp->name,name)){ //if code and name is the same change value
  84.                    
  85.                 tmp->value=value;
  86.                 return 1;
  87.             }
  88.             else{                           //if only code is the same update everything
  89.                
  90.                 tmp->value=value;
  91.                 tmp->price=price;
  92.                 strcpy(tmp->name,name);
  93.                
  94.                 return 0;
  95.             }
  96.         }
  97.         else{
  98.         Product* n = new Product;
  99.         n->code=code;
  100.         n->value=value;
  101.         n->price=price;  
  102.  
  103.         char* c=new char[strlen(name)+1];
  104.         strcpy(c,name);
  105.         n->name=c;  
  106.  
  107.        
  108.         n->next = tmp->next;
  109.         tmp->next = n;
  110.         }
  111.     }
  112.    
  113.     /////    
  114.    
  115. //    if (head!=NULL){      //CTRL + SHIFT + C
  116. //        tmp=head;
  117. //        while(tmp->next!=NULL){
  118. //            if (tmp->code==code){
  119. //                if(tmp->value==value && tmp->price==price && !strcmp(tmp->name,name)/*tmp->name==name*/){
  120. //                    return 1;
  121. //                }
  122. //                tmp->code=code;
  123. //                tmp->value+=value;
  124. //                tmp->price=price;
  125. //                //tmp->name=name;
  126. //                tmp->name=c;
  127. //                if(tmp->value==value && tmp->price==price && !strcmp(tmp->name,name)/*tmp->name==name*/){
  128. //                    return 1;
  129. //                }
  130. //                else
  131. //                    return 0;
  132. //            }
  133. //            tmp=tmp->next;
  134. //            
  135. //            if (tmp->code<code && tmp->next->code>code){ ///inserting new product in the sorted way is not working yet
  136. //                Product* curr=tmp;
  137. //                tmp=tmp->next;
  138. //                curr->next=n;
  139. //                n->next=tmp;
  140. //                }
  141. //            
  142. //        }
  143. //        tmp->next=n;
  144. //    }
  145. //    else
  146. //        head=n;
  147.     //cout << "Adding function ended\n";
  148. }
  149. ////////////////////////////////////
  150. int removeProduct(int code){
  151.  
  152.     Product* curr;
  153.     Product* del=NULL;
  154.     tmp=head;
  155.     curr=head;
  156.     while(curr!=NULL && curr->code!=code){
  157.         tmp=curr;
  158.         curr=curr->next;
  159.     }
  160.     if(curr==NULL){
  161.         cout<<"Product was not in the list\n";
  162.         return 0;
  163.     }
  164.     else{
  165.         del=curr;
  166.         curr=curr->next;
  167.         tmp->next=curr;
  168.         if(del==head){
  169.             head=head->next;
  170.             tmp=NULL;
  171.         }
  172.         cout<<"Product " << del->code << " was deleted\n";
  173.         delete[] del->name;
  174.         delete del;
  175.         return 1;
  176.     }
  177.  
  178.  
  179. }
  180. ////////////////////////////////////
  181. void print(){
  182.  
  183.     tmp=head;
  184.     cout<<"Code\t"<<"Value\t"<<"Price\t"<<"Name\t\n";
  185.     while(tmp!=NULL){
  186.         cout<<tmp->code<<"\t"<<tmp->value<<"\t"<<tmp->price<<"\t"<<tmp->name<<" \n";
  187.         tmp=tmp->next;
  188.     }
  189.  
  190. }
  191.  
  192. int main(){
  193.  
  194.     print();            ///printing empty list
  195.     removeProduct(2);   /// removing product from an empty list
  196.  
  197.     addProduct(1,5,6,"Test1");
  198.     addProduct(6,5,6,"Test6");
  199.     print();
  200.     removeProduct(7);
  201.    
  202.     addProduct(1,5,6,"Test1");   ///adding exact same product
  203.     addProduct(1,5,6,"Test1_2"); ///adding different product with the same code
  204.     addProduct(1,5,6,"Test1_2");
  205.     addProduct(1,6,6,"Test1_2");
  206.     addProduct(2,5,6,"Test2");
  207.     print();
  208.    
  209.     removeProduct(2);
  210.     removeProduct(1);           ///removing actual product from the list
  211.     print();
  212.  
  213.     return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement