c#之链栈

1,创建链栈的节点  LinkNode

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 栈
{
    /// <summary>
    /// 链栈的节点
    /// </summary>
    /// <typeparam name="T"></typeparam>
    class LinkNode<T>
    {
        private T data;//存储数据
        private LinkNode<T> next;//指向下一节点
        public LinkNode()
        {
            data = default(T);
            next = null;
        }
        public LinkNode(T data)
        {
            this.data = data;
            next = null;
        }
        public LinkNode(T data, LinkNode<T> next)
        {
            this.data = data;
            this.next = next;
        }
        public LinkNode(LinkNode<T> next)
        {
            this.next = next;
            data = default(T);
        }
        public T Data { get { return data; } set { data = value; } }
        public LinkNode<T> Next { get { return next; } set { next = value; } }

    }
}

2,创建栈的接口   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 栈
{
    //数序栈
    interface ISeqStack<T>
    {
        int Count { get; }//元素个数(属性)
        int GetLenth();//栈的长度(方法)
        bool IsEmpty();//栈是否为空
        void Clear();//清空栈
        void Push(T item);//入栈
        T Pop();//出栈
        T PeekTop();//取得栈顶元素
    }
}

3,创建链栈

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 栈
{
    class LinkStack<T> : ISeqStack<T>
    {
        private LinkNode<T> top;//栈顶元素节点
        private int count = 0;//记录栈的元素个数

        /// <summary>
        /// 栈中元素的个数
        /// </summary>
        public int Count
        {
            get { return count; }
        }
        public int GetLenth()
        {
            return count;
        }

        public bool IsEmpty()
        {
            return count == 0;
        }

        public void Clear()
        {
            count = 0;
            top = null;
        }

        public void Push(T item)
        {
            //栈顶元素
            LinkNode<T> newNode = new LinkNode<T>(item);
            newNode.Next = top;
            top = newNode;
            count++;

        }

        public T Pop()
        {
            T data = top.Data;
            top = top.Next;
            count--;
            return data;
        }

        public T PeekTop()
        {
            return top.Data;
        }
    }
}
4,测试链栈

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 栈
{
    class Program
    {
        static void Main(string[] args)
        {
            //ISeqStack<char> s = new SeqStack<char>(30);
            ISeqStack<char> s = new LinkStack<char>();

            s.Push('a');
            s.Push('b');
            s.Push('c');
            s.Push('d');
            Console.WriteLine("元素 a,b,c,d  入栈之后,栈的元素个数:" + s.Count + "  栈的长度:" + s.GetLenth());
            Console.WriteLine("栈顶元素:" + s.PeekTop()); s.PeekTop();
            Console.WriteLine("元素 " + s.Pop() + " 出栈之后,栈顶元素:" + s.PeekTop());
            Console.WriteLine("元素 " + s.Pop() + " 出栈之后,栈顶元素:" + s.PeekTop());
            s.Push('e');
            s.Push('f');
            s.Push('g');
            Console.WriteLine("元素 e,f,g  入栈之后,栈的元素个数:" + s.Count + "  栈顶元素:" + s.PeekTop());
            Console.WriteLine("元素 " + s.Pop() + ", " + s.Pop() + " 出栈之后,栈顶元素:" + s.PeekTop());
            s.Clear();
            Console.WriteLine("清空栈之后,栈的元素个数:" + s.Count + "  栈的长度:" + s.GetLenth());
            Console.ReadKey();
        }
    }
}
5,运行结果






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C# 链栈实现数制转换的示例代码: ```csharp using System; // 定义链栈节点 class Node { public int data; // 节点数据 public Node next; // 下一个节点指针 public Node(int data) { this.data = data; this.next = null; } } // 定义链栈类 class LinkStack { private Node top; // 顶节点指针 public LinkStack() { this.top = null; } // 判断是否为空 public bool IsEmpty() { return this.top == null; } // 入 public void Push(int data) { Node newNode = new Node(data); newNode.next = this.top; this.top = newNode; } // 出 public int Pop() { if (this.IsEmpty()) { Console.WriteLine("已空!"); return -1; } int data = this.top.data; this.top = this.top.next; return data; } // 获取顶元素 public int GetTop() { if (this.IsEmpty()) { Console.WriteLine("已空!"); return -1; } return this.top.data; } } class Program { static void Main(string[] args) { int num, baseNum; Console.Write("请输入一个十进制整数:"); num = int.Parse(Console.ReadLine()); Console.Write("请输入要转换的进制数(2-9):"); baseNum = int.Parse(Console.ReadLine()); LinkStack stack = new LinkStack(); while (num > 0) { int remainder = num % baseNum; stack.Push(remainder); num /= baseNum; } Console.Write("转换后的结果为:"); while (!stack.IsEmpty()) { Console.Write(stack.Pop()); } Console.WriteLine(); } } ``` 运行程序,输入一个十进制整数和要转换的进制数,程序会使用链栈进行进制转换并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值