模拟循环链表

 最近看有朋友提问关于这方面的问题,我自己也没有亲自作过,凭借在大学时候的一点点数据结构知识,在这里做一个简单的循环链表模拟,希望不是班门弄斧了!呵呵!(感觉还可以再扩充下,继承arraylist来做集合类,在ADD添加元素的时候就指定arraylist里面的最后一个元素是新元素的前驱,而后继是集合的第一个元素.关于如何判断元素形成了环,我的想法是按照后继遍历元素,发现元素重复过的话就认为成环.以后有时间再将这个做法实现下.)

//一.Countclass类,就是元素类

using System;
using System.Collections.Generic;
using System.Text;

namespace Wintest0827
{
    class Countclass
    {
        private int _myvalue;
        private Countclass  _beforeitem;
        private Countclass  _nextitem;
        private bool _isindex = false;
        private bool _islast = false;
        public  Countclass(int thevalue)
        {
            this._myvalue = thevalue;
        }
        public int myvalue
        {
            get
            {
                return _myvalue;
            }
            set
            {
                _myvalue = value;
            }
        }
        public bool  Isindex
        {
            get
            {
                return _isindex;
            }
            set
            {
                _isindex = value;
            }
        }
        public bool  IsLast
        {
            get
            {
                return _islast;
            }
            set
            {
                _islast = value;
            }
        }
        public Countclass  beforeitem
        {
            get
            {
                return _beforeitem;
            }
            set
            {
                _beforeitem = value;
            }
        }
        public Countclass  nextitem
        {
            get
            {
                return _nextitem;
            }
            set
            {
                _nextitem = value;
            }
        }
    }
}

 

//这个是控制元素的集合类Countcoll

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Wintest0827
{
    class Countcoll
    {
        private Countclass Firstitem;
        private Countclass Lastitem;
        private int _FirstIndex;
        private Countclass[] _a;
        public  Countcoll(int[] a)
        {
           
            int count = a.Length;
            _a = new Countclass[count];
            for (int i = 0; i < count; i++)
            {
                Countclass temp = new Countclass(i+1) ;
                //if (i==0)
                //{
                //    temp.Isindex = true;
                //}
                //if (i == count - 1)
                //{
                //    temp.IsLast = true;
                //}
                _a[i] = temp;
            }
            refreshdata();
            this.setindexbyvalue(a[0]);
        }
        private void refreshdata()
        {
            for (int i = 0; i < _a.Length; i++)
            {
                Countclass item = (Countclass)_a[i];
                //Countclass nextitem;
                if (i == 0)
                {
                    item.beforeitem = (Countclass)_a[_a.Length - 1];
                }
                else
                {
                    item.beforeitem = (Countclass)_a[i - 1];
                }
                if (i == _a.Length - 1)
                {
                    item.nextitem = (Countclass)_a[0];
                }
                else
                {
                    item.nextitem = (Countclass)_a[i+1];
                }
            }
        }
        public  void setindexbyvalue(int value)
        {
            Countclass item=null;
            int lastindex;
            for (int i = 0; i < _a.Length-1; i++)
            {
                item = (Countclass)_a[i];

                if (item.myvalue ==value)
                {
                    this.Firstitem = item;
                    this._FirstIndex = i;
                    if (i - 1 < 0)
                    {
                        lastindex = _a.Length-1;
                    }
                    else
                    {
                        lastindex = i - 1;
                    }
                    this.Lastitem = _a[lastindex];
                    return ;
                }
            }
            this.Firstitem = _a[0];
        }
        public void Setnextindex()
        {
            if (this._FirstIndex == _a.Length - 1)
            {
                this._FirstIndex = 0;
                this.Firstitem = _a[0];
            }
            else
            {
                this.Firstitem = _a[this._FirstIndex + 1];
                this._FirstIndex++;
            }
          
          
        }
        public int Getitemindex
        {
            get
            {
                return this._FirstIndex ;
            }
        }
        public Countclass Getitembyindex
        {
            get
            {
                return Firstitem;
            }
        }
        public string  printvalue()
        {
            string returnstring = "";
            returnstring += Firstitem.myvalue;
            int j=1;
            Countclass item=null;
            //因为firstitem已经设置了值,所以循环次数为_a.length-1
            for (int i = 0; i < _a.Length-1; i++)
            {
                if (i == 0)
                {
                    item = Firstitem;
                }
                if (j == 3)
                {
                    returnstring += "/n";
                    j = 1;
                }
                else
                {
                    j++;
                }
                returnstring += item.nextitem.myvalue;
                item = item.nextitem;
            }
            return returnstring;
        }
    }
}

//测试的调用方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wintest0827
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            x = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            coll = new Countcoll(x);
        }
        private int[] x;
        private Countcoll coll;
        private void button1_Click(object sender, EventArgs e)
        {
            caculatevalue();
        }
        private void caculatevalue()
        {
           string a =coll.printvalue();
           MessageBox.Show(a);
           //Countclass currentitem = coll.Getitembyindex;
           //coll.setindexbyvalue(currentitem.myvalue+1);
           coll.Setnextindex();
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值