zoukankan      html  css  js  c++  java
  • 循环双向链表

    第一次用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;
                    }
                }
            }
        }
    }
    

      

  • 相关阅读:
    2018-2019-2 网络对抗技术 20165318 Exp4 恶意代码分析
    2018-2019-2 网络对抗技术 20165318 Exp3 免杀原理与实践
    2018-2019-2 网络对抗技术 20165318 Exp2 后门原理与实践
    2018-2019-2 网络对抗技术 20165318 Exp1 PC平台逆向破解
    2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165318
    2018-2019-2 20165312《网络对抗技术》Exp9 Web安全基础
    a simple example for form
    2018-2019-2 20165312《网络攻防技术》Exp7 网络欺诈防范
    2018-2019-2 20165312《网络攻防技术》Exp6 信息搜集与漏洞扫描
    2018-2019-2 20165312《网络攻防技术》Exp5 MSF基础应用
  • 原文地址:https://www.cnblogs.com/xuyanran/p/8213628.html
Copyright © 2011-2022 走看看