Advertisement
icatalin

Liste ASD

Jan 1st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 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. void printList (node *n)
  12. {
  13.     cout<<" Lista este urmatoarea: \n";
  14.  
  15.     while(n != NULL)
  16.     {
  17.         cout<<" "<<n->info;
  18.         n = n->next;
  19.     }
  20.  
  21.     cout<<"\n\n";
  22. }
  23.  
  24. void insertFirst(node** head, int new_info) // head are ** pt ca va fi modicat si in lista initiala
  25. {   //adaugare la inceptul listei
  26.     node* new_node = new node;
  27.     new_node->info = new_info;
  28.     new_node->next = (*head);
  29.     (*head) = new_node; //mutam capul listei pe noul nod
  30.  
  31. }
  32.  
  33. void insertAfter(node* prev_node, int new_info)
  34. {   //inserare dupa un anumit nod
  35.  
  36.     if (prev_node == NULL)
  37.     {
  38.         cout<<"Nodul dupa care inseram nu poate fi nul.\n\n";
  39.         return;
  40.     }
  41.  
  42.     node* new_node = new node;
  43.     new_node->info = new_info;
  44.  
  45.     new_node->next = prev_node->next;
  46.     prev_node->next = new_node;
  47. }
  48.  
  49. void insertLast(node** head, int new_info)
  50. {
  51.     node* new_node = new node;
  52.     new_node->info = new_info;
  53.     new_node->next = NULL;
  54.  
  55.     node* last = *head;
  56.  
  57.    if (*head == NULL) //daca lista e goala
  58.    {
  59.        *head = new_node;
  60.        return;
  61.    }
  62.  
  63.    while (last->next != NULL) //mergem pe ultimul nod
  64.         last = last->next;
  65.  
  66.     last->next = new_node;
  67.     return;
  68. }
  69.  
  70.  
  71. void deleteNodeByValue(node** head, int del_info)
  72. {   //stergem un nod dupa valoarea lui
  73.     node* temp = *head, *prev;
  74.  
  75.     if (temp != NULL && temp->info == del_info)
  76.     {   //in cazul in care nodul pe care vrem sa-l stergem e chiar capul
  77.         *head = temp->next; // mutam capul
  78.         delete head;   //eliberam spatiu
  79.         return;
  80.     }
  81.  
  82.     while (temp != NULL && temp->info != del_info)
  83.     {   //cautam elementul care trb sters si il retinem pe anteriorul
  84.         prev = temp;
  85.         temp = temp->next;
  86.     }
  87.  
  88.     if (temp == NULL)
  89.     {
  90.         cout<<" Elementul cautat nu a fost gasit in lista.\n\n";
  91.         return;
  92.     }
  93.  
  94.     prev->next = temp->next; //Scoatem elementul cautat din lista.
  95.     delete temp; cout<<" Elementul "<<del_info<<" a fost sters din lista.\n\n";
  96. }
  97.  
  98. void deleteNodeByPosition(node** head, int pos)
  99. {
  100.     if (*head == NULL) //daca lista e nula
  101.         return;
  102.  
  103.     node* temp = *head;
  104.  
  105.     if (pos == 0) //daca stergem chiar capul
  106.     {
  107.         *head = temp->next;
  108.         delete(head);
  109.         return;
  110.     }
  111.  
  112.     for (int i = 0; temp != NULL && i < pos- 1; i++)
  113.         temp = temp->next;
  114.  
  115.     if (temp == NULL || temp->next == NULL) //daca pozitia e mai mare decat numarul de noduri
  116.         return;
  117.  
  118.     node* next = temp->next->next; // nodul temp->next este cel care trebuie sters
  119.     delete temp->next;
  120.     temp->next = next; // scoatem nodul pe care l-am sters
  121. }
  122.  
  123. void deleteList(node** head)
  124. {
  125.     node* current = *head;
  126.     node* next;
  127.  
  128.     while (current != NULL)
  129.     {
  130.         next = current->next;
  131.         delete current;
  132.         current = next;
  133.     }
  134.  
  135.     *head = NULL;
  136. }
  137.  
  138. bool findElement(node* head, int find_info)
  139. {
  140.     node* current = head;
  141.  
  142.     while (current->next != NULL && current->info != find_info)
  143.         current = current->next;
  144.  
  145.     if (current == NULL) return false;
  146.     return true;
  147. }
  148.  
  149. int main()
  150. {
  151.     node* head = NULL; //initializam o lista nula
  152.  
  153.     insertFirst(&head,3);
  154.  
  155.     insertAfter(head,4);
  156.  
  157.     insertLast(&head,5);
  158.  
  159.     insertAfter(head->next,7);
  160.  
  161.     insertFirst(&head,9);
  162.  
  163.  
  164.     printList(head);
  165.  
  166.     deleteList(&head);
  167.  
  168.     printList(head);
  169.  
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement