Created
January 29, 2024 04:18
-
-
Save primaryobjects/d96607f664dea1ffc3522cd7bdf312f6 to your computer and use it in GitHub Desktop.
Example of reversing a linked list in ASP .NET MVC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using Microsoft.AspNetCore.Mvc; | |
using ReverseLinkedListApp.Models; | |
namespace ReverseLinkedListApp.Controllers; | |
public class HomeController : Controller | |
{ | |
private readonly ILogger<HomeController> _logger; | |
public HomeController(ILogger<HomeController> logger) | |
{ | |
_logger = logger; | |
} | |
[HttpGet] | |
public IActionResult Index() | |
{ | |
return View(); | |
} | |
[HttpPost] | |
public IActionResult Index(NumbersModel model) | |
{ | |
if (model.Numbers != null) | |
{ | |
var numbers = model.Numbers.Split(","); | |
try | |
{ | |
var intNumbers = Array.ConvertAll(numbers, int.Parse); | |
//Array.Reverse(intNumbers); | |
intNumbers = ReverseNumbers(intNumbers).ToArray(); | |
var data = new { numbers = intNumbers }; | |
return Json(data); | |
} | |
catch | |
{ | |
return BadRequest(new { message = "Error: input must be a comma-separated list of integers." }); | |
} | |
} | |
else | |
{ | |
return BadRequest(new { message = "Error: input must be a comma-separated list of integers." }); | |
} | |
} | |
public IActionResult Privacy() | |
{ | |
return View(); | |
} | |
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | |
public IActionResult Error() | |
{ | |
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | |
} | |
private static IEnumerable<int> ReverseNumbers(int[] numbers) | |
{ | |
Node<int> head = new(numbers.First()); | |
Node<int> previous = head; | |
for (int i=1; i<numbers.Length; i++) | |
{ | |
int number = numbers[i]; | |
Node<int> node = new(number); | |
previous.Next = node; | |
previous = node; | |
} | |
return head.Reverse().ToList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model NumbersModel | |
@{ | |
ViewData["Title"] = "Home Page"; | |
} | |
<div class="text-center"> | |
<h1 class="display-4">Reverse a Linked List</h1> | |
<h3 class="mt-5">Enter Comma-Separated Numbers</h1> | |
@using (Html.BeginForm("Index", "Home", FormMethod.Post)) | |
{ | |
<div class="mb-3"> | |
@Html.LabelFor(model => model.Numbers) | |
@Html.TextBoxFor(model => model.Numbers, new { @required = "required" }) | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
} | |
<hr> | |
<div class="collapse-icon" data-bs-toggle="collapse" data-bs-target="#output-container" title="Toggle output."> | |
<i class="bi bi-chevron-down"></i> | |
</div> | |
<div id="output-container" class="collapse show"> | |
<div id="output"></div> | |
</div> | |
<div id="error"></div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
public class Node<T> | |
{ | |
public T? Value { get; set; } | |
public Node<T>? Next { get; set; } | |
public Node() | |
{ | |
} | |
public Node(T value, Node<T>? next = null) | |
{ | |
Value = value; | |
Next = next; | |
} | |
public IEnumerable<T> ToList() | |
{ | |
IList<T> result = new List<T>(); | |
Node<T>? current = this; | |
while (current != null) | |
{ | |
if (current.Value != null) | |
{ | |
result.Add(current.Value); | |
} | |
current = current.Next; | |
} | |
return result; | |
} | |
public Node<T> Reverse() | |
{ | |
Node<T> head = this; | |
Node<T>? previous = null; | |
Node<T>? current = head; | |
while (current != null) | |
{ | |
Node<T>? temp = current.Next; | |
current.Next = previous; | |
previous = current; | |
current = temp; | |
} | |
return previous != null ? previous : this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel.DataAnnotations; | |
public class NumbersModel | |
{ | |
[Display(Name = "Numbers")] | |
[Required] | |
public string? Numbers { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html { | |
font-size: 14px; | |
} | |
@media (min-width: 768px) { | |
html { | |
font-size: 16px; | |
} | |
} | |
html { | |
position: relative; | |
min-height: 100%; | |
} | |
body { | |
margin-bottom: 60px; | |
} | |
#error { | |
color: darkred; | |
} | |
.collapse-icon { | |
cursor: pointer; | |
} | |
#output { | |
background-color: #f8f9fa; | |
border: 1px solid #dee2e6; | |
border-radius: 0.25rem; | |
padding: 1rem; | |
min-height: 60px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$('form').on('submit', function(e) { | |
e.preventDefault(); | |
$('#output').text(''); | |
$('#error').text(''); | |
$.ajax({ | |
url: '/Home/Index', | |
type: 'POST', | |
data: $(this).serialize(), | |
success: function(data) { | |
// Get the output div and the toggle icon | |
const outputDiv = document.getElementById("output-container"); | |
$('#output').text(data.numbers); | |
// Check if the output div is not currently displayed | |
if (outputDiv.style.display === "none" || !outputDiv.classList.contains('show')) { | |
// If it's not displayed, programmatically click the toggle icon. | |
const toggleIcon = document.querySelector(".collapse-icon"); | |
toggleIcon.click(); | |
} | |
}, | |
error: function(data) { | |
$('#error').text(data.responseJSON.message); | |
} | |
}); | |
}); | |
$('[data-bs-toggle="collapse"]').click(function() { | |
$(this).find("i").toggleClass("bi-chevron-down bi-chevron-right"); | |
}); | |
}); |
Author
primaryobjects
commented
Jan 29, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment