C#——自定義泛型鏈表DEMO

問題描述

 已定義如下一個泛型類和泛型接口,請創建一個泛型的MyLinkedList類,表示一個單向鏈表,滿足指定接口和功能。

1)泛型類,代表鏈表中的節點

    public class Node<T>
{
    public T Value { get; set; }
    public Node<T> Next { get; set; }
    public Node(T value)
    {
        Value = value;
        Next = null;
    }
}

2)泛型接口,定義了列表上可執行的操作

    public interface IMyList<T>
{
    void AddToHead(T value);
    void AddToTail(T value);
    int Count { get; }
    bool Contains(T value);
}

3)要求鏈表繼承IMyList,實現接口中定義的功能,並實現:一無參構造方法,一有參構造方法(支持任意數量參數),重寫ToString方法(生成類似1--> 8--> 9--> 43--> 22-->的字符串)。
   

 class MyLinkedList<T> : IMyList<T>
{
    private Node<T> head;
}

4)最終,使用下述Main方法進行測試。

 public static void Main()
{
    MyLinkedList<int> lst = new MyLinkedList<int>();
    Console.WriteLine(lst.Count);
    Random rd = new Random();
    for (int i = 0; i < 10; i++)
    {
        lst.AddToTail(rd.Next(100));
    }
    lst.AddToHead(100);
    lst.AddToTail(100);
    Console.WriteLine(lst);
    Console.WriteLine(lst.Count);
    Console.WriteLine(lst.Contains(100));
    MyLinkedList<int> list = new MyLinkedList<int>(1, 8, 9, 43, 22);
    Console.WriteLine(list);
}

源代碼 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework5
{
    //1)泛型類,代表鏈表中的節點
    public class Node<T>
    {
        public T Value { get; set; }
        public Node<T> Next { get; set; }
        public Node(T value)
        {
            Value = value;
            Next = null;
        }
    }
    //2)泛型接口,定義了列表上可執行的操作
    public interface IMyList<T>
    {
        void AddToHead(T value);
        void AddToTail(T value);
        int Count { get; }
        bool Contains(T value);
    }
    //3)要求鏈表繼承IMyList,實現接口中定義的功能,並實現:一無參構造方法,一有參構造方法(支持任意數量參數),重寫ToString方法(生成類似1--> 8--> 9--> 43--> 22-->的字符串)。
    class MyLinkedList<T> : IMyList<T>
    {
        private Node<T> head;
        public MyLinkedList(){

        }
        public MyLinkedList(params T[] value) { 
            foreach(T temp in value){
                AddToTail(temp);
            
            }
        }
        public int Count {
            get{
                int count = 0;
                if (head != null) {
                    Node<T> temp = head;
                    while (temp!= null)
                    {
                        count++;
                        temp = temp.Next;
                    }
                }
                return count;
            }   
        }

        public void AddToHead(T value)
        {
            var newNode = new Node<T>(value);
            if (head == null)
            {
                head = newNode;
            }
            else
            {
                newNode.Next = head;
                head = newNode;
            }
        }

        public void AddToTail(T value)
        {
            var newNode = new Node<T>(value);
            if (head == null)
            {
                head = newNode;
            }
            else
            {
                Node<T> temp = head;
                while (temp.Next != null) {
                    temp = temp.Next;
                }
                temp.Next = newNode;
            }
          
        }

        public bool Contains(T value)
        {
            if (head != null) {
                Node<T> temp = head;
                while (temp!= null)
                {
                    if (temp.Value.Equals(value))
                    {
                        return true;
                    }
                    temp = temp.Next;
                }
            }     
            return false;
        }

        public override string ToString()
        {
            string result = "";
            if (head != null)
            {
                Node<T> temp = head;
                while (temp != null)
                {
                    result += temp.Value;
                    if (temp.Next != null)
                        result += ',';
                    temp = temp.Next;
                }
            }
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyLinkedList<int> lst = new MyLinkedList<int>();
            Console.WriteLine(lst.Count);
            Random rd = new Random();
            for (int i = 0; i < 10; i++)
            {
                lst.AddToTail(rd.Next(100));
            }
            lst.AddToHead(100);
            lst.AddToTail(100);
            Console.WriteLine(lst);
            Console.WriteLine(lst.Count);
            Console.WriteLine(lst.Contains(100));
            MyLinkedList<int> list = new MyLinkedList<int>(1, 8, 9, 43, 22);
            Console.WriteLine(list);
        }
    }
}

運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章