《数据结构》C#编程实例(二)

s2332049.jpg

《数据结构》学习笔记(二)中JavaScript程序代码实现的栈操作实例为范例用C#语言编程重写:

Stack s = new Stack();
s.Push(1);
s.Push(2);
s.Push(2);
s.Push(2);
s.Push(null);
object b = s.Pop();
Console.WriteLine("s 的Pop方法: " + b);
foreach(int j in s)
{
	Console.WriteLine("s: " + j);
}
s.Push(3);
foreach(int k in s)
{
	Console.WriteLine("Push方法后的 s: " + k);
}

队列操作实例:

Queue s = new Queue();
s.Enqueue(1);
s.Enqueue(2);
s.Enqueue(2);
s.Enqueue(2);

object b = s.Dequeue();
Console.WriteLine("s 的Dequeue方法: " + b);
foreach(int j in s)
{
	Console.WriteLine("s: " + j);
}
s.Enqueue(3);
foreach(int k in s)
{
	Console.WriteLine("Enqueue方法后的 s: " + k);
}

注:Stack类、Queue类都位于System.Collections中。

上面的重写可以说明Push()方法和Pop()方法是栈(Stack)的两个重要操作,与之对应的队列(Queue)的两个操作分别为Enqueue()方法和Dequeue()方法。接下来,用C#编程实现在《数据结构》3.2节:栈的应用举例

3.2.1 数制转换

void conversion()
{
    //构造空栈
    Stack s = new Stack();
    int n;
    int result;
    Int32.TryParse(Console.ReadLine(), out result);
    n = result;
    while (n != 0)
    {
        s.Push(n % 8);
        n = n / 8;
    }
    while (s.Count != 0)
    {
        object e = s.Pop();
        Console.Write(e);
    }
}

下面是泛型编程实现的完整程序代码:

using System;
using System.Collections.Generic;

class StackDemo
{
    static void conversion()
    {
        //构造空栈
        Stack<int> s = new Stack<int>();
        int n;
        int result;
        Int32.TryParse(Console.ReadLine(), out result);
        n = result;
        while (n != 0)
        {
            s.Push(n % 8);
            n = n / 8;
        }
        while (s.Count != 0)
        {
            int e = s.Pop();
            Console.Write(e);
        }
    }
    static void Main(string[] args)
    {
        conversion();
    }
}

3.2.3 行编辑程序 书上是用栈实现,在这儿本人用JavaScript语言编程实现如下:

for(i=0;i<ch.length;i++)
{
    if(tb1.value.length > 0 && ch[i] != '\n')
    {
        switch(ch[i])
        {
            case '#': 
                list.pop();
                break;
            case '@':
                list = [];
                break;
            default:
                list.push(ch[i]);
                break;
        }
    }
}
alert(list.join(''));

上面的代码是在网页上的文本框输入多行文本,点击提交按钮出现程序处理后的内容。下面的代码是用C#语言编程示例。

Stack<char> s = new Stack<char>();
char[] ch = new char[tb1.Text.Length];
ch = tb1.Text.ToCharArray();

for (int i = 0; i < ch.Length; i++)
{
    if (tb1.Text.Length > 0 && ch[i] != '\n')
    {
        switch (ch[i])
        {
            case '#':
                s.Pop();
                break;
            case '@':
                s.Clear();
                break;
            default:
                s.Push(ch[i]);
                break;
        }
    }
}
List<char> ch2 = new List<char>();
IEnumerator enu = s.GetEnumerator();
while(enu.MoveNext())
{
    ch2.Add(char.Parse(enu.Current.ToString()));
}
string sb = "";
foreach (char c in ch2)
{
    sb = sb.Insert(0, c.ToString());
}
Response.Write(sb);
s.Clear();

在上面的代码看后面的暴值是采用得到一个IEnumerator对象的方式访问,这样访问的好处不用把它们从栈中弹出(即Pop()操作方法)。

我们来看看汉诺(Hanoi)塔游戏: http://chemeng.p.lodz.pl/zylla/games/hanoi3e.html

下面是C#编程实现3.3节汉诺塔问题的程序代码:
using System;

class Program
{
    //计数
    static int count = 0;
    static void move(byte n, char x, char y)
    {
        Console.WriteLine("将 {0} 号从 {1} 移到 {2}", n, x, y);
    }
    //Hanoi 方法
    static void Hanoi(byte n, char x, char y, char z)
    {
        
        if (n == 1)
        {
            ++count;
            //将编号为1的圆盘从x移到z
            move(n, x, z);
        }
        else
        {
            ++count;
            //将x上编号为1至n-1的圆盘移到y,z作中间塔
            Hanoi((byte)(n - 1), x, z, y);
            //将编号为n的圆盘从x移到z
            move(n, x, z);
            //将y上编号为1至n-1的圆盘移到z,x作中间塔
            Hanoi((byte)(n - 1), y, x ,z);
        }
    }
    static void Main(string[] args)
    {
        //n为3时
        Hanoi(3, 'x', 'y', 'z');
        Console.WriteLine(count);
    }
}

转载于:https://www.cnblogs.com/georgewing/archive/2009/05/22/1487300.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值