反向链表c#
Reverse Linked List c#
我用 C# 制作了一个 link 列表程序,但我还想反转 link 列表中的数字。该程序运行并让我将数字添加到列表中,但是一旦我添加了数字,数字就不会出现在反向部分,它只会输出“反向列表”。如何以相反的顺序显示数字?
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);
}
}
}
如果您想对单链表执行此操作,您将不得不抓取该列表和re-link所有内容
对于列表A>B>C>D>E
,您需要当前为 B,上一个为 A,下一个为 C。您需要将当前更改为指向上一个,然后将所有内容向前推进一个,直到到达结束
/*
A>B>C>D>E
A<B C>D>E
A<B<C D>E
A<B<C<D<E
*/
var prev = head; //A
var curr = head.Next; //B
var next = curr.Next; //C, we need this so C is not lost when we re-point B at A in loop line 1
prev.Next = null; //null<A B>C>D>E
while(true)
{
curr.Next = prev; //null<A<B C>D>E
prev = curr; //prev is now B, was A
curr = next; //curr is now C, was B
if(curr.Next != null){
next = curr.Next; //next is now D, was C
} else {
head = curr;
break;
}
}
我认为这是逻辑;还没有测试过
我用 C# 制作了一个 link 列表程序,但我还想反转 link 列表中的数字。该程序运行并让我将数字添加到列表中,但是一旦我添加了数字,数字就不会出现在反向部分,它只会输出“反向列表”。如何以相反的顺序显示数字?
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);
}
}
}
如果您想对单链表执行此操作,您将不得不抓取该列表和re-link所有内容
对于列表A>B>C>D>E
,您需要当前为 B,上一个为 A,下一个为 C。您需要将当前更改为指向上一个,然后将所有内容向前推进一个,直到到达结束
/*
A>B>C>D>E
A<B C>D>E
A<B<C D>E
A<B<C<D<E
*/
var prev = head; //A
var curr = head.Next; //B
var next = curr.Next; //C, we need this so C is not lost when we re-point B at A in loop line 1
prev.Next = null; //null<A B>C>D>E
while(true)
{
curr.Next = prev; //null<A<B C>D>E
prev = curr; //prev is now B, was A
curr = next; //curr is now C, was B
if(curr.Next != null){
next = curr.Next; //next is now D, was C
} else {
head = curr;
break;
}
}
我认为这是逻辑;还没有测试过