Question

Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...

Write a program that can:

1. Insert twenty random numbers into a linked list. The numbers should be within a range (E.g., 1 to 7). The user should be prompted to enter the minimum number and maximum number of the range. Each number should be inserted at the end of the list. Section 7.8 of the textbook covers the random number generator. Examples of how to use the random number generator are in Fig 7.6 and 7.7. Here is a simple example:

Random generator = new Random();

int min = 1; int max = 7;

for(int i=1; i<=5; i++) Console.Write(generator.Next(min, max)+” ”);

2. Print all the number in the list.

3. Count the number of occurrences of a given number in the list using recursion. The recursive method must work using the list and not any other data structure such as an array. The user should be prompted to enter the number to count.

4. Find the position (index) of a given number in the list using recursion. The recursive method must work using the list and not any other data structure such as an array. The user should be prompted to enter the number to find.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkedList
{
  
class LinkList
{
public class Node
{
public int data;
public Node next;
public Node(int d)
{
data = d;
next = null;
}

}
private Node head;
public LinkList()
{
head=null;
}
public void insertEnd(int new_data)
{
Node new_node = new Node(new_data);
if (head == null)
{
head = new_node;
return;
}
Node temp=head;
while (temp.next!= null)
{
temp = temp.next;
}
temp.next = new_node;
}
public void print()
{
Node temp = head;
while (temp .next!= null)
{
Console.Write(temp.data + "->");
temp = temp.next;
}
Console.WriteLine(temp.data);
}
public void countOccurnace(int num)
{
Console.WriteLine("\nThe number of occurance of " + num + " in the list is: " + count(head, num));
}
public static int count(Node node, int num)
{
if (node == null)
return 0;
if (node.data == num)
return 1 + count(node.next, num);
else
return count(node.next, num);
}
public void findIndex(int num)
{
if (find(head, num) < 0)
Console.WriteLine("The " + num + " is not in the list");
else
Console.WriteLine("The index " + num + " in the list is: "+(find(head,num)+1));
}
public static int find(Node node, int num)
{
if (node.data == num)
{
return 0;
}
if (node.next == null)
{
return -1;
}
int index = find(node.next,num);
if (index == -1)
{
return -1;
}
else
{
return 1 + index;
}
}
}
class Program
{
static void Main(string[] args)
{
LinkList l1 = new LinkList();
Random generator = new Random();
int min ,max;
Console.Write("Enter minimum range of the random number: ");
min=Convert.ToInt16(Console.ReadLine());
Console.Write("Enter maximum range of the random number: ");
max=Convert.ToInt16(Console.ReadLine());
for (int i = 1; i <= 20; i++)
{
l1.insertEnd(generator.Next(min, max));
}
Console.WriteLine("\n\nList is :");
l1.print();
Console.Write("\n\nEnter number to count its occurance: ");
int number = Convert.ToInt16(Console.ReadLine());
l1.countOccurnace(number);
Console.Write("\n\nEnter number to find its index in list: ");
number = Convert.ToInt16(Console.ReadLine());
l1.findIndex(number);
}
}
}
output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Write a program that can: 1. Insert twenty random numbers into a linked list. The numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • ​Declare an array with 1000 elements of type int Then in a loop generate 1000 random numbers and assign one to each element in the array The random numbers should be between 1 and 50 Then, prompt the user to enter a number, store this number in a local

    Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • Javascript Write a program to generate random numbers between a user-specified range, then count and display...

    Javascript Write a program to generate random numbers between a user-specified range, then count and display the frequencies of the most / least appeared numbers. Sample Output How many random numbers? 1000000 Enter the minimum number: 100 Enter the maximum number: 200 Generated 1000000 random numbers between 100 (inclusive) and 200 (exclusive). Number 133 has the most occurrences (10197). Number 154 has the least occurrences (9749).

  • In the "Go" button add 15 random numbers in the array using a random number generator....

    In the "Go" button add 15 random numbers in the array using a random number generator. Remove the call to "SetToZero" and the code following. Remove the "SetToZero" method. Add Access Keys of your choice Assign an accept button Assign cancel button (Exit) Add a Button "Find Max" Write an appropriately named method that Accepts the array Finds the largest int and returns the value "Find Max" button click Calls above method Opens a MessageBox to display the returned value...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a...

    In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a list of integers into a vector with base type int. Provide the facility to read this vector from an input file. Make sure to ask the user for a filename. The output is a two-column list. The first column is a list of the distinct vector elements. The second column is the count of the number of occurrences for each element. The list should...

  • Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.r...

    Please write a JAVA program according to following requirement. Thanks Part 2 -Arrays We can use the Math.random method to generate random integers. For example, Math.random () generates a random integer greater than or equal to 0 and less than 1. The expression Math. random) 6generates random numbers between 0 and 5, simulating the throw of a die. In this lab assignment, you will use an array to test whether the random generator is fair; that is, whether each possible...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT