C#单链表的练习

以前学数据结构的时候,都是用C/C++编写的,现在正学C#,打算用C#玩一下数据结构的算法。

单链表的练习如下

using System;
using System.Threading;
using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using System.Reflection;
using System.Diagnostics;


namespace ConTest
{          
    class Programe
    {
       
        struct People
        {
            public string name;
        }
        //打印链表
        private static void DisplayDatas(StudentNode root)
        {
            while (root != null)
            {
                Console.Write(root.order + "\t");
                root = root.next;

            }

        }
        //创建链表
        public static StudentNode CreateStudentLink( StudentNode  root, int total)
        {
            StudentNode cur = root;
            for (int i = 1; i <= total; i++)
            {

                StudentNode s = new StudentNode();
                s.next = null;
                s.order = i;
                if (root == null)
                    cur = root = s;
                else
                {
                    cur.next = s;
                    cur = s;
                }

            }
            return root;


        }

        //将单链表中的节点倒序
        public static StudentNode ConverseStudentNode(StudentNode root)
        {
            if (root == null)
            {
                return null;
            }
            StudentNode pre = null;
            StudentNode cur = root;
            StudentNode rear;
            while (cur != null)
            {

                rear = cur.next;
                cur.next = pre;
                pre = cur;
                cur = rear;
            }
           return root = pre;

        }

        public class StudentNode
        {

            public int order;
            public StudentNode next;

        }

         [STAThread]
        public static void Main()
        {

            StudentNode root=null;
            Console.Write("请输入需要创建的节点数目:n");
            int m = int.Parse(Console.ReadLine());
            root=CreateStudentLink( root, m);
            DisplayDatas(root);

            Console.WriteLine();
            root=ConverseStudentNode(root);
            DisplayDatas(root);
            Console.Read();
           
        }
      
    }
}

  

 

posted @ 2013-03-27 23:31  Predator  阅读(254)  评论(0编辑  收藏  举报