Advertisement
majczel23000

[AiSD] 3. Listy

Nov 29th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //===========================
  5. //JEDNOKIERUNKOWA
  6. //===========================
  7. struct wezel{
  8.     wezel *next;
  9.     int key;
  10. };
  11.  
  12. void print(wezel *head)
  13. {
  14.     cout<<"[LISTA] ";
  15.     if(head==NULL)
  16.         cout<<"Lista jednokierunkowa pusta";
  17.     else
  18.         while(head!=NULL)
  19.         {
  20.             if(head->next!=0)
  21.                 cout<<head->key<<" -> ";
  22.             else
  23.                 cout<<head->key;
  24.             head = head->next;
  25.         }
  26.     cout<<endl;
  27. }
  28.  
  29. void add(wezel *&head, int key)
  30. {
  31.     wezel * n = new wezel;
  32.     n->key = key;
  33.     if(head == NULL)
  34.     {
  35.         head = n;
  36.         n->next = 0;
  37.     }
  38.     else
  39.     {
  40.         if(head->key > key)
  41.         {
  42.             n->next = head;
  43.             head = n;
  44.         }
  45.         else
  46.         {
  47.             wezel *p = head;
  48.             wezel *x = head->next;
  49.             while(x != NULL && x->key <= key)
  50.             {
  51.                 p = x;
  52.                 x = x->next;
  53.             }
  54.             n->next = x;
  55.             p->next = n;
  56.         }
  57.     }
  58. }
  59.  
  60. void search(wezel* head,int key)
  61. {
  62.     while(head!=NULL && key != head->key)
  63.         head= head->next;
  64.     if(head!=0)
  65.         cout<<"Element o kluczu: "<<key<< " istnieje i znajduje sie pod adresem: "<<head<<endl;
  66.     else
  67.         cout<<"Element o kluczu: "<<key<< " nie istnieje"<<endl;
  68. }
  69.  
  70. void usun(wezel *& head, int key)
  71. {
  72.     if(head == NULL)
  73.         cout<<"Lista pusta"<<endl;
  74.     else if(head->key > key)
  75.     {
  76.         cout<<"Nie ma takiego elementu"<<endl;
  77.     }
  78.     else if(head->key == key)
  79.     {
  80.         wezel *p = head->next;
  81.         delete head;
  82.         head = p;
  83.         cout<<"Usunieto head o kluczu: "<<key<<endl;
  84.     }
  85.     else
  86.     {
  87.         wezel *p, *n;
  88.         p = head;
  89.         n = head->next;
  90.         for( ; n != 0; n = n->next)
  91.         {
  92.             if(key == n->key)
  93.             {
  94.                 p->next = n->next;
  95.                 delete n;
  96.                 cout<<"Usunieto element o kluczu: "<<key<<endl;
  97.             }
  98.             else
  99.                 p = n;
  100.         }
  101.     }
  102. }
  103.  
  104. void usun_liste(wezel * & head)
  105. {
  106.     wezel *p;
  107.     while(head!=0)
  108.     {
  109.         p = head->next;
  110.         delete head;
  111.         head = p;
  112.     }
  113.     cout<<"Usunieto liste jednokierunkowa"<<endl;
  114. }
  115.  
  116. void poprzedni(wezel * head, int x){
  117.     wezel *p = head;
  118.     wezel *poprzedni = head;
  119.     for(p, poprzedni; p != NULL && poprzedni != NULL; p = p->next){
  120.         if(x == head->key){
  121.             cout << "To jest pierwszy element, przed nim nic nie ma" << endl;
  122.             break;
  123.         }
  124.         if(p->key == x)
  125.             cout << "Poprzednik "<< x<< " to "<<poprzedni->key << endl;
  126.         if(p != head)
  127.             poprzedni = poprzedni->next;
  128.     }
  129. }
  130.  
  131.  
  132. //===========================
  133. //DWUKIERUNKOWA
  134. //===========================
  135.  
  136. struct wezel2{
  137.     wezel2 *next;
  138.     wezel2 *prev;
  139.     int key;
  140. };
  141.  
  142.  
  143. void print2(wezel2 *head)
  144. {
  145.     cout<<"[LISTA] ";
  146.     if(head==NULL)
  147.         cout<<"Lista dwukierunkowa pusta";
  148.     else
  149.         while(head!=NULL)
  150.         {
  151.            if(head->next!=0)
  152.                 cout<<head->key<<" <-> ";
  153.             else
  154.                 cout<<head->key;
  155.             head = head->next;
  156.         }
  157.     cout<<endl;
  158. }
  159.  
  160. void print2_from_end(wezel2 *head)
  161. {
  162.     cout<<"[LISTA - od konca] ";
  163.     if(head==NULL)
  164.         cout<<"Lista dwukierunkowa pusta";
  165.     else
  166.         while(head->next!=NULL)
  167.             head = head->next;
  168.         while(head!=NULL)
  169.         {
  170.             if(head->prev!=0)
  171.                 cout<<head->key<<" <-> ";
  172.             else
  173.                 cout<<head->key;
  174.             head = head->prev;
  175.         }
  176.     cout<<endl;
  177. }
  178.  
  179. void poprzedni2(wezel2 * head, int x){
  180.     wezel2 *p = head;
  181.     for(p; p != NULL ; p = p->next){
  182.         if(x == head->key){
  183.             cout << "To jest pierwszy element, przed nim nic nie ma" << endl;
  184.             break;
  185.         }
  186.         if(p->key == x)
  187.             cout << "Poprzednik "<< x<< " to "<<p->prev->key << endl;
  188.     }
  189. }
  190.  
  191. void add2(wezel2 *&head, int key)
  192. {
  193.     wezel2 * n = new wezel2;
  194.     n->key = key;
  195.     if(head == NULL)    //jesli pusta
  196.     {
  197.         head = n;
  198.         n->next = 0;
  199.         n->prev = 0;
  200.     }
  201.     else
  202.     {
  203.         if(head->key > key) //jesli dodajemy przed head
  204.         {
  205.             n->next = head;
  206.             n->prev = 0;
  207.             head->prev = n;
  208.             head = n;
  209.         }
  210.         else    //dodajemy za head
  211.         {
  212.             wezel2 *p = head;
  213.             wezel2 *x = head->next;
  214.             while(x != NULL && x->key <= key)
  215.             {
  216.                 p = x;
  217.                 x = x->next;
  218.             }
  219.             if(x==NULL)
  220.                 n->next = 0;
  221.             else
  222.             {
  223.                 x->prev = n;
  224.                 n->next = x;
  225.             }
  226.             n->prev = p;
  227.             p->next = n;
  228.         }
  229.     }
  230. }
  231.  
  232. void dodaj_koniec2(wezel2* &head, int key)
  233. {
  234.     wezel2 * n = new wezel2;
  235.     n->key = key;
  236.     wezel2 *p = head;
  237.     if(head==NULL)  //jesli jest pusta to dodawanie na koniec dodaje nowy head
  238.     {
  239.          head = n;
  240.          n->next = 0;
  241.          n->prev = 0;
  242.     }
  243.     else    //jesli juz cos jest to dodaje na koniec
  244.     {
  245.         while(p->next!=0)
  246.             p = p->next;
  247.         p->next = n;
  248.         n->next = 0;
  249.         n->prev = p;
  250.     }
  251. }
  252.  
  253. void dodaj_pocz2(wezel2* &head, int key)
  254. {
  255.     wezel2 * n = new wezel2;
  256.     n->key = key;
  257.     if(head==NULL)  //jesli jest pusta
  258.          n->next = 0;
  259.     else       //jesli cos jest
  260.         n->next = head;
  261.     head = n;
  262.     n->prev = 0;
  263. }
  264.  
  265. void usun2(wezel2 *& head, int key)
  266. {
  267.     //usuwa tylko pierwsze wystąpienie
  268.     if(head == NULL)
  269.         cout<<"Lista pusta"<<endl;
  270.     else if(head->key > key)
  271.     {
  272.         cout<<"Nie ma takiego elementu"<<endl;
  273.     }
  274.     else if(head->key == key)
  275.     {
  276.         wezel2 *p = head->next;
  277.         delete head;
  278.         head = p;
  279.         head->prev = 0;
  280.         cout<<"Usunieto head o kluczu: "<<key<<endl;
  281.     }
  282.     else
  283.     {
  284.         wezel2 *p, *n;
  285.         p = head;
  286.         n = head->next;
  287.         while(n!=0 && key!=n->key)
  288.         {
  289.             p = n;
  290.             n=n->next;
  291.         }
  292.         p->next = n->next;
  293.         if(n->next!=0)
  294.             n->next->prev = p;
  295.         delete n;
  296.         cout<<"Usunieto element o kluczu: "<<key<<endl;
  297.     }
  298. }
  299.  
  300. void usun_liste2(wezel2 * & head)
  301. {
  302.     wezel2 *p;
  303.     while(head!=0)
  304.     {
  305.         p = head->next;
  306.         delete head;
  307.         head = p;
  308.     }
  309.     cout<<"Usunieto liste dwukierunkowa"<<endl;
  310. }
  311.  
  312. int main(){
  313.     wezel *head = 0;
  314.     add(head, 5);
  315.     add(head, -2);
  316.     add(head, 7);
  317.     add(head, 3);
  318.     add(head, 1);
  319.     add(head, 10);
  320.     add(head, 8);
  321.     print(head);
  322.     search(head,-1);
  323.     usun(head, -2);
  324.     usun(head, 8);
  325.     print(head);
  326.     usun_liste(head);
  327.     print(head);
  328.     add(head, 3);
  329.     add(head, 1);
  330.     add(head, 10);
  331.     add(head, 8);
  332.     add(head, 5);
  333.     add(head, 8);
  334.     print(head);
  335.     poprzedni(head, 8);
  336.     cout<<endl<<endl<<endl;
  337.     //============
  338.  
  339.     wezel2 *head2 = 0;
  340.     add2(head2, 1);
  341.     add2(head2, 8);
  342.     add2(head2, 4);
  343.     add2(head2, -4);
  344.     add2(head2, 5);
  345.     add2(head2, 3);
  346.     add2(head2, 1);
  347.     print2(head2);
  348.     usun2(head2, 3);
  349.     print2(head2);
  350.     usun_liste2(head2);
  351.     print2(head2);
  352.     add2(head2, -4);
  353.     add2(head2, 5);
  354.     add2(head2, 5);
  355.     add2(head2, 4);
  356.     add2(head2, 3);
  357.     add2(head2, 1);
  358.     add2(head2, 1);
  359.     add2(head2, 1);
  360.     poprzedni2(head2, 5);
  361.     print2_from_end(head2);
  362.  
  363.  
  364.     print2(head2);
  365.     return 0;
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement