【c#数据结构】已知由单链表表示的线性表中,含有三类字符的数据元素,设计算法构造三个以循环链表示的线性表,使每一个表中只含同一类的字符,且利用原表中的结点空间作为这三个表的空间。

应事先建立好一个非循环单链表 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());
        }
    }
}

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值