码迷,mamicode.com
首页 > 其他好文 > 详细

C#数据结构-单链表

时间:2014-05-29 16:36:06      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

   理论基础:

    链表是用一组任意的存储单元来存储线性表中的数据元素

    如果结点的引用域只存储该结点直接后继结点的存储地址,则该链表叫单链表(Singly Linked List)。

    单链表由头引用H唯一确定。头引用指向单链表的第一个结点,也就是把单链表第一个结点的地址放在H中。

   C#实现:

    1接口

         引用线性表的接口IListDS<T>

    2实现

         首先,必须定义一个单链表的节点类。

bubuko.com,布布扣
bubuko.com,布布扣
 1bubuko.com,布布扣 public class Node<T>
 2bubuko.com,布布扣    {
 3bubuko.com,布布扣        private T data;        //数据域
 4bubuko.com,布布扣        private Node<T> next;  //引用域
 5bubuko.com,布布扣
 6bubuko.com,布布扣        
 7bubuko.com,布布扣        public Node(T val)
 8bubuko.com,布布扣        {
 9bubuko.com,布布扣            data = val;
10bubuko.com,布布扣            next = null;
11bubuko.com,布布扣        }

12bubuko.com,布布扣
13bubuko.com,布布扣        public Node()
14bubuko.com,布布扣        {
15bubuko.com,布布扣            data = default(T);
16bubuko.com,布布扣            next = null;
17bubuko.com,布布扣        }

18bubuko.com,布布扣
19bubuko.com,布布扣        public T Data
20bubuko.com,布布扣        {
21bubuko.com,布布扣            get
22bubuko.com,布布扣            {
23bubuko.com,布布扣                return data;
24bubuko.com,布布扣            }

25bubuko.com,布布扣            set
26bubuko.com,布布扣            {
27bubuko.com,布布扣                data = value;
28bubuko.com,布布扣            }

29bubuko.com,布布扣        }

30bubuko.com,布布扣        public Node<T> Next
31bubuko.com,布布扣        {
32bubuko.com,布布扣            get
33bubuko.com,布布扣            {
34bubuko.com,布布扣                return next;
35bubuko.com,布布扣            }

36bubuko.com,布布扣            set
37bubuko.com,布布扣            {
38bubuko.com,布布扣                next = value;
39bubuko.com,布布扣            }

40bubuko.com,布布扣        }

41bubuko.com,布布扣
42bubuko.com,布布扣
43bubuko.com,布布扣    }
bubuko.com,布布扣

     实现主体类

     Append,Insert,InsertBack三个方法实质上都是插入操作,可以考虑用overload或者override来实现,有兴趣的朋友试试。

bubuko.com,布布扣
bubuko.com,布布扣
  1bubuko.com,布布扣public class LinkList<T> : IListDS<T>
  2bubuko.com,布布扣    {
  3bubuko.com,布布扣        private Node<T> head;
  4bubuko.com,布布扣        public Node<T> Head
  5bubuko.com,布布扣        {
  6bubuko.com,布布扣            get
  7bubuko.com,布布扣            {
  8bubuko.com,布布扣                return head;
  9bubuko.com,布布扣            }

 10bubuko.com,布布扣            set
 11bubuko.com,布布扣            {
 12bubuko.com,布布扣                head = value;
 13bubuko.com,布布扣            }

 14bubuko.com,布布扣        }

 15bubuko.com,布布扣        public LinkList()
 16bubuko.com,布布扣        {
 17bubuko.com,布布扣            head = null;
 18bubuko.com,布布扣        }

 19bubuko.com,布布扣
 20bubuko.com,布布扣        /// <summary>
 21bubuko.com,布布扣        /// 获取长度
 22bubuko.com,布布扣        /// </summary>
 23bubuko.com,布布扣        /// <returns></returns>

 24bubuko.com,布布扣        public int GetLength()
 25bubuko.com,布布扣        {
 26bubuko.com,布布扣            Node<T> p = head;
 27bubuko.com,布布扣            int len = 0;
 28bubuko.com,布布扣            while (p != null)
 29bubuko.com,布布扣            {
 30bubuko.com,布布扣                ++len;
 31bubuko.com,布布扣                p = p.Next;
 32bubuko.com,布布扣            }

 33bubuko.com,布布扣            return len;
 34bubuko.com,布布扣        }

 35bubuko.com,布布扣
 36bubuko.com,布布扣        /// <summary>
 37bubuko.com,布布扣        /// 清空操作
 38bubuko.com,布布扣        /// </summary>

 39bubuko.com,布布扣        public void Clear()
 40bubuko.com,布布扣        {
 41bubuko.com,布布扣            head = null;
 42bubuko.com,布布扣        }

 43bubuko.com,布布扣
 44bubuko.com,布布扣        /// <summary>
 45bubuko.com,布布扣        /// 判断线性表是否为空
 46bubuko.com,布布扣        /// </summary>
 47bubuko.com,布布扣        /// <returns></returns>

 48bubuko.com,布布扣        public bool IsEmpty()
 49bubuko.com,布布扣        {
 50bubuko.com,布布扣            if (head == null)
 51bubuko.com,布布扣            {
 52bubuko.com,布布扣                return true;
 53bubuko.com,布布扣            }

 54bubuko.com,布布扣            else
 55bubuko.com,布布扣            {
 56bubuko.com,布布扣                return false;
 57bubuko.com,布布扣            }

 58bubuko.com,布布扣        }

 59bubuko.com,布布扣
 60bubuko.com,布布扣        /// <summary>
 61bubuko.com,布布扣        /// 附加操作,线性表未满,将值为item的新元素添加到末尾
 62bubuko.com,布布扣        /// </summary>
 63bubuko.com,布布扣        /// <param name="item"></param>

 64bubuko.com,布布扣        public void Append(T item)
 65bubuko.com,布布扣        {
 66bubuko.com,布布扣            Node<T> newNode = new Node<T>(item);  //根据元素创建新的节点
 67bubuko.com,布布扣            Node<T> node = new Node<T>();
 68bubuko.com,布布扣
 69bubuko.com,布布扣            if (head == null)
 70bubuko.com,布布扣            {
 71bubuko.com,布布扣                head = newNode;
 72bubuko.com,布布扣                return;
 73bubuko.com,布布扣            }

 74bubuko.com,布布扣            node = head;
 75bubuko.com,布布扣            while (node.Next != null)
 76bubuko.com,布布扣            {
 77bubuko.com,布布扣                node = node.Next;
 78bubuko.com,布布扣            }

 79bubuko.com,布布扣            node.Next = newNode;
 80bubuko.com,布布扣
 81bubuko.com,布布扣        }

 82bubuko.com,布布扣
 83bubuko.com,布布扣        /// <summary>
 84bubuko.com,布布扣        /// 寻找节点
 85bubuko.com,布布扣        /// </summary>
 86bubuko.com,布布扣        /// <param name="i"></param>
 87bubuko.com,布布扣        /// <returns></returns>

 88bubuko.com,布布扣        public Node<T> FindNode(int i)
 89bubuko.com,布布扣        {
 90bubuko.com,布布扣            if (IsEmpty())
 91bubuko.com,布布扣            {
 92bubuko.com,布布扣                Console.Write("List is empty");
 93bubuko.com,布布扣                return null;
 94bubuko.com,布布扣            }

 95bubuko.com,布布扣            if (i < 1)
 96bubuko.com,布布扣            {
 97bubuko.com,布布扣                Console.Write("Index is error");
 98bubuko.com,布布扣                return null;
 99bubuko.com,布布扣            }

100bubuko.com,布布扣            Node<T> current = head;
101bubuko.com,布布扣            int j = 1;
102bubuko.com,布布扣
103bubuko.com,布布扣            while (current.Next != null && j < i)
104bubuko.com,布布扣            {
105bubuko.com,布布扣                ++j;
106bubuko.com,布布扣                current = current.Next;
107bubuko.com,布布扣            }

108bubuko.com,布布扣            return current;
109bubuko.com,布布扣        }

110bubuko.com,布布扣
111bubuko.com,布布扣        /// <summary>
112bubuko.com,布布扣        /// 插入操作,在第i个节点前面插入item
113bubuko.com,布布扣        /// </summary>
114bubuko.com,布布扣        /// <param name="item"></param>
115bubuko.com,布布扣        /// <param name="i"></param>

116bubuko.com,布布扣        public void Insert(T item, int i)
117bubuko.com,布布扣        {
118bubuko.com,布布扣            Node<T> newNode = new Node<T>(item);
119bubuko.com,布布扣            Node<T> node = new Node<T>();
120bubuko.com,布布扣            Node<T> current = FindNode(i);
121bubuko.com,布布扣            if (current != null)
122bubuko.com,布布扣            {
123bubuko.com,布布扣                node = current;       //对目标节点备份
124bubuko.com,布布扣                newNode.Next = current;
125bubuko.com,布布扣                node.Next = newNode;
126bubuko.com,布布扣            }

127bubuko.com,布布扣        }

128bubuko.com,布布扣
129bubuko.com,布布扣        /// <summary>
130bubuko.com,布布扣        /// 插入操作,在第i个节点后面插入item
131bubuko.com,布布扣        /// </summary>
132bubuko.com,布布扣        /// <param name="item"></param>
133bubuko.com,布布扣        /// <param name="i"></param>

134bubuko.com,布布扣        public void InsertBack(T item, int i)
135bubuko.com,布布扣        {
136bubuko.com,布布扣            Node<T> newNode = new Node<T>(item);
137bubuko.com,布布扣            Node<T> current = FindNode(i);
138bubuko.com,布布扣            if (current != null)
139bubuko.com,布布扣            {
140bubuko.com,布布扣                newNode.Next = current.Next;
141bubuko.com,布布扣                current.Next = newNode;
142bubuko.com,布布扣            }

143bubuko.com,布布扣        }

144bubuko.com,布布扣
145bubuko.com,布布扣        /// <summary>
146bubuko.com,布布扣        /// 删除操作
147bubuko.com,布布扣        /// </summary>
148bubuko.com,布布扣        /// <param name="i"></param>
149bubuko.com,布布扣        /// <returns></returns>

150bubuko.com,布布扣        public T Delete(int i)
151bubuko.com,布布扣        {
152bubuko.com,布布扣            Node<T> current = FindNode(i);
153bubuko.com,布布扣            Node<T> node = new Node<T>();
154bubuko.com,布布扣            if (current != null)
155bubuko.com,布布扣            {
156bubuko.com,布布扣                node = current;   //对目标节点备份
157bubuko.com,布布扣                node.Next = current.Next;
158bubuko.com,布布扣                return current.Data;
159bubuko.com,布布扣            }

160bubuko.com,布布扣            else
161bubuko.com,布布扣            {
162bubuko.com,布布扣                Console.Write("the node is not exist!");
163bubuko.com,布布扣                return default(T);
164bubuko.com,布布扣            }

165bubuko.com,布布扣        }

166bubuko.com,布布扣
167bubuko.com,布布扣        /// <summary>
168bubuko.com,布布扣        /// 去表元
169bubuko.com,布布扣        /// </summary>
170bubuko.com,布布扣        /// <param name="i"></param>
171bubuko.com,布布扣        /// <returns></returns>

172bubuko.com,布布扣        public T GetElem(int i)
173bubuko.com,布布扣        {
174bubuko.com,布布扣            Node<T> current = FindNode(i);
175bubuko.com,布布扣
176bubuko.com,布布扣            if (current != null)
177bubuko.com,布布扣            {
178bubuko.com,布布扣                return current.Data;
179bubuko.com,布布扣            }

180bubuko.com,布布扣            else
181bubuko.com,布布扣            {
182bubuko.com,布布扣                Console.Write("the node is not exist!");
183bubuko.com,布布扣                return default(T);
184bubuko.com,布布扣            }

185bubuko.com,布布扣        }

186bubuko.com,布布扣
187bubuko.com,布布扣        /// <summary>
188bubuko.com,布布扣        /// 按值查找
189bubuko.com,布布扣        /// </summary>
190bubuko.com,布布扣        /// <param name="value"></param>
191bubuko.com,布布扣        /// <returns></returns>

192bubuko.com,布布扣        public int Locate(T value)
193bubuko.com,布布扣        {
194bubuko.com,布布扣            if (IsEmpty())
195bubuko.com,布布扣            {
196bubuko.com,布布扣                Console.WriteLine("List is Empty!");
197bubuko.com,布布扣                return -1;
198bubuko.com,布布扣            }

199bubuko.com,布布扣            Node<T> current = new Node<T>();
200bubuko.com,布布扣            current = head;
201bubuko.com,布布扣            int i = 1;
202bubuko.com,布布扣            while (current.Next != null && !current.Data.Equals(value))
203bubuko.com,布布扣            {
204bubuko.com,布布扣                current = current.Next;
205bubuko.com,布布扣                ++i;
206bubuko.com,布布扣            }

207bubuko.com,布布扣            return i;
208bubuko.com,布布扣        }

209bubuko.com,布布扣    }
bubuko.com,布布扣

碰到的问题:1 方法参数为int类型,改用object类型碰到一些问题,查找节点的时候,遍历整个链表,如何对比参数节点

                   对比参数会不会影响算法的时间复杂度

                2 append,insert,insertback方法本质上都是插入操作,有没有更好的方法实现 

代码没有经过测试,有兴趣的朋友可以试试,有问题,告知一下!   

   理论基础:

    链表是用一组任意的存储单元来存储线性表中的数据元素

    如果结点的引用域只存储该结点直接后继结点的存储地址,则该链表叫单链表(Singly Linked List)。

    单链表由头引用H唯一确定。头引用指向单链表的第一个结点,也就是把单链表第一个结点的地址放在H中。

   C#实现:

    1接口

         引用线性表的接口IListDS<T>

    2实现

         首先,必须定义一个单链表的节点类。

bubuko.com,布布扣
bubuko.com,布布扣
 1bubuko.com,布布扣 public class Node<T>
 2bubuko.com,布布扣    {
 3bubuko.com,布布扣        private T data;        //数据域
 4bubuko.com,布布扣        private Node<T> next;  //引用域
 5bubuko.com,布布扣
 6bubuko.com,布布扣        
 7bubuko.com,布布扣        public Node(T val)
 8bubuko.com,布布扣        {
 9bubuko.com,布布扣            data = val;
10bubuko.com,布布扣            next = null;
11bubuko.com,布布扣        }

12bubuko.com,布布扣
13bubuko.com,布布扣        public Node()
14bubuko.com,布布扣        {
15bubuko.com,布布扣            data = default(T);
16bubuko.com,布布扣            next = null;
17bubuko.com,布布扣        }

18bubuko.com,布布扣
19bubuko.com,布布扣        public T Data
20bubuko.com,布布扣        {
21bubuko.com,布布扣            get
22bubuko.com,布布扣            {
23bubuko.com,布布扣                return data;
24bubuko.com,布布扣            }

25bubuko.com,布布扣            set
26bubuko.com,布布扣            {
27bubuko.com,布布扣                data = value;
28bubuko.com,布布扣            }

29bubuko.com,布布扣        }

30bubuko.com,布布扣        public Node<T> Next
31bubuko.com,布布扣        {
32bubuko.com,布布扣            get
33bubuko.com,布布扣            {
34bubuko.com,布布扣                return next;
35bubuko.com,布布扣            }

36bubuko.com,布布扣            set
37bubuko.com,布布扣            {
38bubuko.com,布布扣                next = value;
39bubuko.com,布布扣            }

40bubuko.com,布布扣        }

41bubuko.com,布布扣
42bubuko.com,布布扣
43bubuko.com,布布扣    }
bubuko.com,布布扣

     实现主体类

     Append,Insert,InsertBack三个方法实质上都是插入操作,可以考虑用overload或者override来实现,有兴趣的朋友试试。

bubuko.com,布布扣
bubuko.com,布布扣
  1bubuko.com,布布扣public class LinkList<T> : IListDS<T>
  2bubuko.com,布布扣    {
  3bubuko.com,布布扣        private Node<T> head;
  4bubuko.com,布布扣        public Node<T> Head
  5bubuko.com,布布扣        {
  6bubuko.com,布布扣            get
  7bubuko.com,布布扣            {
  8bubuko.com,布布扣                return head;
  9bubuko.com,布布扣            }

 10bubuko.com,布布扣            set
 11bubuko.com,布布扣            {
 12bubuko.com,布布扣                head = value;
 13bubuko.com,布布扣            }

 14bubuko.com,布布扣        }

 15bubuko.com,布布扣        public LinkList()
 16bubuko.com,布布扣        {
 17bubuko.com,布布扣            head = null;
 18bubuko.com,布布扣        }

 19bubuko.com,布布扣
 20bubuko.com,布布扣        /// <summary>
 21bubuko.com,布布扣        /// 获取长度
 22bubuko.com,布布扣        /// </summary>
 23bubuko.com,布布扣        /// <returns></returns>

 24bubuko.com,布布扣        public int GetLength()
 25bubuko.com,布布扣        {
 26bubuko.com,布布扣            Node<T> p = head;
 27bubuko.com,布布扣            int len = 0;
 28bubuko.com,布布扣            while (p != null)
 29bubuko.com,布布扣            {
 30bubuko.com,布布扣                ++len;
 31bubuko.com,布布扣                p = p.Next;
 32bubuko.com,布布扣            }

 33bubuko.com,布布扣            return len;
 34bubuko.com,布布扣        }

 35bubuko.com,布布扣
 36bubuko.com,布布扣        /// <summary>
 37bubuko.com,布布扣        /// 清空操作
 38bubuko.com,布布扣        /// </summary>

 39bubuko.com,布布扣        public void Clear()
 40bubuko.com,布布扣        {
 41bubuko.com,布布扣            head = null;
 42bubuko.com,布布扣        }

 43bubuko.com,布布扣
 44bubuko.com,布布扣        /// <summary>
 45bubuko.com,布布扣        /// 判断线性表是否为空
 46bubuko.com,布布扣        /// </summary>
 47bubuko.com,布布扣        /// <returns></returns>

 48bubuko.com,布布扣        public bool IsEmpty()
 49bubuko.com,布布扣        {
 50bubuko.com,布布扣            if (head == null)
 51bubuko.com,布布扣            {
 52bubuko.com,布布扣                return true;
 53bubuko.com,布布扣            }

 54bubuko.com,布布扣            else
 55bubuko.com,布布扣            {
 56bubuko.com,布布扣                return false;
 57bubuko.com,布布扣            }

 58bubuko.com,布布扣        }

 59bubuko.com,布布扣
 60bubuko.com,布布扣        /// <summary>
 61bubuko.com,布布扣        /// 附加操作,线性表未满,将值为item的新元素添加到末尾
 62bubuko.com,布布扣        /// </summary>
 63bubuko.com,布布扣        /// <param name="item"></param>

 64bubuko.com,布布扣        public void Append(T item)
 65bubuko.com,布布扣        {
 66bubuko.com,布布扣            Node<T> newNode = new Node<T>(item);  //根据元素创建新的节点
 67bubuko.com,布布扣            Node<T> node = new Node<T>();
 68bubuko.com,布布扣
 69bubuko.com,布布扣            if (head == null)
 70bubuko.com,布布扣            {
 71bubuko.com,布布扣                head = newNode;
 72bubuko.com,布布扣                return;
 73bubuko.com,布布扣            }

 74bubuko.com,布布扣            node = head;
 75bubuko.com,布布扣            while (node.Next != null)
 76bubuko.com,布布扣            {
 77bubuko.com,布布扣                node = node.Next;
 78bubuko.com,布布扣            }

 79bubuko.com,布布扣            node.Next = newNode;
 80bubuko.com,布布扣
 81bubuko.com,布布扣        }

 82bubuko.com,布布扣
 83bubuko.com,布布扣        /// <summary>
 84bubuko.com,布布扣        /// 寻找节点
 85bubuko.com,布布扣        /// </summary>
 86bubuko.com,布布扣        /// <param name="i"></param>
 87bubuko.com,布布扣        /// <returns></returns>

 88bubuko.com,布布扣        public Node<T> FindNode(int i)
 89bubuko.com,布布扣        {
 90bubuko.com,布布扣            if (IsEmpty())
 91bubuko.com,布布扣            {
 92bubuko.com,布布扣                Console.Write("List is empty");
 93bubuko.com,布布扣                return null;
 94bubuko.com,布布扣            }

 95bubuko.com,布布扣            if (i < 1)
 96bubuko.com,布布扣            {
 97bubuko.com,布布扣                Console.Write("Index is error");
 98bubuko.com,布布扣                return null;
 99bubuko.com,布布扣            }

100bubuko.com,布布扣            Node<T> current = head;
101bubuko.com,布布扣            int j = 1;
102bubuko.com,布布扣
103bubuko.com,布布扣            while (current.Next != null && j < i)
104bubuko.com,布布扣            {
105bubuko.com,布布扣                ++j;
106bubuko.com,布布扣                current = current.Next;
107bubuko.com,布布扣            }

108bubuko.com,布布扣            return current;
109bubuko.com,布布扣        }

110bubuko.com,布布扣
111bubuko.com,布布扣        /// <summary>
112bubuko.com,布布扣        /// 插入操作,在第i个节点前面插入item
113bubuko.com,布布扣        /// </summary>
114bubuko.com,布布扣        /// <param name="item"></param>
115bubuko.com,布布扣        /// <param name="i"></param>

116bubuko.com,布布扣        public void Insert(T item, int i)
117bubuko.com,布布扣        {
118bubuko.com,布布扣            Node<T> newNode = new Node<T>(item);
119bubuko.com,布布扣            Node<T> node = new Node<T>();
120bubuko.com,布布扣            Node<T> current = FindNode(i);
121bubuko.com,布布扣            if (current != null)
122bubuko.com,布布扣            {
123bubuko.com,布布扣                node = current;       //对目标节点备份
124bubuko.com,布布扣                newNode.Next = current;
125bubuko.com,布布扣                node.Next = newNode;
126bubuko.com,布布扣            }

127bubuko.com,布布扣        }

128bubuko.com,布布扣
129bubuko.com,布布扣        /// <summary>
130bubuko.com,布布扣        /// 插入操作,在第i个节点后面插入item
131bubuko.com,布布扣        /// </summary>
132bubuko.com,布布扣        /// <param name="item"></param>
133bubuko.com,布布扣        /// <param name="i"></param>

134bubuko.com,布布扣        public void InsertBack(T item, int i)
135bubuko.com,布布扣        {
136bubuko.com,布布扣            Node<T> newNode = new Node<T>(item);
137bubuko.com,布布扣            Node<T> current = FindNode(i);
138bubuko.com,布布扣            if (current != null)
139bubuko.com,布布扣            {
140bubuko.com,布布扣                newNode.Next = current.Next;
141bubuko.com,布布扣                current.Next = newNode;
142bubuko.com,布布扣            }

143bubuko.com,布布扣        }

144bubuko.com,布布扣
145bubuko.com,布布扣        /// <summary>
146bubuko.com,布布扣        /// 删除操作
147bubuko.com,布布扣        /// </summary>
148bubuko.com,布布扣        /// <param name="i"></param>
149bubuko.com,布布扣        /// <returns></returns>

150bubuko.com,布布扣        public T Delete(int i)
151bubuko.com,布布扣        {
152bubuko.com,布布扣            Node<T> current = FindNode(i);
153bubuko.com,布布扣            Node<T> node = new Node<T>();
154bubuko.com,布布扣            if (current != null)
155bubuko.com,布布扣            {
156bubuko.com,布布扣                node = current;   //对目标节点备份
157bubuko.com,布布扣                node.Next = current.Next;
158bubuko.com,布布扣                return current.Data;
159bubuko.com,布布扣            }

160bubuko.com,布布扣            else
161bubuko.com,布布扣            {
162bubuko.com,布布扣                Console.Write("the node is not exist!");
163bubuko.com,布布扣                return default(T);
164bubuko.com,布布扣            }

165bubuko.com,布布扣        }

166bubuko.com,布布扣
167bubuko.com,布布扣        /// <summary>
168bubuko.com,布布扣        /// 去表元
169bubuko.com,布布扣        /// </summary>
170bubuko.com,布布扣        /// <param name="i"></param>
171bubuko.com,布布扣        /// <returns></returns>

172bubuko.com,布布扣        public T GetElem(int i)
173bubuko.com,布布扣        {
174bubuko.com,布布扣            Node<T> current = FindNode(i);
175bubuko.com,布布扣
176bubuko.com,布布扣            if (current != null)
177bubuko.com,布布扣            {
178bubuko.com,布布扣                return current.Data;
179bubuko.com,布布扣            }

180bubuko.com,布布扣            else
181bubuko.com,布布扣            {
182bubuko.com,布布扣                Console.Write("the node is not exist!");
183bubuko.com,布布扣                return default(T);
184bubuko.com,布布扣            }

185bubuko.com,布布扣        }

186bubuko.com,布布扣
187bubuko.com,布布扣        /// <summary>
188bubuko.com,布布扣        /// 按值查找
189bubuko.com,布布扣        /// </summary>
190bubuko.com,布布扣        /// <param name="value"></param>
191bubuko.com,布布扣        /// <returns></returns>

192bubuko.com,布布扣        public int Locate(T value)
193bubuko.com,布布扣        {
194bubuko.com,布布扣            if (IsEmpty())
195bubuko.com,布布扣            {
196bubuko.com,布布扣                Console.WriteLine("List is Empty!");
197bubuko.com,布布扣                return -1;
198bubuko.com,布布扣            }

199bubuko.com,布布扣            Node<T> current = new Node<T>();
200bubuko.com,布布扣            current = head;
201bubuko.com,布布扣            int i = 1;
202bubuko.com,布布扣            while (current.Next != null && !current.Data.Equals(value))
203bubuko.com,布布扣            {
204bubuko.com,布布扣                current = current.Next;
205bubuko.com,布布扣                ++i;
206bubuko.com,布布扣            }

207bubuko.com,布布扣            return i;
208bubuko.com,布布扣        }

209bubuko.com,布布扣    }
bubuko.com,布布扣

碰到的问题:1 方法参数为int类型,改用object类型碰到一些问题,查找节点的时候,遍历整个链表,如何对比参数节点

                   对比参数会不会影响算法的时间复杂度

                2 append,insert,insertback方法本质上都是插入操作,有没有更好的方法实现 

代码没有经过测试,有兴趣的朋友可以试试,有问题,告知一下!   

C#数据结构-单链表,布布扣,bubuko.com

C#数据结构-单链表

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/lxclqy/p/3758560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!