This is the prompt: When you create the linked list, insert each new account at
ID: 3801613 • Letter: T
Question
This is the prompt:
When you create the linked list, insert each new account at the appropriate location so that the linked list is sorted in the ascending order according to the account number. Display the original order when each new account is created and then list the whole linked list at the end according to the sorted order.
I have the linked list but I do not know how to display them again in a sort fashion.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment4
{
public class Acct
{
public int ANum { get; set; }
public double Bal { get; set; }
public Acct Next { get; set; }
public Acct() { }
public Acct(int cname, int anum, double bal)
{
ANum = anum; Bal = bal; Next = null;
}
public string ShowAcct()
{
return (ANum + " " + " " + "$" + Bal);
}
}
class Program
{
static void Main(string[] args)
{
Acct head = new Acct(0, 0, 0.0);
Acct current;
Random rnd = new Random();
int custName;
int acctNum;
double acctBal;
double total = 0;
double average = 0;
double lessThan = 0;
double moreThan = 0;
for (int i = 1; i <= 20; i++)
{
custName = rnd.Next();
acctNum = rnd.Next(10000, 49999);
if (acctNum > 49999) acctNum += 2000;
acctBal = rnd.Next(100, 4999999) / 100.00;
current = new Acct(custName, acctNum, acctBal);
current.Next = head.Next;
head.Next = current;
if (acctBal < 1000) lessThan++;
else if (acctBal > 10000) moreThan++;
total += acctBal;
average = total / i;
}
Console.WriteLine("{0,1} {1,1}", "Account number", "Balance amount");
current = head.Next;
while (current != null)
{
Console.WriteLine(current.ShowAcct());
current = current.Next;
}
Console.WriteLine(" The number of accounts with less than $1000 are: " + lessThan);
Console.WriteLine("The number of accounts with more than $10000 are: " + moreThan);
Console.WriteLine("The total amount is $" + total);
Console.WriteLine("The average amount is $" + average);
Console.ReadLine();
}
}
}
Explanation / Answer
Answer
In order to display the linked list in a sorted manner, you'll need to sort the linked list first.
You can use any of the below methods to sort a linked list:
i. bubble sort
ii. selection sort
iii. insertion sort
iv. merge sort
v quick sort
After using any of the above sorting techniques, display the linked list normally, i.e. like you normally display a linked list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.