-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideo_5_Sum_Count_Avg.py
More file actions
56 lines (50 loc) · 1.41 KB
/
Video_5_Sum_Count_Avg.py
File metadata and controls
56 lines (50 loc) · 1.41 KB
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
#Find Sum, Count, Average of a Linked List
class Node:
def __init__(self,data):
self.info=data
self.next=None
class LinkedList:
def __init__(self):
self.head=None
def insbeg(self,ele):
current=Node(ele)
if self.head is None:
self.head=current
else:
current.next=self.head
self.head=current
def display(self):
if self.head is None:
print("Empty Linked List")
return
else:
current=self.head
while current:
print(current.info," -> ", end="")
current=current.next
print("None")
#to count, sum and average the nodes
def countSumAvgNodes(self):
if self.head is None:
print("Empty Linked List")
return
else:
count=0
sum=0
current=self.head
while current:
count+=1
sum=sum+current.info
current=current.next
print("Sum of all Nodes are ",sum)
print("Total Nodes are ",count)
print("Average ",sum/count)
mylist=LinkedList()
n=int(input("How many elements you want to insert"))
for i in range(n):
ele=int(input("Enter element"))
mylist.insbeg(ele)
print("My linked list")
mylist.display()
print("Even elements are ")
mylist.displayEven()