迎接2012之集合和泛型(3)------栈的顺序结构基本实现

 一、非泛型实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack s = new Stack(3);
            s.Push(3);
            s.Push(4);
            s.Push(5);
            for (int i = 0; i < s.array.Length; i++)
            {
                Console.WriteLine(s.array[i]);
            }
            Console.WriteLine(s.Peek());
            Console.WriteLine(s.Pop());
        }
    }
    /// <summary>
    /// 栈的顺序结构基本实现(非泛型)
    /// </summary>
    public class Stack
    {
        /// <summary>
        /// 存放元素的数组
        /// </summary>
        public object[] array;
        /// <summary>
        /// 元素的下标
        /// </summary>
        public int index;
        /// <summary>
        /// 默认的构造函数
        /// </summary>
        public Stack()
        {
            this.array = new object[10];
            this.index = 0;
        }
        /// <summary>
        /// 构造函数重载
        /// </summary>
        /// <param name="capcity"></param>
        public Stack(int count)
        {
            this.array = new object[count];
            this.index = 0;
        }
        /// <summary>
        /// 扩大容器
        /// </summary>
        private void Dilatation()
        {
            //将数组个数扩大为原来的两倍
            object[] newarray = new object[this.index * 2];
            for (int i = 0; i < this.array.Length; i++)
            {
                newarray[i] = this.array[i];
            }
            this.array = newarray;
        }
        /// <summary>
        /// 入栈
        /// </summary>
        /// <param name="obj"></param>
        public void Push(object obj)
        {
            //判断是否为满,满的话扩容,最后就是添加元素
            if (index==array.Length)
            {
                Dilatation();
            }
            this.array[index] = obj;
            this.index++;
        }
        /// <summary>
        /// 出栈
        /// </summary>
        public object Pop()
        {
            if (index!=0)
            {
                index--;
                return array[index];
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 查找元素是否存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Contains(object obj)
        {
            bool b = false;
            for (int i = 0; i < this.array.Length; i++)
            {
                if (this.array[i].Equals(obj))
                {
                    b = true;
                    break;
                }
            }
            return b;
        }
        /// <summary>
        /// 取栈顶
        /// </summary>
        public object Peek()
        {
            if (index!=0)
            {
                return array[index - 1];
            }
            else
            {
                return "";
            }
        }
    }

}

二、泛型实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack<int> s = new Stack<int>(3);
            s.Push(3);
            s.Push(4);
            s.Push(5);
            for (int i = 0; i < s.array.Length; i++)
            {
                Console.WriteLine(s.array[i]);
            }
            Console.WriteLine(s.Peek());
            Console.WriteLine(s.Pop());
        }
    }
    /// <summary>
    /// 栈的顺序结构基本实现(泛型)
    /// </summary>
    public class Stack<T>
    {
        /// <summary>
        /// 存放元素的数组
        /// </summary>
        public T[] array;
        /// <summary>
        /// 元素的下标
        /// </summary>
        public int index;
        /// <summary>
        /// 默认的构造函数
        /// </summary>
        public Stack()
        {
            this.array = new T[10];
            this.index = 0;
        }
        /// <summary>
        /// 构造函数重载
        /// </summary>
        /// <param name="capcity"></param>
        public Stack(int count)
        {
            this.array = new T[count];
            this.index = 0;
        }
        /// <summary>
        /// 扩大容器
        /// </summary>
        private void Dilatation()
        {
            //将数组个数扩大为原来的两倍
            T[] newarray = new T[this.index * 2];
            for (int i = 0; i < this.array.Length; i++)
            {
                newarray[i] = this.array[i];
            }
            this.array = newarray;
        }
        /// <summary>
        /// 入栈
        /// </summary>
        /// <param name="obj"></param>
        public void Push(T obj)
        {
            //判断是否为满,满的话扩容,最后就是添加元素
            if (index==array.Length)
            {
                Dilatation();
            }
            this.array[index] = obj;
            this.index++;
        }
        /// <summary>
        /// 出栈
        /// </summary>
        public T Pop()
        {
            if (index == 0)
            {
                Console.WriteLine("元素不存在");
            }
            index--;
            return array[index];

        }
        /// <summary>
        /// 查找元素是否存在
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Contains(T obj)
        {
            bool b = false;
            for (int i = 0; i < this.array.Length; i++)
            {
                if (this.array[i].Equals(obj))
                {
                    b = true;
                    break;
                }
            }
            return b;
        }
        /// <summary>
        /// 取栈顶
        /// </summary>
        public T Peek()
        {
            if (index==0)
            {
                Console.WriteLine("元素不存在");
            }
            return array[index - 1];

        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值