应事先建立好一个非循环单链表 L(该链表结点的 data 域类型为 char), 该链表中含三种字符,例如该链表可为如下: (’1’->’c’->’!’->’h’->’$’->’2’->’3’->’i’->’n’->’#’->’a’)
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
namespace ConsoleApp1
{
class Program
{
enum ContentType
{
Number=0,
Letter=1,
Other=2
}
class LinkedList<T>
{
public int Count
{
get { return count; }
set { count = value; }
}
private int count;
private Node<T> head;
private Node<T> last;
public LinkedList()
{
head = null;
count = 0;
}
/// <summary>
/// 在尾部插入一个新的节点
/// </summary>
/// <param name="tValue">新节点的值</param>
public void Add(T tValue)
{
if (last == null)
{
last = new Node<T>(tValue);
head = last;
}
else
{
last.n_next = new Node<T>(tValue);
last = last.n_next;
}
}
/// <summary>
/// 链接两个List
/// </summary>
/// <param name="linkedList"></param>
public void Append(LinkedList<T> linkedList)
{
this.last = linkedList.head;
}
/// <summary>
/// 插入一个值
/// </summary>
/// <param name="node">在此节点之后插入新节点</param>
/// <param name="tValue">新节点的值</param>
public void Insert(Node<T> node,T tValue)
{
Node<T> tempNode = node.n_next; ;
node.n_next = new Node<T>(tValue);
node.n_next.n_next = tempNode;
}
/// <summary>
/// 查找并返回第一个具有相应值的节点
/// </summary>
/// <param name="tValue">需要查找的值</param>
/// <returns></returns>
public Node<T> Find(T tValue)
{
Node<T> tempNode = head;
while (tempNode.n_next != null)
{
if (tempNode.n_data.Equals(tValue))
return tempNode;
tempNode = tempNode.n_next;
}
return null;
}
/// <summary>
/// 删除第一个具有相应值的节点
/// </summary>
/// <param name="tValue">需要删除的值</param>
public void Delete(T tValue)
{
Node<T> tempNode = Find(tValue);
if (!tempNode.Equals(null))
{
tempNode = tempNode.n_next;
}
}
/// <summary>
/// 查找某一个类型的数据,并形成新链表(仅在char类型下可用)
/// </summary>
/// <param name="typeName">类型名称</param>
public LinkedList<char> GetList(ContentType contentType)
{
Node<char> tempNode = head as Node<char>;
LinkedList<char> newList = new LinkedList<char>();
while (tempNode.n_next != null)
{
switch (contentType)
{
case ContentType.Number:
if (tempNode.n_data >= 48 && tempNode.n_data <= 57)
{
newList.Add(tempNode.n_data);
}
break;
case ContentType.Letter:
if (tempNode.n_data >= 65 && tempNode.n_data <= 122)
{
newList.Add(tempNode.n_data);
}
break;
default:
if (tempNode.n_data < 48 || (tempNode.n_data > 57 && tempNode.n_data < 65) || tempNode.n_data > 122)
{
newList.Add(tempNode.n_data);
}
break;
}
tempNode = tempNode.n_next;
}
return newList;
}
public override string ToString()
{
return head == null ? "No Data" : head.ToString();
}
public static LinkedList<T> operator +(LinkedList<T> firstList, LinkedList<T> secondList)
{
Node<T> tempNode = firstList.head;
while (firstList.head.n_next != null)
firstList.head = firstList.head.n_next;
firstList.head.n_next = secondList.head;
firstList.head=tempNode;
return firstList;
}
}
class Node<T>
{
public T n_data;
public Node<T> n_next;
public Node(T data)
:this(data, null)
{ }
public Node(T data, Node<T> next)
{
n_data = data;
n_next = next;
}
public override string ToString()
{
return n_data.ToString()+(n_next!=null?n_next.ToString():String.Empty);
}
}
static void Main(string[] args)
{
Console.WriteLine("请输入链表长度");
int listLength = Convert.ToInt32(Console.ReadLine());
LinkedList<char> linkedList = new Program.LinkedList<char>();
for (int i = 0; i < listLength; i++)
{
linkedList.Add(Convert.ToChar(Console.ReadLine()));
}
Console.WriteLine();
Console.WriteLine("输入的字符串是:");
Console.WriteLine(linkedList.ToString());
Console.WriteLine("------------------------");
Console.WriteLine("数字部分:");
Console.WriteLine(linkedList.GetList(ContentType.Number).ToString());
Console.WriteLine("------------------------");
Console.WriteLine("字母部分:");
Console.WriteLine(linkedList.GetList(ContentType.Letter).ToString());
Console.WriteLine("------------------------");
Console.WriteLine("其他:");
Console.WriteLine(linkedList.GetList(ContentType.Other).ToString());
Console.WriteLine("------------------------");
Console.WriteLine("新的链表:");
Console.WriteLine((linkedList.GetList(ContentType.Number) + linkedList.GetList(ContentType.Letter) + linkedList.GetList(ContentType.Other)).ToString());
}
}
}