193030

SAA VII. Zadacha 1

Oct 27th, 2020
708
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Задача VII.1:
  3.     Да се разработят процедура и функция, които намират броя на елементите
  4.     на линеен едносвързан списък от цели числа.
  5. */
  6.  
  7. #include <iostream>
  8.  
  9. struct Node
  10. {
  11.     int data;
  12.     struct Node* next;
  13. } *first = NULL, *last = NULL;
  14. using namespace std;
  15.  
  16. void addNumbers(int x)
  17. {
  18.     if(first == NULL)
  19.     {
  20.         first = new  Node;
  21.         first->data = x;
  22.         first->next = NULL;
  23.         last = first;
  24.     }
  25.     else
  26.     {
  27.         Node *t = new Node;
  28.         t->data = x;
  29.         t->next = NULL;
  30.         last->next = t;
  31.         last = t;
  32.     }
  33. }
  34.  
  35. void displayList(struct Node *p)
  36. {
  37.     while(p)
  38.     {
  39.         cout << p->data << " ";
  40.         p = p->next;
  41.     }
  42. }
  43. void countNumber(struct Node *p)
  44. {
  45.     int numberOfElements = 0;
  46.     while(p)
  47.     {
  48.         p = p->next;
  49.         numberOfElements++;
  50.     }
  51.     cout << "The number of elements in the list is: " << numberOfElements << endl;
  52. }
  53.  
  54.  
  55. int main()
  56. {
  57.     addNumbers(1);
  58.     addNumbers(2);
  59.     addNumbers(3);
  60.     addNumbers(4);
  61.     displayList(first);
  62.     countNumber(first);
  63.     displayListReverse(first);
  64. }
  65.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×