Stack Task 02/15 O(n) Time complexity

这段代码展示了如何用C#实现一个基本的栈数据结构,包括初始化、压栈、弹栈、查看栈顶元素和显示所有元素等功能。用户可以输入数值进行压栈和弹栈操作,最后显示当前栈中元素的个数及内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

namespace ArrayStack
{
    class MyStack
    {
        public int[] array;
        public int max;
        public int count = 0;
        public MyStack(int size)
        {
            array = new int[size];
            max = size;
        }
        /// <summary>
        /// the number of elements in the stack
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return count;
        }
        /// <summary>
        /// get the top element of the stack
        /// </summary>
        /// <exception cref="Exception"></exception>
        public void Peak()
        {
            if (count == 0)
            {
                throw new Exception("no elements in stack");
            }
            Console.WriteLine("Peaked element now is: " + array[count-1]);

        }
        /// <summary>
        /// get the top element of the stack, and remove it from the stack
        /// </summary>
        /// <exception cref="Exception"></exception>
        public void Pop()
        {
            if (count == 0)
            {
                throw new Exception("no elements in stack");
            }
            Console.WriteLine("poped element is:" + array[count - 1]);
            int[] NewArray = new int[count];
            for (int i = count -2; i >=0 ; i--)
                NewArray[i] = array[i]; //
            array = NewArray;
            count--;
        }
        /// <summary>
        /// Add one element into the stack, take O(1) time.
        /// </summary>
        /// <param name="item">what you want to add</param>
        /// <exception cref="Exception"></exception>
        public void Push(int item)
        {
            if (max == 0)
            {
                throw new Exception("insufficient length of stack");
            }
            else
            {
                array[count] = item;
                count++;
            }

        }
        /// <summary>
        /// display the elements in the stack
        /// </summary>
        public void display() //display the list
        {
            Console.WriteLine("your stack now contains:");
            Console.WriteLine(" ");
            for (int j = 0; j < array.Length; j++)
            {
                Console.WriteLine(array[j]);
            }
            Console.ReadLine();
        }
    }
    static class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Input the length of your stack:");
            int StackLength = int.Parse(Console.ReadLine());
            MyStack Stack = new MyStack(StackLength);
            Console.WriteLine(" ");

            Console.Write("how many numbers you want to push?");
            int num = int.Parse(Console.ReadLine());
            Console.WriteLine(" ");
            if (num > StackLength)
            {
                Console.Write("Numbers cannot exceed your stack length, reinput numbers:");
                num = int.Parse(Console.ReadLine());
            }
            //添加元素
            for (int k = 0; k < num; k++)
            {
                Console.Write("A number you want to push to your stack?");
                int newElement = int.Parse(Console.ReadLine());
                Stack.Push(newElement); //添加元素
            }
            Stack.display();
            Stack.Peak();

            Console.Write("how many numbers you want to pop?");
            int num_2 = int.Parse(Console.ReadLine());
            Console.WriteLine(" ");
            for (int j = 0; j < num_2; j++)
            {
                Stack.Pop(); //删除最上层元素
            }
            Console.WriteLine("Count of numbers in stack is: " + Stack.Count());
            Stack.display();

            Console.Write("Let's push another number into the stack:");
            int newElement_2 = int.Parse(Console.ReadLine());
            Stack.Push(newElement_2);

            Stack.display();
            Console.WriteLine("Count of numbers in stack is: " + Stack.Count());


        }
    }
    
}

Result:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值