Advertisement
icatalin

ASD Liste

Jan 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int info;
  8.     Node* next;
  9. };
  10.  
  11. class Lista
  12. {
  13. public:
  14.     Node* head;
  15.     int counter = 0;
  16.  
  17.     Lista()
  18.     {
  19.         head = NULL;
  20.     }
  21.  
  22.     void printList();
  23.     void insert(int val, int pos);
  24.     void deleteByPos(int pos);
  25.     void reverse();
  26.     void deleteList();
  27.  
  28. };
  29.  
  30.  
  31. void Lista::printList()
  32. {
  33.     for (Node* p = head; p != NULL; p = p->next)
  34.         {
  35.             cout << p->info;
  36.  
  37.             if (p->next == NULL)
  38.                 cout<<". ";
  39.             else
  40.                 cout<<", ";
  41.         }
  42.  
  43.     cout<<"\n";
  44.  
  45. }
  46.  
  47. void Lista::insert(int val, int pos)
  48. {
  49.     Node *p = new Node;
  50.     p->info = val;
  51.     p->next = NULL;
  52.     counter++;
  53.  
  54.     if (head == NULL) // inserare lista goala
  55.     {
  56.         head = p;
  57.         return;
  58.     }
  59.  
  60.     if (pos == 0) //inserare la inceput
  61.     {
  62.         p -> next = head;
  63.         head = p;
  64.         return;
  65.     }
  66.  
  67.     int i = 0; //inserare dupa o anumita pozitie
  68.     Node* temp;
  69.     temp = head;
  70.  
  71.     while(temp->next != NULL && i != pos - 1) //parcurgem lista pana pe pozitia pos - 1
  72.     {
  73.         temp = temp -> next;
  74.         i++;
  75.     }
  76.  
  77.     p->next = temp->next;
  78.     temp->next = p;
  79.  
  80. }
  81.  
  82. void Lista::deleteByPos(int pos)
  83. {
  84.     counter--;
  85.  
  86.     if (head == NULL) //daca lista e goala
  87.         return;
  88.  
  89.     if (pos == 0) //daca stergem primul element
  90.         {
  91.             Node* p = head;
  92.             delete p;
  93.  
  94.             head = head -> next;
  95.             return;
  96.         }
  97.  
  98.     int i = 0; //stergere la o anumita pozitie
  99.     Node* temp;
  100.     temp = head;
  101.  
  102.     while(temp->next != NULL && i != pos - 1) //parcurgem lista pana pe pozitia pos - 1
  103.     {
  104.         temp = temp -> next;
  105.         i++;
  106.     }
  107.  
  108.     Node* p;
  109.     p = temp->next;
  110.     delete p;
  111.  
  112.     if(temp->next->next != NULL)
  113.         temp->next = temp->next->next;
  114.     else
  115.         temp->next = NULL;
  116.  
  117. }
  118.  
  119. void Lista::reverse()
  120. {
  121.     Node *current = head, *prev = NULL, *urm = NULL;
  122.  
  123.     while (current != NULL)
  124.     {
  125.         urm = current->next;
  126.  
  127.         current->next = prev;
  128.  
  129.         prev = current;
  130.         current = urm;
  131.     }
  132.  
  133.     head = prev;
  134. }
  135.  
  136. void Lista::deleteList()
  137. {
  138.     Node *p;
  139.     p = head;
  140.  
  141.     while (head != NULL)
  142.     {
  143.         head = head->next;
  144.         delete p;
  145.  
  146.         p = head;
  147.  
  148.         counter--;
  149.     }
  150. }
  151.  
  152. int main()
  153. {
  154.     Lista l;
  155.  
  156.     l.insert(10,0);
  157.     l.insert(20,0);
  158.     l.insert(15,1);
  159.     l.printList();
  160.     l.deleteList();
  161.     l.printList();
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement