Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Да се разработи функция, която намира броя на елементите на линеен
- едносвързан списък от цели числа, имащи зададена стойност (да се
- предава като параметър).
- */
- #include <iostream>
- struct Node
- {
- int data;
- struct Node* next;
- } *first = NULL, *last = NULL;
- using namespace std;
- void addNumbers(int x)
- {
- if(first == NULL)
- {
- first = new Node;
- first->data = x;
- first->next = NULL;
- last = first;
- }
- else
- {
- Node *t = new Node;
- t->data = x;
- t->next = NULL;
- last->next = t;
- last = t;
- }
- }
- void displayList(struct Node *p)
- {
- while(p)
- {
- cout << p->data << " ";
- p = p->next;
- }
- }
- void countNumber(struct Node *p)
- {
- int numberOfElements = 0;
- while(p)
- {
- p = p->next;
- numberOfElements++;
- }
- cout << "The number of elements in the list is: " << numberOfElements << endl;
- }
- int countSpecificNumbers(struct Node *p, int specifiedNumber)
- {
- int numberOfElements = 0;
- while(p)
- {
- if(p->data == specifiedNumber)
- numberOfElements++;
- p = p->next;
- }
- cout << "The number of elements with value "<< specifiedNumber << " in the list is: " << numberOfElements << endl;
- return specifiedNumber;
- }
- int main()
- {
- addNumbers(1);
- addNumbers(2);
- addNumbers(2);
- addNumbers(4);
- displayList(first);
- countNumber(first);
- cout << countSpecificNumbers(first,2) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement