Advertisement
icatalin

ASD Coada (Stiva) cu clasa bazata pe lista

Jan 17th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 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.     int getElement(int pos);
  28.  
  29. };
  30.  
  31.  
  32. void Lista::printList()
  33. {
  34.     for (Node* p = head; p != NULL; p = p->next)
  35.         {
  36.             cout << p->info;
  37.  
  38.             if (p->next == NULL)
  39.                 cout<<". ";
  40.             else
  41.                 cout<<", ";
  42.         }
  43.  
  44.     cout<<"\n";
  45.  
  46. }
  47.  
  48. int Lista::getElement(int pos)
  49. {
  50.     Node *temp = head;
  51.     int i = 0;
  52.     while (temp->next != NULL && i != pos - 1)
  53.         {
  54.             temp = temp->next;
  55.             i++;
  56.         }
  57.  
  58.     if (i == pos - 1)
  59.         return temp->info;
  60.     else
  61.         return -1; // pozitia nu exista in lista
  62. }
  63.  
  64.  
  65. void Lista::insert(int val, int pos)
  66. {
  67.     Node *p = new Node;
  68.     p->info = val;
  69.     p->next = NULL;
  70.     counter++;
  71.  
  72.     if (head == NULL) // inserare lista goala
  73.     {
  74.         head = p;
  75.         return;
  76.     }
  77.  
  78.     if (pos == 0) //inserare la inceput
  79.     {
  80.         p -> next = head;
  81.         head = p;
  82.         return;
  83.     }
  84.  
  85.     int i = 0; //inserare dupa o anumita pozitie
  86.     Node* temp;
  87.     temp = head;
  88.  
  89.     while(temp->next != NULL && i != pos - 1) //parcurgem lista pana pe pozitia pos - 1
  90.     {
  91.         temp = temp -> next;
  92.         i++;
  93.     }
  94.  
  95.     p->next = temp->next;
  96.     temp->next = p;
  97.  
  98. }
  99.  
  100. void Lista::deleteByPos(int pos)
  101. {
  102.     counter--;
  103.  
  104.     if (head == NULL) //daca lista e goala
  105.         return;
  106.  
  107.     if (pos == 0) //daca stergem primul element
  108.         {
  109.             Node* p = head;
  110.             delete p;
  111.  
  112.             head = head -> next;
  113.             return;
  114.         }
  115.  
  116.     int i = 0; //stergere la o anumita pozitie
  117.     Node* temp;
  118.     temp = head;
  119.  
  120.     while(temp->next != NULL && i != pos - 1) //parcurgem lista pana pe pozitia pos - 1
  121.     {
  122.         temp = temp -> next;
  123.         i++;
  124.     }
  125.  
  126.     Node* p;
  127.     p = temp->next;
  128.     delete p;
  129.  
  130.     if(temp->next->next != NULL)
  131.         temp->next = temp->next->next;
  132.     else
  133.         temp->next = NULL;
  134.  
  135. }
  136.  
  137. void Lista::reverse()
  138. {
  139.     Node *current = head, *prev = NULL, *urm = NULL;
  140.  
  141.     while (current != NULL)
  142.     {
  143.         urm = current->next;
  144.  
  145.         current->next = prev;
  146.  
  147.         prev = current;
  148.         current = urm;
  149.     }
  150.  
  151.     head = prev;
  152. }
  153.  
  154. void Lista::deleteList()
  155. {
  156.     Node *p;
  157.     p = head;
  158.  
  159.     while (head != NULL)
  160.     {
  161.         head = head->next;
  162.         delete p;
  163.  
  164.         p = head;
  165.  
  166.         counter--;
  167.     }
  168. }
  169.  
  170. class Coada
  171. {
  172.     Lista l;
  173. public:
  174.     void push(int x);
  175.     void pop();
  176.     int size();
  177.     int top();
  178.  
  179.     void printCoada();
  180.     void deleteCoada();
  181. };
  182.  
  183. void Coada::printCoada()
  184. {
  185.     l.printList();
  186. }
  187.  
  188. void Coada::push(int x)
  189. {
  190.     l.insert(x,l.counter);
  191. }
  192.  
  193. void Coada::pop()
  194. {
  195.     l.deleteByPos(0);
  196. }
  197.  
  198. int Coada::size()
  199. {
  200.     return l.counter;
  201. }
  202.  
  203. int Coada::top()
  204. {
  205.     return l.getElement(l.counter);
  206. }
  207.  
  208. void Coada::deleteCoada()
  209. {
  210.     l.deleteList();
  211.     return;
  212. }
  213.  
  214. int main()
  215. {
  216.     Coada c;
  217.     c.push(1);
  218.     c.push(5);
  219.     c.push(10);
  220.     c.push(15);
  221.     c.pop();
  222.     c.printCoada();
  223.     cout<<c.top();
  224.  
  225.     return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement