-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy path03 Practice.cpp
101 lines (86 loc) · 1.32 KB
/
03 Practice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node* next;
}*first;
void Create(int A[], int no)
{
int i;
struct Node* t, * last;
first = new Node;
first->data = A[0];
first->next = NULL;
last = first;
for (i = 1; i < no; i++)
{
t = new Node;
t->data = A[i];
t->next = NULL;
last->next = t;
last = t;
}
}
void Display(struct Node* p)
{
while (p != NULL)
{
cout << p->data << "->";
p = p->next;
}
}
void RDisplay(struct Node* p)
{
if (p != NULL)
{
cout << p->data << "->";
RDisplay(p->next); // for reverse call 1st then print
}
}
int Count(struct Node* p)
{
int c=0;
while (p != NULL)
{
c++;
p = p->next;
}
return c;
}
int Rcount(struct Node* p)
{
if (p != NULL)
return Rcount(p->next) + 1;
else
return 0;
}
int Sum(struct Node* p)
{
int sum = 0;
while (p!=0)
{
sum += p->data;
p = p->next;
}
return sum;
}
int Rsum(struct Node* p)
{
if (p == 0)
return 0;
return Rsum(p->next) + p->data;
}
int main()
{
int A[] = { 1,2,3,4,5 };
int no = sizeof(A) / sizeof(int);
Create(A, no);
RDisplay(first);
cout << endl;
cout << "Total terms in the LL is " << Count(first)<<endl;
cout << "Total sum of the LL is " << Sum(first)<<endl;
cout << "Revursive count " << Rcount(first)<<endl;
cout << "Recursive sum is " << Rsum(first) << endl;
return 0;
}