提示 ①题目 1 应该先建立好单循环链表,并返回主调函数该单循环链表的头指针或尾指 针;②用户输入某个整数,在建好的单循环链表中查找该整数,如找到将该结点的地址传给 s; ③根据 s 删除其前驱结点,特别要注意删除的就是第一个结点的前驱;④参考测试用例:如 链表中为(1,2,3,4,5),用户输入 3,结果为(1,2,4,5);如链表中为(1,2,3,4,5),用户输 入 1,则结果应为(1,2,3,4)
Node.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test3._1
{
class Node
{
public int data;
public Node next;
public Node()
{
data = 0;
next = null;
}
public Node(int value)
{
data = value;
}
public Node(Node next)
{
this.next = next;
}
public int Data
{
set { data = value; }
get { return data; }
}
public Node Next
{
set { next = value; }
get { return next; }
}
}
}
XHLinklist.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Test3._1
{
class XHLinklist
{
private Node head;
public XHLinklist()
{
head = null;
}
public void List(int []a,int n)
{
Node addNode = new Node();
Node lastNode = new Node();
lastNode = null;
head = null;
for(int i = n - 1; i >= 0; i--)
{
addNode = new Node();
addNode.data = a[i];
addNode.next = head;
head = addNode;
if (addNode.next == null)
{
lastNode = addNode;
}
lastNode.next = head;
}
}
public void delete(int i)
{
Node deletenode = new Node();
Node node = new Node();
deletenode = head;
node = head;
while (deletenode.next.data != i)
{
node = deletenode;
deletenode = deletenode.next;
}
node.next = deletenode.next;
}
public void MyForeach()//遍历输出
{
Node tempNode = new Node();
tempNode = head;
while (tempNode.next != head)
{
Console.WriteLine(tempNode.data);
tempNode = tempNode.next;
}
Console.WriteLine(tempNode.data);
}
}
}
Program.cs
using System;
namespace Test3._1
{
class Program
{
static void Main(string[] args)
{
XHLinklist xHLinklist = new XHLinklist();
Console.WriteLine("请输入链表的长度");
int length = Convert.ToInt32(Console.ReadLine());
int[] arry = new int[length];
Console.WriteLine("请输入"+length+"个数字");
for (int i = 0; i < length; i++)
{
arry[i] = Convert.ToInt32(Console.ReadLine());
}
xHLinklist.List(arry, length);
Console.WriteLine("删除哪个数字的前驱点?");
int shuzi = Convert.ToInt32(Console.ReadLine());
xHLinklist.delete(shuzi);
xHLinklist.MyForeach();
}
}
}
|