第一次用C#描述链表,和大学时用C++ 描述链表的感觉很一致,用visual studio很快就可以实现单向链表,双向链表, 和循环链表。

The reference type of C# is a key, and play the same role as pointer in C++.

上一段自己写的循环链表代码,单元测试都已经通过。

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

namespace TestCase1.TestCase
{
    public class Node<T> {
        public T data { get; set; }

        public Node<T> previous
        {
            get;
            set;
        }

        public Node<T> next
        {
            get;
            set;
        }

        public Node(T data)
        {
            this.data = data;
        }
    }

    public class CircularLinkedList<T>
    {
        protected Node<T> head = null;

        protected Node<T> tail = null;

        int count = 0;

        public void Add (T element)
        {
            if (this.head == null)
            {
                this.AddFirstItem(element);
            }
            else
            {
                Node<T> newNode = new Node<T>(element);
                this.tail.next = newNode;
                newNode.previous = this.tail;
                newNode.next = this.head;

                this.tail = newNode;

                // important logic
                this.head.previous = this.tail;
            }

            count++;
        }

        public void Remove(T element)
        {
            Node<T> targetNode = this.Find(element);
            if (targetNode != null)
            {
                if (count == 1)
                {
                    this.head = null;
                    this.tail = null;
                }
                else if (targetNode == this.head)
                {
                    this.head = targetNode.next;                   
                    this.head.previous = targetNode.previous;

                    targetNode.next = null;
                    targetNode.previous = null;

                    // important logic
                    this.tail.next = this.head;
                }
                else if (targetNode == this.tail)
                {
                    this.tail = targetNode.previous;
                    this.tail.next = targetNode.next;
                    targetNode.next = null;
                    targetNode.previous = null;

                    this.head.previous = this.tail;
                }
                else
                {
                    targetNode.previous.next = targetNode.next;
                    targetNode.next.previous = targetNode.previous;
                    targetNode.previous = null;
                    targetNode.next = null;
                    
                }

                count--;
            }
        }

        private Node<T> Find(T element)
        {
            Node<T> result = null;
            Node<T> current = this.head;
            
            while(current != null)
            {
                if (Comparer<T>.Equals(current.data, element))
                {
                    result = current;
                    break;
                }
                else
                {
                    current = current.next;
                    if (current == this.head)
                    {
                        break;
                    }
                }
            }

            return result;
        }

        private void AddFirstItem(T element)
        {
            Node<T> newNode = new Node<T>(element);

            if (this.head == null)
            {
                this.head = newNode;
                this.head.next = this.head;
                
                this.tail = this.head;
                this.tail.next = this.head;
                this.tail.previous = this.head;

                this.head.previous = this.tail;
            }
        }

        public void PrintNodes()
        {
            Node<T> temp = this.head;
            while(temp != null)
            {
                Console.WriteLine(temp.data);
                temp = temp.next;
                if (temp == this.head)
                {
                    break;
                }
            }
        }
    }
}

  

 

posted on 2018-01-06 10:57  xuyanran  阅读(140)  评论(0编辑  收藏  举报