Question

In: Computer Science

Write a function that counts the number of times a given integer occurs in a Linked...

Write a function that counts the number of times a given integer occurs in a Linked List. What is the time complexity of your algorithm? Justify your answer in python

Solutions

Expert Solution

CODE:

#node structure
class Node(object):
   next=None
   def __init__(self,data):
       self.data=data

class Linked_List(object):
   def __init__(self):
       self.head=None

    #inserting node in the begning
   def push_beg(self,data):
       newNode=Node(data)
      
       #if head is null
       if self.head is None:
           self.head=newNode
       else:
            #if head is not null
           newNode.next=self.head
           self.head=newNode
      
   #getting of count of item
   def getCount(self,data):
        temp2=self.head
        count=0
        #iterate unless node is null
        while(temp2 is not None):
            #if item matches the item, increment count by 1
            if temp2.data==data:
                count+=1
            temp2=temp2.next
       #returns the total count
        return count  
      
list=Linked_List()

#inserting values in Linked_List
list.push_beg(4)
list.push_beg(5)
list.push_beg(3)
list.push_beg(4)
list.push_beg(1)

#input of item to be searched
n = int(input("Enter number to be searched and counted: "))
#calling method to be searched
print("Count of Item is ",list.getCount(n))

SCREENSHOT:

OUTPUT


Related Solutions

Make a python dictionary that counts the number of times each word occurs in the the...
Make a python dictionary that counts the number of times each word occurs in the the book below book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt'
Make a python dictionary that counts the number of times each word occurs in the the...
Make a python dictionary that counts the number of times each word occurs in the the book below book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt' without downloading to computer.
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is...
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is followed by a bit of the opposite parity (01 or 10) and writes the value to B. For example 00100110 has 4 cases: 00100110, 00100110, 00100110, 00100110.
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input...
//----------------------------------------------------------------- // Counts the number of odd, even, and zero digits in an integer // input value. Repeat as long as the user wishes to continue //----------------------------------------------------------------- public static void main(String[] args) {     // Declare the identifiers final int SENTINEL = -99;    // Declare the remaining identifiers ... Scanner scan = new Scanner(System.in);    // Display the programmer's information              // Input an integer number          // Count the number of odd, even, and...
C programming review excerises. 1. Write a function that counts the number of lines in a...
C programming review excerises. 1. Write a function that counts the number of lines in a file, using the following declaration: int countLines(char *filename) 2. Write a program that counts the number of words in a text file. Use the command-line arguments to read the name of the file. The syntax: countwords filename.txt 3. Write a program that counts the number of words starting with the first letter ‘T’ in a text file. Using commend line argument for the text...
Write a function in lisp XIT that counts the number of items in each sub-list of...
Write a function in lisp XIT that counts the number of items in each sub-list of a list and returns the count in a list. It should only work if there are two levels of brackets in the parameter (simple lists inside the parent list). Thus:             (XIT '((A B C)))        -> (3)             (XIT '((A) (A B) (A B C))) -> (1 2 3)             (XIT '((1 2)))              -> (2)             (XIT '(1 (2 3)))...
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Implement a function that returns the maximum number in a given unsorted linked list. For example,...
Implement a function that returns the maximum number in a given unsorted linked list. For example, there is a linked list 3->5->1->10->9. The printMax() function in max.c should return the maximum number in the linked list, namely 10 in the example. 1. Implement max.c with the completed printMax() function. 2. Provide an explanation for your solution #include <stdio.h> typedef struct node { int value; struct node *next; } node; int printMax(node *first) { // Your implementation return 0; } int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT