分类目录归档:.Net

.Net

ActionResults action returns

Name Framework Behavior Producing Method
ContentResult Returns a string literal Content
EmptyResult No response
FileContentResult
FilePathResult
FileStreamResult
Return the contents of a file File
HttpUnauthorizedResult Returns an HTTP 403 status
JavaScriptResult Returns a script to execute JavaScript
JsonResult Returns a data in JSON format Json
RedirectResult Redirects the client to a new URL Redirect
RedirectToRouteResult Redirect to another action, or
another controller’s action
RedirectToRoute
RedirectToAction
ViewResult
PartialViewResult
Response is the responsibility
of a view engine
View/PartialView

C#二叉树

public class TreeNode
    {
        private T data;
        private TreeNode leftNode;
        private TreeNode rightNode;
        public TreeNode(T nodeValue)
        {
            data = nodeValue;
            leftNode = null;
            rightNode = null;
        }
        //数据
        public T Data
        {
            get { return data; }
            set { data = value; }
        }
        //左子树
        public TreeNode LeftNode
        {
            get { return leftNode; }
            set { leftNode = value; }
        }
        //右子树
        public TreeNode RightNode
        {
            get { return rightNode; }
            set { rightNode = value; }
        }
        /// 
        /// 前序遍历:先跟节点然后左子树,右子树
        /// 
        /// 
        public void PreOrderTree(TreeNode root)
        {
            if (root != null)
            {
                Console.Write(root.Data);
                PreOrderTree(root.LeftNode);
                PreOrderTree(root.RightNode);
            }
        }
        /// 
        /// 中序遍历:左子树,根节点,右子树可以实现顺序输出
        /// 
        /// 
        public void InOrderTree(TreeNode root)
        {
            if (root != null)
            {
                InOrderTree(root.LeftNode);
                Console.Write(root.Data);
                InOrderTree(root.RightNode);
            }
        }
        /// 
        /// 深度
        /// 
        /// 
        /// 
        public int GetTreeDepth(TreeNode tree)
        {
            if (tree == null)
                return 0;
            int leftDepth = GetTreeDepth(tree.LeftNode);
            int rightDepth = GetTreeDepth(tree.RightNode);
            return leftDepth > rightDepth ? (leftDepth + 1) : (rightDepth + 1);
        }
    }

C#反转链表

public class ReverseLinkedList
    {
        void main()
        {
            Node list = InitList();
            Console.WriteLine(list);
            list = list.ReverseList(list);
            Console.WriteLine(list);
        }
        public Node InitList()
        {
            Node head = new Node();
            head.Next = null;
            head.Data = -1;
            Node q, p;
            p = head;
            for (int i = 1; i <= 10; i++)
            {
                q = new Node();
                q.Data = i;
                q.Next = null;
                p.Next = q;
                p = q;
            }
            return head;
        }
    }
    public class Node
    {
        private T data;
        private Node next;
        public T Data
        {
            get { return data; }
            set { this.data = value; }
        }
        public Node Next
        {
            get { return next; }
            set { next = value; }
        }
        public Node ReverseList(Node head)
        {
            if (head.Next == null || head.Next.Next == null)
            {
                return head;   /*链表为空或只有一个元素则直接返回*/
            }
            Node t = null,
                     p = head.Next,
                     q = head.Next.Next;
            while (q != null)
            {
                t = q.Next;
                q.Next = p;
                p = q;
                q = t;
            }
            /*此时q指向原始链表最后一个元素,也是逆转后的链表的表头元素*/
            head.Next.Next = null;  /*设置链表尾*/
            head.Next = p;           /*调整链表头*/
            return head;
        }
        public override string ToString()
        {
            string result = string.Empty ;
            Node p = this.Next;
            while (p != null)
            {
                result += string.Format("{0}t", p.Data);
                p = p.Next;
            }
            return result;
        }
    }