Linked List - Count -> i-Practice -> Lab Programs -> Data Structures Laboratory Using Python

Linked List - Count

Write a program to count the number of nodes in the Linked List.

Define the class Node with the following data members:
data - to store data
next - to store the next node

Define class LinkedList with following data member:
head - to store head node

Include functions

  • append --- to add data at the end of the linked list.

  • display --- to display all the data in the linked list.

  • count --- to return the number of data elements in the linked list.

Refer the function specifications for further details.

[Note: The statement. 'Elements in the linked list are' should be in the main function.

Input and Output Format:

Refer sample input and output for formatting specifications.

Sample Input and Output:

[All text in bold corresponds to input and the rest corresponds to output.]

Enter the value

10

Do you want to add another node? Type Yes/No

Yes

Enter the value

17

Do you want to add another node? Type Yes/No

Yes

Enter the value

11

Do you want to add another node? Type Yes/No

Yes

Enter the value

28

Do you want to add another node? Type Yes/No

No

The elements in the linked list are 10 17 11 28

The number of elements in the linked list is 4

Main.py


import LinkedList as LL

ll = LL.LinkedList()
choice="Yes"
while choice=="Yes":
    data = int(input("Enter the value\n"))
    ll.append(data)
    choice = input("Do you want to add another node? Type Yes/No\n")

print("The elements in the linked list are ",end="")
ll.display()
print("\nThe number of elements in the linked list is ",end="")
ll.count()



Node.py


class Node:
  def __init__(self,data):
      self.data=data;
      self.next=None;  

LinkedList.py


from Node import Node
class LinkedList:
    def __init__(self):
        self.head=None
        self.tail=None
    def append(self,data):
        newNode=Node(data)
        if(self.head==None):
            self.head=newNode
            self.tail=newNode
        else:
            self.tail.next=newNode
            self.tail=newNode
    def count(self):
        count=0
        current=self.head
        while(current!=None):
            count=count+1
            current=current.next
        print(count)
    def display(self):
        current=self.head
        if(self.head==None):
            print("List is empty")
            return
        while(current!=None):
            print(current.data)
            current=current.next            
L=LinkedList()

Comments

Post a Comment

Popular posts from this blog

You are secretary of Gymkhana Club, Madurai.Write a notice in not more than 50 words informing the members to attend an extraordinary meeting of the governing body. Include details like date, time, venue, etc. Sign as Prabhu/ Prabha. - Notice

On the occasion of National Science and Technology Day, the school has decided to organise a Science Fair. Vikram, the secretary of the Science Society, wants to call a meeting of the office bearers of the society to discuss the arrangements for the fair. Write a notice in not more than 50 words. - Notice

The Address Summary - by Marga Minco