| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1529 人关注过本帖, 2 人收藏
标题:C#数据结构 开一帖
取消只看楼主 加入收藏
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:Program.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/11/

 * @概  述:使用顺序链表实现进制转换

 */
using System;

public interface IStack<T>
{
    int GetLength();//求栈的长度
    bool IsEmpty();//判断栈是否为空
    void Clear();//清空栈操作
    void Push(T nItem);//入栈
    void Pop();//出栈
    T GetTop();//取栈顶元素
}
//顺序栈
public class SeqStack<T> : IStack<T>
{
    private int m_MaxSize;//顺序栈的容量
    private T[] m_Data; //数组
    private int m_Top;//指示顺序栈的栈顶

    //索引器
    public T this[int nIndex]
    {
        get { return m_Data[nIndex];}
        set { m_Data[nIndex] = value; }
    }
    //容量属性
    public int MaxSize
    {
        get { return m_MaxSize; }
        set { m_MaxSize = value; }
    }
    //构造器
    public SeqStack(int nSize)
    {
        m_Data = new T[nSize];
        m_MaxSize = nSize;
        m_Top = -1;
    }
    //求栈的长度
    public int GetLength()
    {
        return (m_Top + 1);
    }
    //判断栈是否为空
    public bool IsEmpty()
    {
        return (-1 == m_Top);
    }
    //判断栈是否为满
    public bool IsFull()
    {
        return (m_MaxSize - 1 == m_Top);
    }
    //清空栈操作
    public void Clear()
    {
        m_Top = -1;
    }
    //进栈
    public void Push(T nItem)
    {
        if (IsFull())
        {
            Console.WriteLine("压栈失败,栈满...");
            return;
        }
        m_Data[++m_Top] = nItem;
    }
    //出栈
    public void Pop()
    {
        if (IsEmpty())
        {
            Console.WriteLine("出栈失败,栈空...");
            return;
        }
        --m_Top;
    }
    //取栈顶元素
    public T GetTop()
    {
        return m_Data[m_Top];
    }
}

public class SamplesArrayList
{
    static SeqStack<int> stack = new SeqStack<int>(80);

    public static void Main()
    {
        int N;//要转换的进制
        int getConsoleInput;//

        Console.Write("输入要变换的数字(十进制):");
        getConsoleInput = int.Parse(Console.ReadLine());
        Console.Write("输入要转换的进制:");
        N = int.Parse(Console.ReadLine());

        Conversion(getConsoleInput, N);

        Console.Write("转换成"+N+"进制后的结果:");
        while (!stack.IsEmpty())
        {
            if (stack.GetTop() >= 10)
            {
                Console.Write((char)(87 + stack.GetTop()));
            }
            else
            {
                Console.Write(stack.GetTop());
            }
            stack.Pop();
        }
        Console.WriteLine();
    }

    public static void Conversion(int a, int b)
    {
        do
        {
            stack.Push(a%b);
            a = a / b;
        } while (0 != a);
    }
}

输入要变换的数字(十进制):250
输入要转换的进制:16
转换成16进制后的结果:fa
请按任意键继续. . .
2012-05-11 08:25
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:Program.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/12/

 * @概  述:使用链栈实现进制转换

 */
using System;

public interface IStack<T>
{
    int GetLength();//求栈的长度
    bool IsEmpty();//判断栈是否为空
    void Clear();//清空栈操作
    void Push(T nItem);//入栈
    void Pop();//出栈
    T GetTop();//取栈顶元素
}

//结点
class Node<T>
{
    private T m_Data;
    private Node<T> m_Next;

    //m_Next属性
    public Node<T> Next
    {
        get { return m_Next; }
        set { m_Next = value; }
    }
    //m_Data属性
    public T Data
    {
        get { return m_Data; }
        set { m_Data = value; }
    }
    //构造函数
    public Node(T nData, Node<T> nNext)
    {
        m_Data = nData;
        m_Next = nNext;
    }
    public Node()
    {
        m_Data = default(T);
        m_Next = null;
    }
}

//链栈
public class LinkStack<T> : IStack<T>
{
    private Node<T> m_Top;
    private int m_Length;

    //构造器
    public LinkStack()
    {
        m_Length = 0;
        m_Top = null;
    }
    //求栈的长度
    public int GetLength()
    {
        return m_Length;
    }
    //判断栈是否为空
    public bool IsEmpty()
    {
        return (null == m_Top);
    }
    //清空栈操作
    public void Clear()
    {
        m_Top = null;
    }
    //进栈
    public void Push(T nItem)
    {
        if (IsEmpty())
        {
            m_Top = new Node<T>(nItem, null);
            ++m_Length;
            return;
        }
        m_Top = new Node<T>(nItem, m_Top);
        ++m_Length;
    }
    //出栈
    public void Pop()
    {
        if (IsEmpty())
        {
            Console.WriteLine("出栈失败,栈空...");
            return;
        }
        m_Top = m_Top.Next;
        --m_Length;
    }
    //取栈顶元素
    public T GetTop()
    {
        return m_Top.Data;
    }
}

public class SamplesArrayList
{
    static LinkStack<int> stack = new LinkStack<int>();

    public static void Main()
    {
        int N;//要转换的进制
        int getConsoleInput;//

        Console.Write("输入要变换的数字(十进制):");
        getConsoleInput = int.Parse(Console.ReadLine());
        Console.Write("输入要转换的进制:");
        N = int.Parse(Console.ReadLine());

        Conversion(getConsoleInput, N);

        Console.Write("转换成"+N+"进制后的结果:");
        while (!stack.IsEmpty())
        {
            if (stack.GetTop() >= 10)
            {
                Console.Write((char)(87 + stack.GetTop()));
            }
            else
            {
                Console.Write(stack.GetTop());
            }
            stack.Pop();
        }
        Console.WriteLine();
    }

    public static void Conversion(int a, int b)
    {
        do
        {
            stack.Push(a%b);
            a = a / b;
        } while (0 != a);
    }
}

输入要变换的数字(十进制):10
输入要转换的进制:8
转换成8进制后的结果:12
请按任意键继续. . .
2012-05-12 07:45
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:Program.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/13/

 * @概  述:使用栈实现 括号匹配的检验 其中括号只有[ ] ( ) 字符组成

 */
using System;

//此处省略掉栈的实现环节

public class App
{
    static LinkStack<char> stack = new LinkStack<char>();
    static string nStr;
    public enum Opr{OPR_IN, OPR_OUT}

    public static void Main()
    {
        Console.Write("输入要检验的括号字符串:");
        nStr = Console.ReadLine();
        if (Compare())
        {
            Console.WriteLine("结果:匹配成功!");
        }
        else
        {
            Console.WriteLine("结果:匹配失败!");
        }
    }

    //检验成功返回true  否则返回false
    public static bool Compare()
    {
        for (int i = 0; i < nStr.Length; ++i)
        {
            switch (GetOpr(nStr[i]))
            {
                case Opr.OPR_IN:
                    stack.Push(nStr[i]);
                    break;
                case Opr.OPR_OUT:
                    if (stack.GetTop() == '[' && nStr[i] == ']' ||
                        stack.GetTop() == '(' && nStr[i] == ')')
                    {
                        stack.Pop();
                    }
                    else
                    {
                        return false;
                    }
                    break;
                default: return false;
            }
        }
        return stack.IsEmpty();
    }

    //获取字符应该执行的操作
    public static Opr GetOpr(char c)
    {
        if ('[' == c || '(' == c)
        {//进栈操作
            return Opr.OPR_IN;
        }
        //出栈操作
        return Opr.OPR_OUT;
    }
}


输入要检验的括号字符串:[]([])[()]()
结果:匹配成功!
请按任意键继续. . .

输入要检验的括号字符串:(()]
结果:匹配失败!
请按任意键继续. . .
2012-05-13 10:10
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:Program.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/13/

 * @概  述:使用栈实现 行编辑器

 */
using System;

//此处省略掉栈的实现环节

public class App
{
    static LinkStack<char> stack = new LinkStack<char>();
    static string nStr;

    public static void Main()
    {
        Console.Write("输入要字符串:");
        nStr = Console.ReadLine();
        LineEditor();
        Console.Write("得到的最终字符串为:");
        PrintResult();
        Console.WriteLine();
    }
    /**
     * 功  能:在字符中 ‘#’为退格 ‘@’为删除该行中的内容
     */
    public static void LineEditor()
    {
        foreach(char i in nStr)
        {
            if (i == '#')
            {
                stack.Pop();
            }
            else if (i == '@')
            {
                stack.Clear();
            }
            else
            {
                stack.Push(i);
            }
        }
    }
    //
    public static void PrintResult()
    {
        if (!stack.IsEmpty())
        {
            char i = stack.GetTop();
            stack.Pop();
            PrintResult();
            Console.Write(i);  
        }
    }
}

输入要字符串:whli##ilr#e(s#*s)
得到的最终字符串为:while(*s)
请按任意键继续. . .

输入要字符串:outcha@putchar(*s=#++);
得到的最终字符串为:putchar(*s++);
请按任意键继续. . .


[ 本帖最后由 寒风中的细雨 于 2012-5-13 12:41 编辑 ]
2012-05-13 11:53
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:App.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/14/

 * @概  述:使用栈实现 迷宫

 */
using System;

enum Dir
{
    EAST,//
    WEST,//西
    SOUTH,//
    NORTH,//
    END
}

class Pos//坐标
{
    int m_Row;//行号
    int m_Col;//列号
    Dir m_Dir;//方向
    public Pos(int nRow, int nCol, Dir nDir)
    {
        m_Row = nRow;
        m_Col = nCol;
        m_Dir = nDir;
    }
    public int Row
    {
        get { return m_Row; }
        set { m_Row = value; }
    }
    public int Col
    {
        get { return m_Col; }
        set { m_Col = value; }
    }
    public Dir Dir
    {
        get { return m_Dir; }
        set { m_Dir = value; }
    }
    public bool Equal(Pos obj)
    {
        return m_Col == obj.m_Col && m_Row == obj.m_Row;
    }
}

class App
{
    static int[,] map =//迷宫地图
    {  //0  1  2  3  4  5  6  7  8  9
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},//0
        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},//1
        {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},//2
        {1, 0, 0, 0, 0, 1, 1, 0, 0, 1},//3
        {1, 0, 1, 1, 1, 0, 0, 0, 0, 1},//4
        {1, 0, 0, 0, 1, 0, 0, 0, 0, 1},//5
        {1, 0, 1, 0, 0, 0, 1, 0, 0, 1},//6
        {1, 0, 1, 1, 1, 0, 1, 1, 0, 1},//7
        {1, 1, 0, 0, 0, 0, 0, 0, 0, 1},//8
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},//9
    };
    static LinkStack<Pos> stack = new LinkStack<Pos>();

    static void Main(string[] args)
    {
        Pos startPos = new Pos(1, 1, Dir.EAST);//起点
        Pos endPos = new Pos(8, 8, Dir.EAST);//终点

        stack.Push(startPos);//压入起点
        map[startPos.Row, startPos.Col] = 1;

        MazePath(endPos);//

        Console.WriteLine("下面为行走轨迹...");
        PrintTrace();
    }

    //走迷宫
    static void MazePath(Pos endPos)
    {
        while (!stack.IsEmpty() && !stack.GetTop().Equal(endPos))
        {
            Pos tmpPos = stack.GetTop();

            switch (tmpPos.Dir)
            {
            case Dir.EAST://
                tmpPos.Dir = Dir.SOUTH;//修改方向
                if (0 == map[tmpPos.Row, tmpPos.Col + 1])
                {
                    stack.Push(new Pos(tmpPos.Row, tmpPos.Col+1, Dir.EAST));
                    map[tmpPos.Row, tmpPos.Col + 1] = 1;
                }
                break;
            case Dir.SOUTH://
                tmpPos.Dir = Dir.WEST;//修改方向
                if (0 == map[tmpPos.Row+1, tmpPos.Col])
                {
                    stack.Push(new Pos(tmpPos.Row + 1, tmpPos.Col, Dir.EAST));
                    map[tmpPos.Row + 1, tmpPos.Col] = 1;
                }
                break;
            case Dir.WEST://西
                tmpPos.Dir = Dir.NORTH;//修改方向
                if (0 == map[tmpPos.Row, tmpPos.Col-1])
                {
                    stack.Push(new Pos(tmpPos.Row, tmpPos.Col - 1, Dir.EAST));
                    map[tmpPos.Row, tmpPos.Col - 1] = 1;
                }
                break;
            case Dir.NORTH://
                tmpPos.Dir = Dir.END;//修改方向
                if (0 == map[tmpPos.Row-1, tmpPos.Col])
                {
                    stack.Push(new Pos(tmpPos.Row-1, tmpPos.Col, Dir.EAST));
                    map[tmpPos.Row - 1, tmpPos.Col] = 1;
                }
                break;
            case Dir.END://出栈
                stack.Pop();
                break;
            default:
                break;
            }
        }
    }

    //输出行走轨迹
    static void PrintTrace()
    {
        if (!stack.IsEmpty())
        {
            Pos tmpPos = stack.GetTop();
            stack.Pop();
            PrintTrace();
            Console.WriteLine("("+tmpPos.Row+", "+tmpPos.Col+")");
        }
    }
}
2012-05-14 11:29
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:App.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/14/

 * @概  述:使用栈实现 中缀表达式转为后缀表达式

 *          支持一位整数的操作数

 */
using System;

class App
{
    static int[,] op_pri = //运算符为左结合 所以栈里面的优先级大于栈外的
    {//  +, -, *, /, #, (, )
        {2, 2, 4, 4, 0, 6, 1},//out
        {3, 3, 5, 5, 0, 1, 'X'}//in
    };
    static LinkStack<char> optr = new LinkStack<char>();//操作符
    static LinkStack<char> opnd = new LinkStack<char>();//操作数

    static void Main(string[] args)
    {
        string nStr;
        Console.Write("输入表达式:");
        nStr = Console.ReadLine();
        nStr += '#';
        optr.Push('#');
        EvaluateExpression(nStr);
        Console.WriteLine("结果为:");
        Print();
    }

    static void EvaluateExpression(string nStr)
    {
        for (int i=0; i<nStr.Length;)
        {
            if (-1 == GetIndex(nStr[i]))
            {
                opnd.Push(nStr[i]);
                ++i;
            }
            else
            {
                i = DoOptr(nStr[i], i);
            }
        }
    }

    static int DoOptr(char nChar, int i)
    {
        int nIn = GetIndex(optr.GetTop());
        int nOut = GetIndex(nChar);
        if (op_pri[0, nOut] > op_pri[1, nIn])
        {//进栈
            optr.Push(nChar);
            return i + 1;
        }
        else if (op_pri[0, nOut] < op_pri[1, nIn])
        {//出栈
            opnd.Push(optr.GetTop());
            optr.Pop();
            return i;
        }
        else
        {
            optr.Pop();
            return i + 1;
        }
    }

    static void Print()
    {
        if (!opnd.IsEmpty())
        {
            char nChar = opnd.GetTop();
            opnd.Pop();
            Print();
            Console.Write(nChar);
        }
    }

    //获取字符在数组中对应的下标志
    static int GetIndex(char nChar)
    {
        switch (nChar)
        {
            case '+':
                return 0;
                break;
            case '-':
                return 1;
                break;
            case '*':
                return 2;
                break;
            case '/':
                return 3;
                break;
            case '#':
                return 4;
                break;
            case '(':
                return 5;
                break;
            case ')':
                return 6;
                break;
            default:
                return -1;
                break;
        }
    }
}

输入表达式:4+2*3-9/5
结果为:
423*+95/-请按任意键继续. . .
2012-05-14 11:30
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:App.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/14/

 * @概  述:使用栈实现  字符“ABCD”的全排

 */
using System;
class Node
{
    private int m_PosSwap;
    private string m_Data;
    public Node(int nPos, string nData)
    {
        m_Data = nData;
        m_PosSwap = nPos;
    }
    public int GetPos
    {
        get { return m_PosSwap; }
        set { m_PosSwap = value; }
    }
    public string Data
    {
        get { return m_Data; }
        set { m_Data = value; }
    }
}
class App
{
    //要进行全排的字符串
    static string m_Str = "ABCD";
    static LinkStack<Node> stack = new LinkStack<Node>();

    static void Main(string[] args)
    {
        stack.Push(new Node(-1, m_Str));//初始化栈
        DoAllSort();//进行全排
    }

    static void DoAllSort()
    {
        while (!stack.IsEmpty())
        {
            Node tmpNode = stack.GetTop();
            stack.Pop();
            string nStr = tmpNode.Data;//当前处理的字符串
            int nStartPos = tmpNode.GetPos+1;//字符交换的起始位置
            char[] m_Array = new char[nStr.Length];
            nStr.CopyTo(0, m_Array, 0, nStr.Length);
            for (int i = nStartPos; i < nStr.Length; ++i)
            {
                //交换字符串的位置
                CharSwap(ref m_Array[i], ref m_Array[nStartPos]);
                stack.Push(new Node(nStartPos, new string(m_Array)));
                CharSwap(ref m_Array[i], ref m_Array[nStartPos]);
            }
            PrintArrange();
        }
    }

    static void CharSwap(ref char c, ref char b)
    {
        char tmp = c;
        c = b;
        b = tmp;
    }

    static void PrintArrange()
    {
        while (!stack.IsEmpty())
        {
            Node tmpNode = stack.GetTop();
            if (tmpNode.GetPos + 1 == tmpNode.Data.Length)
            {
                foreach (char i in tmpNode.Data)
                {
                    Console.Write("{0} ", i);
                }
                Console.WriteLine();
                stack.Pop();//出栈
            }
            else
            {
                break;
            }
        }
    }
}

D A B C
D A C B
D C A B
D C B A
D B A C
D B C A
C D B A
C D A B
C A D B
C A B D
C B D A
C B A D
B D A C
B D C A
B C D A
B C A D
B A D C
B A C D
A D B C
A D C B
A C D B
A C B D
A B D C
A B C D
请按任意键继续. . .
2012-05-14 15:24
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
回复 17楼 寒风中的细雨
因为对象指示器属性为只读, 中间只好转向到字符数组中去交换字符操作

因为不熟悉C#中字符串 和 字符数组   感觉和c++中的有很大的差别  用起来有点费神

因此代码看起来很臃肿...    留待后面再改进吧
2012-05-14 15:31
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
回复 19楼 BlueGuy
写的不好的地方,  是希望路过的前辈可以加以改正。

你突然蹦出这么句话  我表示很茫然
2012-05-14 18:41
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
程序代码:
/**

 * @文件名:Program.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/16/

 * @概  述:栈的基本操作, 队列的基本操作

 */
using System;

public interface IStack<T>
{
    int GetLength();//求栈的长度
    bool IsEmpty();//判断栈是否为空
    void Clear();//清空栈操作
    void Push(T nItem);//入栈
    void Pop();//出栈
    T GetTop();//取栈顶元素
}
public interface IQueue<T>
{
    int GetLength();//求队列的长度
    bool IsEmpty();//判断队列是否为空
    void Clear();//清空队列
    void In(T nItem);//入队
    void Out();//出队
    T GetFront();//取对头元素
}
//结点
class Node<T>
{
    private T m_Data;
    private Node<T> m_Next;
    //m_Next属性
    public Node<T> Next
    {
        get { return m_Next; }
        set { m_Next = value; }
    }
    //m_Data属性
    public T Data
    {
        get { return m_Data; }
        set { m_Data = value; }
    }
    //构造函数
    public Node(T nData, Node<T> nNext)
    {
        m_Data = nData;
        m_Next = nNext;
    }
    public Node()
    {
        m_Data = default(T);
        m_Next = null;
    }
}
//链栈
public class LinkStack<T>:IStack<T>
{
    private Node<T> m_Top;
    private int m_Length;
    //构造器
    public LinkStack()
    {
        m_Length = 0;
        m_Top = null;
    }
    //求栈的长度
    public int GetLength()
    {
        return m_Length;
    }
    //判断栈是否为空
    public bool IsEmpty()
    {
        return (null == m_Top);
    }
    //清空栈操作
    public void Clear()
    {
        m_Top = null;
    }
    //进栈
    public void Push(T nItem)
    {
        if (IsEmpty())
        {
            m_Top = new Node<T>(nItem, null);
            ++m_Length;
            return;
        }
        m_Top = new Node<T>(nItem, m_Top);
        ++m_Length;
    }
    //出栈
    public void Pop()
    {
        if (IsEmpty())
        {
            Console.WriteLine("出栈失败,栈空...");
            return;
        }
        m_Top = m_Top.Next;
        --m_Length;
    }
    //取栈顶元素
    public T GetTop()
    {
        return m_Top.Data;
    }
}
//队列
public class LinkQueue<T>:IQueue<T>
{
    private Node<T> m_Rear;//队尾
    private Node<T> m_Front;//对头
    private int m_Length;//队长度
    //构造函数
    public LinkQueue()
    {
        m_Length = 0;
        m_Front = m_Rear = null;
    }
    //求队列的长度
    public int GetLength()
    {
        return m_Length;
    }
    //判断队列是否为空
    public bool IsEmpty()
    {
        return 0 == m_Length;
    }
    //清空队列
    public void Clear()
    {
        m_Length = 0;
        m_Front = m_Rear = null;
    }
    //入队
    public void In(T nItem)
    {
        if (this.IsEmpty())
        {
            m_Front = m_Rear = new Node<T>(nItem, null);
        }
        else
        {
            m_Rear.Next = new Node<T>(nItem, null);
            m_Rear = m_Rear.Next;
        }
        ++m_Length;
    }
    //出队
    public void Out()
    {
        if (this.IsEmpty())
        {
            Console.WriteLine("队列为空...");
            return;
        }
        if (m_Length > 1)
        {
            m_Front = m_Front.Next;
        }
        else
        {
            m_Front = m_Rear = null;
        }
        --m_Length;
    }
    //取对头元素
    public T GetFront()
    {
        return m_Front.Data;
    }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**

 * @文件名:App.cs

 * @作  者:寒风中的细雨

 * @时  间:2012/5/16/

 * @概  述:树的基本操作 模拟递归实现

 */
using System;

//二叉数的结点结构
class BinaryTreeNode<T>
{
    private int m_Num;//后序遍历时候使用
    private T m_Data;
    private BinaryTreeNode<T> m_Lchild;
    private BinaryTreeNode<T> m_Rchild;

    //构造函数
    public BinaryTreeNode(T nData, BinaryTreeNode<T> nLchild,
        BinaryTreeNode<T> nRchild)
    {
        m_Data = nData;
        m_Lchild = nLchild;
        m_Rchild = nRchild;
        m_Num = 0;
    }

    //属性域
    public T Data
    {
        get { return m_Data; }
        set { m_Data = value; }
    }
    public BinaryTreeNode<T> Lchild
    {
        get { return m_Lchild; }
        set { m_Lchild = value; }
    }
    public BinaryTreeNode<T> Rchild
    {
        get { return m_Rchild; }
        set { m_Rchild = value; }
    }
    public int Num
    {
        get { return m_Num; }
        set { m_Num = value; }
    }
}

//二叉树类
class BinaryTree
{
    private BinaryTreeNode<char> m_Head;

    //属性域
    public BinaryTreeNode<char> Head
    {
        get { return m_Head; }
        set { m_Head = value; }
    }

    //构造函数
    public BinaryTree()
    {
        m_Head = null;
    }
      
    //建树 利用字符串ABC@@D@@E@F@@  先序字符串
    public void CreatTreeByString(string nStr)
    {
        LinkStack<BinaryTreeNode<char>> stack = new LinkStack<BinaryTreeNode<char>>();
        int i = 0;
        BinaryTreeNode<char> tmp = null;

        do
        {
            while (nStr[i] != '@')
            {//左孩子
                if (m_Head == null)
                {
                    m_Head = new BinaryTreeNode<char>(nStr[i], null, null);
                    stack.Push(m_Head);
                }
                else
                {
                    tmp = new BinaryTreeNode<char>(nStr[i], null, null);
                    stack.GetTop().Lchild = tmp;
                    stack.Push(tmp);
                }
                ++i;
            }
           
            stack.GetTop().Lchild = null;
            tmp = stack.GetTop();
            stack.Pop();

            ++i;
            if (nStr[i] == '@')
            {//第二个 进行出栈
                tmp.Rchild = null;
                if (stack.IsEmpty())
                {
                    break;
                }
                tmp = stack.GetTop();
                stack.Pop();
                ++i;
                if (nStr[i] == '@')
                {
                    tmp.Rchild = null;
                    ++i;
                    continue;
                }
            }
    
            BinaryTreeNode<char> tmp1 = null;
            tmp1 = new BinaryTreeNode<char>(nStr[i], null, null);
            tmp.Rchild = tmp1;
            stack.Push(tmp1);
            ++i;
        } while (!stack.IsEmpty());
    }

    //先序遍历
    public void PreOrderTraverse()
    {
        LinkStack<BinaryTreeNode<char>> stack = new LinkStack<BinaryTreeNode<char>>();

        if (m_Head == null)
        {
            Console.WriteLine("为空树...");
            return;
        }

        BinaryTreeNode<char> tmp = m_Head;
        do
        {
            while (tmp != null)
            {
                Console.Write("{0} ", tmp.Data);
                stack.Push(tmp);
                tmp = tmp.Lchild;
            }

            tmp = stack.GetTop();
            stack.Pop();
            tmp = tmp.Rchild;
        } while (!stack.IsEmpty() || null !=tmp);
        Console.WriteLine();
    }

    //中序遍历
    public void InOrderTraverse()
    {
        LinkStack<BinaryTreeNode<char>> stack = new LinkStack<BinaryTreeNode<char>>();

        if (m_Head == null)
        {
            Console.WriteLine("为空树...");
            return;
        }

        BinaryTreeNode<char> tmp = m_Head;
        do
        {
            while (tmp != null)
            {
                stack.Push(tmp);
                tmp = tmp.Lchild;
            }

            tmp = stack.GetTop();
            Console.Write("{0} ", tmp.Data);
            stack.Pop();
            tmp = tmp.Rchild;
        } while (!stack.IsEmpty() || null != tmp);
        Console.WriteLine();
    }
   
    //后序遍历
    public void PostOrderTraverse()
    {
        LinkStack<BinaryTreeNode<char>> stack = new LinkStack<BinaryTreeNode<char>>();
        if (m_Head == null)
        {
            Console.WriteLine("为空树...");
            return;
        }
        BinaryTreeNode<char> tmp = m_Head;

        do
        {
            while (tmp != null)
            {
                stack.Push(tmp);
                tmp = tmp.Lchild;
            }

            tmp = stack.GetTop();
            tmp.Num += 1;

            while (!stack.IsEmpty() && stack.GetTop().Num == 2)
            {
                Console.Write("{0} ", stack.GetTop().Data);
                stack.Pop();
                if (!stack.IsEmpty())
                {
                    stack.GetTop().Num += 1;
                }
            }
            if (!stack.IsEmpty())
            {
                tmp = stack.GetTop().Rchild;
            }

        } while (!stack.IsEmpty());
        Console.WriteLine();
    }

    //层序遍历
    public void LevelOrderTraverse()
    {
        LinkQueue<BinaryTreeNode<char>> queue = new LinkQueue<BinaryTreeNode<char>>();

        if (m_Head == null)
        {
            Console.WriteLine("为空树...");
            return;
        }

        BinaryTreeNode<char> tmp = m_Head;
        queue.In(tmp);

        do
        {
            tmp = queue.GetFront();
            Console.Write("{0} ", tmp.Data);
            queue.Out();

            if (tmp.Lchild != null)
            {
                queue.In(tmp.Lchild);
            }
            if (tmp.Rchild != null)
            {
                queue.In(tmp.Rchild);
            }
        } while (!queue.IsEmpty());
        Console.WriteLine();
    }
}

class App
{
    static void Main(string[] args)
    {
        BinaryTree Tree = new BinaryTree();

        Tree.CreatTreeByString("AB@D@@EF@@@");
        Console.WriteLine("先序遍历");
        Tree.PreOrderTraverse();
        Console.WriteLine("中序遍历");
        Tree.InOrderTraverse();
        Console.WriteLine("后序遍历");
        Tree.PostOrderTraverse();
        Console.WriteLine("层序遍历");
        Tree.LevelOrderTraverse();

        Console.WriteLine();

        BinaryTree tree = new BinaryTree();

        tree.CreatTreeByString("ABC@@D@@E@F@@");
        Console.WriteLine("先序遍历");
        tree.PreOrderTraverse();
        Console.WriteLine("中序遍历");
        tree.InOrderTraverse();
        Console.WriteLine("后序遍历");
        tree.PostOrderTraverse();
        Console.WriteLine("层序遍历");
        tree.LevelOrderTraverse();
    }
}

先序遍历
A B D E F
中序遍历
B D A F E
后序遍历
D B F E A
层序遍历
A B E D F

先序遍历
A B C D E F
中序遍历
C B D A E F
后序遍历
C D B F E A
层序遍历
A B E C D F
请按任意键继续. . .
2012-05-16 13:12
快速回复:C#数据结构 开一帖
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032096 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved