Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct list {
- int info;
- list* next;
- };
- // Схема 1
- void printList(list *p) {
- if (p != NULL) {
- cout << p->info << " ";
- printList(p->next);
- }
- }
- // Схема 2 [Сумма положительных элементов списка]
- int positiveSum(list* p) {
- if (p != NULL) {
- if (p->info > 0)
- return p->info + positiveSum(p->next);
- else
- return positiveSum(p->next);
- }
- return 0;
- }
- void main() {
- list* beg = new(list);
- int x;
- cin >> x;
- beg->info = x; beg->next = NULL;
- list* p = beg;
- cin >> x;
- while (x != 0) {
- list* r = new(list);
- r->info = x; r->next = NULL;
- p->next = r;
- p = r;
- cin >> x;
- }
- printList(beg); cout << endl;
- cout << "s: " << positiveSum(beg) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement