Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 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.  
  80. void deleteElement(struct Lista2K* myList, int position){
  81.    
  82.     struct elListy2K * new_el  = (struct elListy2K*) malloc(sizeof(struct elListy2K));
  83.    
  84.  
  85.             int ile=1;
  86.             struct elListy2K * temp = myList -> head;
  87.             while(ile<position && temp!=NULL)
  88.             {
  89.                 temp=temp->nast;
  90.                 ile++;
  91.             }
  92.             if (temp==NULL)
  93.             {
  94.                 cout<<" Nie ma nic na tej pozycji ";               
  95.             }
  96.             else
  97.             {
  98.            
  99.                 if(temp->nast == NULL){
  100.                    
  101.                     cout << " nastepny to zero ";
  102.                     cout << " poprzedni " << temp->pop->x << "|";
  103.                        
  104.                         myList -> tail = temp -> pop;
  105.                         temp->pop->nast = NULL;
  106.                    
  107.                        
  108.                 }else if (temp->pop ==NULL)
  109.                 {
  110.                    
  111.                      myList -> head = temp -> nast;
  112.                      myList->head -> pop = NULL;
  113.  
  114.                    
  115.                 }
  116.                 else{
  117.                     cout << "mam obok siebie na prawo i lewo \n usuwam dziada";
  118.                     temp -> nast -> pop = temp -> pop;
  119.                     temp -> pop -> nast = temp -> nast;
  120.                 }
  121.            
  122.                
  123.            
  124.                            
  125.             }
  126.            
  127.            
  128.    
  129. }
  130.  
  131. void deletChoosenElements(struct Lista2K* myList, int element_to_delete ){
  132.    
  133.     struct elListy2K * new_el  = (struct elListy2K*) malloc(sizeof(struct elListy2K));
  134.    
  135.             int test[2];
  136.             int ile=1;
  137.             bool find=false;
  138.             struct elListy2K * temp = myList -> head;
  139.            
  140.             while(temp!=NULL)
  141.             {
  142.                
  143.              
  144.                 if(temp->x == element_to_delete){
  145.                     cout << " wyrzucam " << temp->x;
  146.                    
  147.                     temp=temp->nast;
  148.                     deleteElement(myList, ile);
  149.                    
  150.                    
  151.                  }
  152.                  else
  153.                  {
  154.                     temp=temp->nast;
  155.                     ile++;
  156.                  }
  157.  
  158.                
  159.             }
  160.            
  161.             if (temp==NULL)
  162.             {
  163.                 cout<<" doszedlem do konca ";              
  164.             }
  165.             else
  166.             {
  167.            
  168.                
  169.                 temp -> pop -> nast = temp -> nast;
  170.                
  171.            
  172.                            
  173.             }
  174.            
  175.            
  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. void reversWrite(struct Lista2K *myList){
  199.            
  200.             struct elListy2K *el;
  201.            
  202.             el = myList -> tail;
  203.            
  204.                
  205.                 while(el!=NULL){
  206.                    
  207.                     cout << el -> x<<",  ";
  208.                    
  209.                     el= el -> pop;
  210.                    
  211.                 }  
  212.                 cout <<" \n";
  213.             el = myList -> tail;
  214. }
  215.  
  216.  
  217.  
  218. int main(int argc, char** argv) {
  219.    
  220.     struct Lista2K *  myList  = (struct Lista2K*) malloc(sizeof(struct Lista2K));
  221.     myList -> head = NULL;
  222.     myList -> tail = NULL;
  223.    
  224.     int position = 1;
  225.    
  226.         addElement(myList, 10, 1);
  227.         addElement(myList, 20, 2);
  228.         addElement(myList, 30, 3);
  229.         addElement(myList, 40, 4);
  230.         addElement(myList, 50, 5);     
  231.         addElement(myList, 60, 6);
  232.         deletChoosenElements(myList, 40);
  233.         write(myList);
  234.         reversWrite(myList);
  235.     /*  el = myList -> head;
  236.         while(el!=NULL){
  237.            
  238.             cout << el -> x<<",  ";
  239.             el= el -> nast;
  240.            
  241.         }   */
  242.        
  243.     return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement