Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  6. using namespace std;
  7. struct elListy2K {
  8.     int x;
  9.     struct elListy2K *nast;
  10.     struct elListy2K *pop;
  11. };
  12.  
  13. struct Lista2K {
  14.     struct elListy2K *head;
  15.     struct elListy2K *tail;
  16. };
  17.  
  18. void addElement(struct Lista2K* myList, int value , int pos){
  19.    
  20.     struct elListy2K * new_el  = (struct elListy2K*) malloc(sizeof(struct elListy2K));
  21.    
  22.    
  23.     new_el -> x = value;
  24.  
  25.    
  26.  
  27.  
  28.  
  29.     if(myList -> head == NULL){
  30.         myList -> head =  new_el;
  31.         myList -> tail = new_el;
  32.         new_el -> nast = NULL;
  33.         new_el -> pop = NULL;
  34.     }
  35.     else
  36.         if (pos==1)
  37.         {
  38.             myList -> head -> pop = new_el;
  39.             new_el->nast=myList->head;
  40.             new_el -> pop = NULL;
  41.             myList->head=new_el;    
  42.         }
  43.         else
  44.         {
  45.             int ile=1;
  46.             struct elListy2K * temp=myList->head;
  47.             while(ile<pos && temp!=NULL)
  48.             {
  49.                 temp=temp->nast;
  50.                 ile++;
  51.             }
  52.             if (temp!=NULL)
  53.             {
  54.                 //wstawiamy przed temp
  55.                 temp->pop -> nast = new_el;
  56.                 new_el -> pop = temp -> pop;
  57.                
  58.                 temp-> pop = new_el;
  59.                 new_el -> nast = temp;
  60.                
  61.             }
  62.             else
  63.             {
  64.                 //wstawiamy na koniec listy
  65.                 myList -> tail -> nast =  new_el;
  66.                 new_el->pop=myList->tail;
  67.                 new_el -> nast = NULL;
  68.                 myList->tail=new_el;
  69.                
  70.             }
  71.            
  72.         }
  73.    
  74.    
  75.  
  76.    
  77. }
  78.  
  79. void deleteElement(struct Lista2K* myList, int position){
  80.    
  81.     struct elListy2K * new_el  = (struct elListy2K*) malloc(sizeof(struct elListy2K));
  82.    
  83.  
  84.             int ile=1;
  85.             struct elListy2K * temp = myList -> head;
  86.             while(ile<position && temp!=NULL)
  87.             {
  88.                 temp=temp->nast;
  89.                 ile++;
  90.             }
  91.             if (temp==NULL)
  92.             {
  93.                 cout<<" Nie ma nic na tej pozycji ";              
  94.             }
  95.             else
  96.             {
  97.            
  98.                 if(temp->nast == NULL){
  99.                    
  100.                        
  101.                         myList -> tail = temp -> pop;
  102.                         temp->pop->nast = NULL;
  103.                    
  104.                        
  105.                 }else if (temp->pop ==NULL)
  106.                 {
  107.                    
  108.                      myList -> head = temp -> nast;
  109.                      myList->head -> pop = NULL;
  110.  
  111.                    
  112.                 }
  113.                 else{
  114.                    
  115.                     temp -> nast -> pop = temp -> pop;
  116.                     temp -> pop -> nast = temp -> nast;
  117.                 }
  118.            
  119.                
  120.            
  121.                            
  122.             }
  123.             free(temp);
  124.            
  125.            
  126.            
  127.    
  128. }
  129.  
  130. int deletChoosenElements(struct Lista2K* myList, int element_to_delete ){
  131.    
  132.     struct elListy2K * new_el  = (struct elListy2K*) malloc(sizeof(struct elListy2K));
  133.    
  134.             int test[2];
  135.             int ile=1;
  136.             bool find=false;
  137.             struct elListy2K * temp = myList -> head;
  138.             int how_many_delete = 0;
  139.            
  140.             while(temp!=NULL)
  141.             {
  142.                
  143.              
  144.                 if(temp->x == element_to_delete){
  145.                    
  146.                     temp=temp->nast;
  147.                     deleteElement(myList, ile);
  148.                     how_many_delete++;
  149.                    
  150.                    
  151.                  }
  152.                  else
  153.                  {
  154.                     temp=temp->nast;
  155.                     ile++;
  156.                  }
  157.  
  158.                
  159.             }
  160.            
  161.             if (temp==NULL)
  162.             {
  163.                            
  164.             }
  165.             else
  166.             {
  167.            
  168.                
  169.                 temp -> pop -> nast = temp -> nast;
  170.                
  171.            
  172.                            
  173.             }
  174.            free(temp);
  175.            return how_many_delete;
  176.    
  177. }
  178.  
  179. void write(struct Lista2K *myList){
  180.            
  181.             struct elListy2K *el;
  182.            
  183.             el = myList -> head;
  184.            
  185.                
  186.                 while(el!=NULL){
  187.                    
  188.                     cout << "| " << el -> x<<" ";
  189.                    
  190.                     el= el -> nast;
  191.                    
  192.                 }  
  193.             el = myList -> head;
  194.             cout <<" \n";
  195.        
  196. }
  197.  
  198.  
  199. void reversWrite(struct Lista2K *myList){
  200.            
  201.             struct elListy2K *el;
  202.            
  203.             el = myList -> tail;
  204.            
  205.                
  206.                 while(el!=NULL){
  207.                    
  208.                      cout << "| " << el -> x <<" ";
  209.                    
  210.                     el= el -> pop;
  211.                    
  212.                 }  
  213.                 cout <<" \n";
  214.             el = myList -> tail;
  215.            
  216. }
  217.  
  218.  
  219.  
  220. int main(int argc, char** argv) {
  221.    
  222.     struct Lista2K *  myList  = (struct Lista2K*) malloc(sizeof(struct Lista2K));
  223.     myList -> head = NULL;
  224.     myList -> tail = NULL;
  225.    
  226.     int position = 1;
  227.     int option = 0;
  228.     int value_user;
  229.     int position_user;
  230.     int how_many;
  231.     cout <<"Wybierz opcje: \n";
  232.     cout <<" 1.Dodanie elementu do listy na podaną pozycję\n 2.Usunięcie elementu z listy z podanej pozycji\n ";
  233.     cout << "3.Usunięcie z listy wszystkich elementów o podanej wartości i wyświetlenie ich liczby \n 4.Wydruk elementów listy od początku \n 5.Wydruk elementów listy od konca.\n 6.Koniec programu";
  234.      
  235.         while(option!=6){
  236.             cout <<"\n Wybierz opcje: \n";
  237.             cin >> option;
  238.             switch( option )
  239.             {
  240.             case 1:
  241.                 cout << "Ile elementow chcesz dodac?";
  242.                 cin >> how_many;
  243.                
  244.                
  245.                 for(int i=0;i<how_many;i++){
  246.                     cout << "Podaj wartosc: \n";
  247.                     cin >> value_user;
  248.                     cout << "Podaj numer pozycji na jaka chcesz wstawic: \n";
  249.                     cin >> position_user;  
  250.                     addElement(myList,value_user, position_user );
  251.                 }
  252.              
  253.                 break;
  254.                
  255.             case 2:
  256.                
  257.                 cout << "Element z ktorej pozycji chcesz usunac?\n";
  258.                 write(myList);
  259.                 cin >> position_user;
  260.                 deleteElement(myList, position_user);
  261.                 write(myList);
  262.                
  263.                 break;
  264.             case 3:
  265.                 cout << "Jakie wartosci chcesz usunac z listy?\n";
  266.                 write(myList);
  267.                 cin >> value_user;
  268.                
  269.                 cout << "Usunieto: " << deletChoosenElements(myList,value_user) << " wartosci \n";;
  270.                
  271.                 write(myList);
  272.                
  273.                
  274.                 break;
  275.             case 4:
  276.                 write(myList);
  277.                 break;
  278.             case 5:
  279.                 reversWrite(myList);
  280.                 break;    
  281.             case 6:
  282.                  cout <<"Wychodze";
  283.                 break;    
  284.             default:
  285.                 cout<<" Nie ma takiej opcji ";
  286.                 break;
  287.             }
  288.            
  289.         }
  290.  
  291.      
  292.      
  293.        
  294.     return 1;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement