问题描述
我已经用C#制作了一个链接列表程序,但我也想反转链接列表中的数字。该程序运行,让我将数字添加到列表中,但是一旦添加了数字,数字就不会出现在反向部分中,它只会输出“反向列表”。如何以相反的顺序显示数字?
using System;
namespace LinkedList
{
class Program
{
public class Node
{
public int data;
public Node next;
};
static Node add(Node head,int data)
{
Node temp = new Node();
Node current;
temp.data = data;
temp.next = null;
if (head == null)
head = temp;
else
{
current = head;
while (current.next != null)
current = current.next;
current.next = temp;
}
return head;
}
static void reverse_list(Node head)
{
Node prev = null,current = head,next = null;
while (current != null)
next = current.next;
current.next = prev;
prev = current;
current = next;
}
static void print_numbers(Node head)
{
while (head != null)
{
Console.Write(head.data + " ");
head = head.next;
}
}
static Node List(int[] a,int n)
{
Node head = null;
for (int i = 1; i <= n; i++)
head = add(head,a[i]);
return head;
}
public static void Main(String[] args)
{
int n = 10;
int[] a;
a = new int[n + 1];
a[0] = 0;
Console.WriteLine("Add values to the list");
for (int i = 1; i <= n; i++)
a[i] = int.Parse(Console.ReadLine());
Node head = List(a,n);
Console.WriteLine("Linked List: ");
print_numbers(head);
Console.ReadLine();
Console.WriteLine();
Console.WriteLine("Reversed list: ");
reverse_list(head);
print_numbers(head);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)