Facebook
From Cream Lechwe, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 473
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. struct ele {
  7. int dana;
  8. ele * nast;
  9. ele * poprz;
  10. };
  11.  
  12.  
  13. void insert(ele* &head, ele* &tail, int x){
  14.        
  15.         ele* nowy = new ele {x,NULL, NULL};
  16.         cout << "Dodalem: " << x << endl;
  17.         if(head==NULL){
  18.                 head = nowy;
  19.                 tail = nowy;
  20.                 return;
  21.         }
  22.         nowy->poprz = tail;
  23.         tail->nast = nowy;
  24.         tail = nowy;
  25. }
  26.  
  27. void removeFirst(ele* &head, ele* &tail){
  28.         if(head == NULL){
  29.                 cout << "Lista pusta" << endl;
  30.                 return;
  31.         }
  32.  
  33.                 else{
  34.                 ele* temp = head;
  35.                 head = head->nast;
  36.                 cout << "Usunalem: " << temp->dana << endl;
  37.                 delete temp;
  38.                 temp = NULL;
  39.                 return;
  40.         }
  41. }
  42.  
  43. void remove(ele* &head, ele* &tail, int i){
  44.        
  45.         ele* temp = head;
  46.        
  47.         if(head == NULL){
  48.                 return;
  49.         }
  50.         if(i == 0){
  51.                 head = head->nast;
  52.                 delete temp;
  53.                 temp = NULL;
  54.                 return;
  55.         }
  56.         else{
  57.                 int j = 0;
  58.                 while (j!=i){
  59.                         j++;
  60.                         temp=temp->nast;
  61.                 }
  62.                 temp->poprz->nast =temp->nast;
  63.                 temp->nast->poprz = temp->poprz;
  64.                 delete temp;
  65.                 temp = NULL;
  66.                 return;
  67.         }
  68. }
  69.  
  70. void display(ele* &head, ele* &tail){
  71.        
  72.         if(head == NULL){
  73.                 cout << "Lista pusta";
  74.         }
  75.        
  76.         ele* temp = head;
  77.  
  78.         while(temp){
  79.                 cout << temp->dana << " ";
  80.                 temp=temp->nast;
  81.         }
  82. }
  83.  
  84.  
  85.  
  86. int main(int argc, char** argv) {
  87.        
  88.         ele* head = NULL;
  89.         ele* tail = NULL;
  90.        
  91.         insert(head, tail, 5);
  92.         insert(head, tail, 10);
  93.         insert(head, tail, 15);
  94.         insert(head, tail, 20);
  95.         remove(head, tail, 2);
  96.         remove(head, tail, 0);
  97.        
  98.         cout << "Lista: ";
  99.         display(head, tail);
  100.  
  101.        
  102.         return 0;
  103. }