zoukankan      html  css  js  c++  java
  • C# 数据结构题目

     

    1.二叉树的先序遍历

    1. 二叉树的先序遍历
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace BinaryTree
    {
        public interface IBinaryTree<T>
        {
            IBinaryTree<T> Left { get; }
            IBinaryTree<T> Right { get; }
            T Data { get; }
        }
    
        public static class BinaryTreeNodeCounter
        {
            public static int CountNodesRecursively<T>(this IBinaryTree<T> tree)
            {
                int BnCount = 0;
    
                if (tree == null)
                    return 0;
    
                Console.Write("{0} ", tree.Data);
                BnCount++;
    
                BnCount = BnCount + CountNodesRecursively<T>(tree.Left);
    
                BnCount = BnCount +CountNodesRecursively<T>(tree.Right);
    
                return BnCount;
            }
        }
    
        class TreeNode<T> : IBinaryTree<T>
        {
            private T data;
            private TreeNode<T> left;
            private TreeNode<T> right;
    
            public TreeNode(T _data,TreeNode<T> _left,TreeNode<T> _right)
            {
                data = _data;
                left = _left;
                right = _right;
            }
    
            public IBinaryTree<T> Left 
            {
                get
                {
                     return left;
                }
            }
    
            public IBinaryTree<T> Right
            {
                get
                {
                    return right;
                }
            }
    
            public T Data 
            {
                get
                {
                    return data;
                }
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                TreeNode<string> nodeF = new TreeNode<string>("F", null, null);
                TreeNode<string> nodeE = new TreeNode<string>("E", null, nodeF);
                TreeNode<string> nodeD = new TreeNode<string>("D", null, null);
                TreeNode<string> nodeC = new TreeNode<string>("C", null, null);
                TreeNode<string> nodeB = new TreeNode<string>("B", nodeD, nodeE);
                TreeNode<string> nodeA = new TreeNode<string>("A", nodeB, nodeC);
    
                int k = BinaryTreeNodeCounter.CountNodesRecursively<string>(nodeA);
    
                Console.WriteLine("
    {0}",k);
    
                Console.ReadLine();
            }
        }
    }
    View Code

    2. 链表的冒泡排序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Link
    {
        public interface Link
        {
            int Data{get;set;}
            Link Next { get;set;}
        }
    
        public class LinkClass : Link
        {
            private int data;
            private Link next;
    
            public LinkClass(int _data,Link _next)
            {
                data = _data;
                next = _next;
            }
    
            public int Data
            {
                get
                {
                    return data;
                }
                set 
                {
                    data = value;
                }
            }
    
            public Link Next
            {
                get
                {
                    return next;
                }
                set
                {
                    next = value;
                }
            }
        }
    
        public class LinkSort
        {
            public static Link SortLink(Link head)
            {
                try
                {
                    int linkLength = 0;
                    Link p = head;
    
                    if (p == null)
                        return null;
    
                    while (p!=null)
                    {
                        linkLength++;
                        p = p.Next;
                    }
    
                    p = head;
    
                    for (int i = 0; i < linkLength; i++)
                    {
                        p = head;
    
                        for (int j= 0; j < linkLength-i-1; j++)
                        {
                            if (p.Data > p.Next.Data)
                            {
                                int temp = p.Data;
                                p.Data = p.Next.Data;
                                p.Next.Data = temp;
                            }
    
                            p = p.Next;
                        }
                    }
    
                    Console.WriteLine("
    链表长度:{0}", linkLength);
                }
                catch
                {
                   
                }
    
                return head;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                LinkClass lc1 = new LinkClass(20,null);
                LinkClass lc2 = new LinkClass(12, lc1);
                LinkClass lc3 = new LinkClass(2, lc2);
                LinkClass lc4 = new LinkClass(0, lc3);
                LinkClass lc5 = new LinkClass(96, lc4);
                LinkClass lc6 = new LinkClass(24, lc5);
                LinkClass lc7 = new LinkClass(13, lc6);
                LinkClass lc8 = new LinkClass(1, lc7);
                LinkClass lc9 = new LinkClass(65, lc8);
                LinkClass lc10 = new LinkClass(34, lc9);
    
                Link p = lc10;
    
                while (p != null)
                {
                    Console.Write("{0} ", p.Data);
                    p = p.Next;
                }
    
                LinkSort.SortLink(lc10);
    
                Console.Write("
    ");
    
                Link q = lc10;
                while (q != null)
                {
                    Console.Write("{0} ", q.Data);
                    q = q.Next;
                }
    
                Console.ReadLine();
            }
        }
    }
    View Code

    3. 冒泡排序(交换排序)

    4. 快速排序 (交换排序)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5  
     6 namespace QuickSort
     7 {
     8     class Program
     9     {
    10         private static void QuickSort(int[] arr, int left, int right)
    11         {
    12             int i, j,temp;
    13  
    14             i = left;
    15             j = right;
    16             temp = arr[i];   //set PivotKey = arr[i], also can be set to PivotKey = arr[(i+j)/2];
    17  
    18             if(left < right)
    19             {
    20                 while(i!=j)
    21                 {
    22                     while (j>i && arr[j] > temp)
    23                         j--;
    24  
    25                     arr[i] = arr[j];
    26  
    27                     while (i<j && arr[i] < temp)
    28                         i++;
    29  
    30                     arr[j] = arr[i];
    31                 }
    32  
    33                 arr[i] = temp;
    34  
    35                 //foreach(int p in arr)
    36                 //{
    37                 //    Console.Write("{0} ",p);
    38                 //}
    39                 //Console.Write("PivotKey: " + temp);
    40  
    41                 //Console.WriteLine();
    42  
    43                 QuickSort(arr,left,i-1);
    44                 QuickSort(arr,j+1,right);
    45             }
    46              
    47         }
    48  
    49         static void Main(string[] args)
    50         {
    51             int[] arr = new int[] {49,38,65,97,76,13,27};
    52  
    53             foreach (int p in arr)
    54             {
    55                 Console.Write("{0} ", p);
    56             }
    57             Console.WriteLine();
    58  
    59             QuickSort(arr,0,arr.Length-1);
    60  
    61             foreach (int p in arr)
    62             {
    63                 Console.Write("{0} ", p);
    64             }
    65             Console.WriteLine();
    66  
    67             Console.ReadLine();
    68         }
    69     }
    70 }
    View Code

     4. 用泛型实现ArrayList的功能

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestSimpleClass
    {
        public interface ISimpleList<T>
        {
            int Count { get; }
            T this[int index] { get; set; }
            void Add(T item);
            void RemoveAt(int index);
            void Clear();
        }
    
        class SimpleList<T> : ISimpleList<T>
        {
            private T[] data;
            private int count = 0;
    
            public SimpleList()
            {
                this.data = new T[4];
            }
    
            public SimpleList(int capacity)
            {
                if (capacity <= 0)
                {
                    throw new ArgumentException("illegal init: "+capacity.ToString());
                }
    
                this.data = new T[capacity];
            }
    
            public int Count
            {
                get
                {
                    return this.count;
                }
            }
    
            private void DoubleStorageArraySize()
            {
                T[] data1 = new T[data.Length * 2];
                for (int i = 0;i<count ;i++)
                {
                    data1[i] = data[i];
                }
    
                data = data1;
            }
    
            public T this[int index]
            {
                //implement
                get
                {
                    if (index < 0 || index>= count)
                    {
                        throw new ArgumentException("illegal init: " + index.ToString());
                    }
    
                    return data[index];
                }
                set
                {
                    data[index] = value;
                }
            }
    
            public void Add(T item)
            {
                if (count >= data.Length)
                    DoubleStorageArraySize();
                
                data[count] = item;
                count++;
            }
    
            public void RemoveAt(int index)
            {
                //implement
                if (index >= count || index<0)
                    throw new ArgumentException("illegal init: " + index.ToString());
    
                for (int i = index;i<count-1 ;i++)
                {
                    data[i] = data[i+1];
                }
    
                count--;
            }
    
            public void Clear()
            {
                //implement
                data = new T[4];
                count = 0;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    SimpleList<int> simpleList = new SimpleList<int>();
    
                    simpleList.Add(5);
                    simpleList.Add(6);
                    simpleList.Add(9);
                    simpleList.Add(4);
                    simpleList.Add(67);
                    simpleList.Add(12);
                    simpleList.Add(90);
                    simpleList.Add(85);
                    simpleList.Add(76);
    
                    Console.WriteLine("原数组和长度为:{0}" ,simpleList.Count);
                    for (int i = 0; i<simpleList.Count;i++)
                    {
                        Console.Write("{0} ",simpleList[i]);
                    }
    
                    simpleList.RemoveAt(2);
    
                    Console.WriteLine("
    删除后的数组和长度为:{0}",simpleList.Count);
                    for (int i = 0; i < simpleList.Count; i++)
                    {
                        Console.Write("{0} ", simpleList[i]);
                    }
    
                    simpleList.Clear();
    
                    Console.WriteLine("
    清空后的数组和长度为:{0}",simpleList.Count);
                    for (int i = 0; i < simpleList.Count; i++)
                    {
                        Console.Write("{0} ", simpleList[i]);
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
    
                Console.ReadLine();
            }
        }
    }
    View Code

     5. 直接插入排序 (插入排序 - Straight insert sort)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace StraightInsertSort
    {
        class Program
        {
            static void StraightInsertSort(int[] arr)
            {
                int i, j, temp;
    
                for(i=1; i<arr.Length; i++)
                {
                    temp = arr[i];
                    j = i - 1;
    
                    while(j>=0 && arr[j] > temp)
                    {
                        arr[j + 1] = arr[j];
                        j--;
                    }
                    arr[j+1] = temp;
    
                    //each time
                    foreach (int p in arr)
                    {
                        Console.Write("{0} ", p.ToString());
                    }
                    Console.Write("
    ");
                }
            }
    
            static void Main(string[] args)
            {
                int[] arr = { 10,5,9,14,25,6,21,30};
                foreach(int i in arr)
                {
                    Console.Write("{0} ",i.ToString());
                }
                Console.Write("
    ");
    
                StraightInsertSort(arr);
    
                Console.ReadLine();
            }
        }
    }
    View Code

     6.希尔排序 (插入排序 - Shell sort)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ShellSort
    {
        class Program
        {
            static void ShellSort(int[] arr)
            {
                int i, j, d, temp;
    
                //set the increment
                d = arr.Length / 2;
    
                while( d>0 )
                {
                    Console.WriteLine("Increment is {0}.",d.ToString());
                    for (i = d; i<arr.Length;i++)
                    {
                        temp = arr[i];
                        j = i-d;
                        while(j>=0 && arr[j]>temp)
                        {
                            arr[j + d] = arr[j];
                            j=j-d;
                        }
    
                        arr[j+d] = temp;
    
                        foreach (int p in arr)
                        {
                            Console.Write("{0} ", p.ToString());
                        }
                        Console.Write("
    ");
                    }
                    d = d / 2;
                }
            }
    
            static void Main(string[] args)
            {
                int[] arr = {9,8,7,6,5,4,3,2,1,0};
                foreach(int i in arr)
                {
                    Console.Write("{0} ",i.ToString());
                }
                Console.Write("
    ");
    
                ShellSort(arr);
    
                Console.ReadLine();
            }
        }
    }
    View Code

    7. 简单选择排序(Simple Select Sort)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SimpleSelectSort
    {
        class Program
        {
            static void SimpleSelectSort(int[] arr)
            {
                int i, j, min, temp;
    
                for (i = 0; i< arr.Length-1 ;i++)
                {
                    min = i;
    
                    for (j = i + 1;j<arr.Length ;j++)
                    {
                        if(arr[j] < arr[min])
                        {
                            min = j;
                        }
                    }
    
                    if(min != i)
                    {
                        temp = arr[i];
                        arr[i] = arr[min];
                        arr[min] = temp;
                    }
    
                    for (int p = 0; p < arr.Length; p++)
                    {
                        Console.Write("{0} ", arr[p].ToString());
                    }
                    Console.Write("
    ");
                }
            }
    
            static void Main(string[] args)
            {
                int[] arr = {6,8,7,9,0,1,3,2,4,5 };
    
                for(int p=0;p<arr.Length;p++)
                {
                    Console.Write("{0} ",arr[p].ToString());
                }
                Console.Write("
    ");
    
                SimpleSelectSort(arr);
    
                Console.ReadLine();
            }
        }
    }
    View Code

     8. 堆排序(Heap Sort)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace HeapSort
    {
        class Program
        {
            static void Sift(int[] arr,int low, int high)
            {
                int i, j, temp;
    
                i = low;
                j= i*2+1;
                temp = arr[i];
    
                while( j< high)
                {
                    if(j+1 < high && arr[j] < arr[j+1])
                    {
                        j++;
                    }
    
                    if (temp < arr[j])
                    {
                        arr[i] = arr[j];
                        i = j;
                        j = i * 2+1;
                    }
                    else break;
                }
    
                arr[i] = temp;
            }
    
            static void HeapSort(int[] arr)
            {
                int i, temp;
    
                //intial the heap
                for (i = arr.Length/2-1; i>=0 ;i--)
                {
                    Sift(arr, i, arr.Length);
                }
    
                for (i = 0; i < arr.Length; i++)
                {
                    Console.Write("{0} ", arr[i]);
                }
                Console.Write("
    ");
    
                for(i = arr.Length-1; i>= 1 ;i--)
                {
                    temp = arr[0];
                    arr[0] = arr[i];
                    arr[i] = temp;
    
                    Console.WriteLine(temp);
    
                    for (int p = 0; p < arr.Length; p++)
                    {
                        Console.Write("{0} ", arr[p]);
                    }
                    Console.Write("
    ");
    
                    Sift(arr, 0, i);
    
                    for (int p = 0; p < arr.Length; p++)
                    {
                        Console.Write("{0} ", arr[p]);
                    }
                    Console.Write("
    ");
                }
            }
    
            static void Main(string[] args)
            {
                int[] arr = { 6,8,7,9,0,1,3,2,4,5};
    
                HeapSort(arr);
    
                Console.ReadLine();
            }
        }
    }
    View Code
  • 相关阅读:
    vue(七)--监听属性(watch)
    vue(六)--计算属性(computed)
    JVM参数配置&&命令工具
    GC回收算法&&GC回收器
    JVM宏观认知&&内存结构
    分布式配置中心Apollo——QuickStart
    了解敏捷开发
    服务链路跟踪 && 服务监控
    数据读写API——IO流
    Nginx+Zuul集群实现高可用网关
  • 原文地址:https://www.cnblogs.com/zzunstu/p/3564528.html
Copyright © 2011-2022 走看看