zoukankan      html  css  js  c++  java
  • 单向链表(C#)

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;
    using System.Collections;
    
    namespace ConsoleApplication2
    {
        public class Program
        {
            public static void Main()
            {
                SinglyLinked<int> noteLink = new SinglyLinked<int>();
    
                Console.WriteLine("新增数据");
                noteLink.AddNote(5);
                noteLink.AddNote(8);
                noteLink.AddNote(4);
                noteLink.AddNote(1);
                noteLink.AddNote(2);
                foreach (var item in noteLink)
                {
                    Console.WriteLine(item);
                }
    
                Console.WriteLine("删除数据2");
                noteLink.RemoveNote(2);
                foreach (var item in noteLink)
                {
                    Console.WriteLine(item);
                }
    
                Console.WriteLine("在8前面增加99");
                noteLink.InsertItemBefore(8,99);
    
                foreach (var item in noteLink)
                {
                    Console.WriteLine(item);
                }
    
                Console.WriteLine("在1后面增加55");
                noteLink.InsertItemAfter(1,55);
                foreach (var item in noteLink)
                {
                    Console.WriteLine(item);
                }
    
                Console.WriteLine("节点数量:"+noteLink.Count);
    
                Console.Read();
            }
        }
    
    
    
        public class SinglyLinked<T> where T : IComparable
        {
            public DNote<T> HeadNote;
    
            public int Count{get;set;}
    
            public SinglyLinked()
            {
                HeadNote = new DNote<T>(default(T));
            }
    
            public void AddNote(T t)
            {
                DNote<T> tNote = HeadNote;
                while (tNote.NextNote!= null)
                {
                    tNote = tNote.NextNote;
                }
    
                tNote.NextNote = new DNote<T>(t);
    
                Count++;
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                DNote<T> tNote = HeadNote;
                while (tNote.NextNote != null)
                {
                    tNote = tNote.NextNote;
                    yield return tNote.NoteValue;
                }
            }
    
            public void RemoveNote(T t)
            {
                DNote<T> tNote = HeadNote;
                do
                {
                    if (tNote.NextNote.NoteValue.CompareTo(t) == 0)
                    {
                        tNote.NextNote = tNote.NextNote.NextNote;
                        Count--;
                    }
                    tNote = tNote.NextNote;
                } while (tNote!=null && tNote.NextNote!=null);
            }
    
            public void InsertItemBefore(T t,T insertNoteValue)
            {
                DNote<T> tNote = HeadNote;
                do
                {
                    if (tNote.NextNote.NoteValue.CompareTo(t) == 0)
                    {
                        var nextNote = tNote.NextNote;
                        var newNote = new DNote<T>(insertNoteValue);
                        newNote.NextNote = nextNote;
                        tNote.NextNote = newNote;
                        Count++;
                        break;
                    }
                    tNote = tNote.NextNote;
                } while (tNote != null && tNote.NextNote != null);
            }
    
    
            public void InsertItemAfter(T t, T insertNoteValue)
            {
                DNote<T> tNote = HeadNote;
                do
                {
                    if (tNote.NextNote.NoteValue.CompareTo(t) == 0)
                    {
                        var nextNote = tNote.NextNote.NextNote;
                        var newNote = new DNote<T>(insertNoteValue);
                        newNote.NextNote = nextNote;
                        tNote.NextNote.NextNote = newNote;
                        Count++;
                        break;
                    }
                    tNote = tNote.NextNote;
                } while (tNote != null && tNote.NextNote != null);
            }
        }
    
    
        public class DNote<T>
        {
            public DNote<T> NextNote { get; set;}
    
            public T NoteValue { get; set; }
    
    
            public DNote(T t)
            {
                NoteValue = t;
            }
        }
    
    
    }
  • 相关阅读:
    10进制转换为二十六进制字符串A-Z
    解决Missing artifact jdk.tools:jdk.tools:jar:1.8报错
    JAVA中AES对称加密和解密以及与Python兼容
    Nginx配置客户端SSL双向认证
    (备忘)最全的正则表达式
    (备忘)Python字符串、元组、列表、字典互相转换的方法
    (备忘)Nodepad++常用快捷键
    (备忘)正则表达式全部符号解释
    (备忘)Window7下安装Python2.6及Django1.4
    (备忘)Java Map 遍历
  • 原文地址:https://www.cnblogs.com/bbvi/p/5045539.html
Copyright © 2011-2022 走看看