Note that there are some explanatory texts on larger screens.

plurals
  1. POReverse a single chained List
    primarykey
    data
    text
    <p>I hope I am using the right terminology. I have made a single-chained list.</p> <pre><code>class MyStack { public Node Initial { get; set; } public MyStack() { Initial = null; } public void Push(int data) { var node = new Node { Data = data, Next = Initial }; Initial = node; } public int Pop() { int res = Initial.Data; Initial = Initial.Next; return res; } public int Sum() { int sum = 0; Node currentNode = Initial; while (currentNode != null) { sum += currentNode.Data; currentNode = currentNode.Next; } return sum; } public int Count() { int count = 0; Node currentNode = Initial; while (currentNode != null) { count++; currentNode = currentNode.Next; } return count; } public void PrintAll() { Node currentNode = Initial; while(currentNode != null) { Console.WriteLine("tmp.Data = " + currentNode.Data); currentNode = currentNode.Next; } } } public class Node { public int Data; public Node Next; } </code></pre> <p>Meaning you can do something like this:</p> <pre><code> var s = new MyStack(); s.Push(5); s.Push(3); s.Push(7); s.PrintAll(); Console.WriteLine("Sum: " + s.Sum()); Console.WriteLine("Count: " + s.Count()); </code></pre> <p>Now, I want to try and make a Reverse method. This seems to be working:</p> <pre><code>public void Reverse() { Node predesesor, location; location = Initial; predesesor = null; while(Initial != null) { Initial = Initial.Next; location.Next = predesesor; predesesor = location; location = Initial; } Initial = predesesor; } </code></pre> <p>I am hardly able to see how it works, and it will be tough to maintain. It seems more like a hack than anything else.</p> <p>Can you offer any assistance?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload