Facebook
From ja, 4 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 280
  1.  #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node{
  6. int val;
  7. node* next;
  8. };
  9.  
  10. void Add(node*&H, int x)
  11. {
  12.   node* p = new node;
  13.   p->val=x;
  14.   p->next=H;
  15.   H=p;
  16. }
  17. void del(node*&H)
  18. {
  19.   if(H!=NULL)
  20.   {
  21.     node*p = H;
  22.     H=H->next;
  23.     delete p;
  24.   }
  25. }
  26. void show(node*H)
  27. {
  28.   node*p=H;
  29.   while(p!=NULL)
  30.   {
  31.     cout<<p->val<<"->";
  32.     p=p->next;
  33.   }
  34.   cout<<"NULL"<<endl;
  35. }
  36.  
  37. int findmin(node*H)
  38. {
  39.   if(H!=NULL)
  40.   {
  41.   int min = 0;
  42.   node *p = H;
  43.   while(p!=NULL)
  44.   {
  45.     if(p->val <min)
  46.     {
  47.       min=p->val;
  48.     }
  49.     p=p->next;
  50.   }
  51.  
  52.   }
  53. }
  54. void bubblesort(node *&H) {
  55.     node *p = H;
  56.     node *wstaw = NULL;
  57.     int counter = 0;
  58.  
  59.     while (p) {
  60.         counter++;
  61.         p=p->next;
  62.     }
  63.  
  64.  
  65.     Add(H, findmin(H) - 1);
  66.     p=H;
  67.     for (int i = 0; i < counter; i++) {
  68.         for (int j = 0; j < counter - 1; j++) {
  69.             if (p->next->val > p->next->next->val) {
  70.                 wstaw = p->next;
  71.                 p->next = wstaw->next;
  72.                 wstaw->next = p->next->next;
  73.                 p->next->next = wstaw;
  74.             }
  75.             p = p->next;
  76.         }
  77.         p = H;
  78.     }
  79.     del(H);
  80. show(H);
  81.  
  82. }
  83.  
  84. void insert(node *& H, int x)//wymaga aby lista byla posortowana
  85. {
  86.     node * pom = new node;//pomocniczy wskaźnik do skakania
  87.     if (H == NULL)//jesli pusta lista to standardowo wstawiamy
  88.     {
  89.         Add(H, x);
  90.     }
  91.     else if (H->val > x)//jesli jest najmniejszy to także standardowo wstawiamy
  92.     {
  93.         Add(H, x);
  94.     }
  95.     else
  96.     {
  97.         pom = H;
  98.         while (pom->next)
  99.         {
  100.             if (pom->next->val > x)// wstawianie jesli w środku
  101.             {
  102.                 node * p = new node;
  103.                 node * pomocniczy = pom->next;
  104.                 p->val = x;
  105.                 pom->next = p;
  106.                 p->next = pomocniczy;
  107.                 return;
  108.             }
  109.             pom = pom->next;
  110.         }
  111.         //wstawianie jesli wskaznik idzie na koniec
  112.         node * p = new node;
  113.         node * pomocniczy = pom->next;
  114.         p->val = x;
  115.         pom->next = p;
  116.         p->next = pomocniczy;
  117.  
  118.  
  119.  
  120.     }
  121. del(H);
  122. show(H);
  123. }
  124.  
  125. int main()
  126. {
  127.     node* H = NULL;
  128.   Add(H, 5);
  129.   Add(H, 2);
  130.    Add(H, 5);
  131.   Add(H, 9);
  132.    Add(H, 2);
  133.   Add(H, 6);
  134.    Add(H, 5);
  135.   Add(H, 3);
  136.    Add(H, 5);
  137.   Add(H, 7);
  138.    Add(H, 2);
  139.   Add(H, 1);
  140.  bubblesort(H);
  141.   cout<<endl;
  142.  
  143.   insert(H,0);
  144.  
  145.     return 0;
  146. }
  147.  

Replies to proszeniekrzyczana rss

Title Name Language When
liczenie ile jest w liscie elementow ja cpp 4 Years ago.