请教各位高手!如何遍历单链表!小弟在此谢过!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace 单链表_8__插入操作
    {
        public interface IListDS<T>
        {
            int Count();                    //返回单链表长度
            void clear();                   //清空单链表中所有的元素
            bool IsEmpty();                 //判断是否为空
            void Append(T item);            //在单链表尾添加一个元素
            void Insert(T item, int pos);   //在单链表中添加一个元素
            //T Delete(int pos);              //删除单链表中的元素
            //T GetElem(int pos);             //返回单链表中的元素
            //int Locate(T value);            //在单链表中查找值为value的元素
        }
        //单链表结点类
        public class Node<T>
        {
            private T data;                 //数据域
            private Node<T> next;           //引用域
            //构造器
            public Node(T val, Node<T> p)
            {
                data = val;
                next = p;
            }
            //构造器
            public Node(Node<T> p)
            {
                next = p;
            }
            //构造器
            public Node(T val)
            {
                data = val;
                next = null;
            }
            //构造器
            public Node()
            {
                data = default(T);
                next = null;
            }
            //数据域属性
            public T Data
            {
                get
                {
                    return data;
                }
                set
                {
                    data = value;
                }
            }
            //引用域属性
            public Node<T> Next
            {
                get
                {
                    return next;
                }
                set
                {
                    next = value;
                }
            }
        }
        //单链表类
        public class LinkList<T> : IListDS<T>
        {
            private Node<T> head;              //单链表的头引用
            private int count;                 //结点个数
            //头引用属性
            public Node<T> Head
            {
                get
                {
                    return head;
                }
                set
                {
                    head = value;
                }
            }
            //单链表初始化
            public LinkList()
            {
                head = null;
                count = 0;
            }
            //求单链表长度
            public int Count()
            {
                return count;
            }
            //清空操作
            public void clear()
            {
                head = null;
                count = 0;
            }
            //判断是否为空
            public bool IsEmpty()
            {
                if ((head == null) && (count == 0))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            //附加操作
            public void Append(T item)
            {
                Node<T> q = new Node<T>(item);
                Node<T> p = new Node<T>();
                //单链表为空
                if (head == null)
                {
                    head = q;
                    ++count;
                    return;
                }
                //查找尾部结点
                p = head;
                if ((p != null) && (p.Next != null))
                {
                    p = p.Next;
                }
                //添加结点
                p.Next = q;
                //表长加1
                ++count;
            }
            //前插操作
            public void Insert(T item, int pos)
            {
                Node<T> q = new Node<T>(item);
                Node<T> p = new Node<T>();
                //插入位置不对
                if ((pos < 1) || (pos > count))
                {
                    Console.WriteLine("插入位置不对");
                    return;
                }
                //在第一个结点处插入新结点
                if (pos == 1)
                {
                    q.Next = head;
                    head = q;
                    ++count;
                    return;
                }
                //其他位置插入新结点
                p = head;
                Node<T> r = new Node<T>();
                int i = 1;
                while ((p != null) && (p.Next != null) && (i < pos))
                {
                    r = p;
                    p = p.Next;
                    ++i;
                }
                //插入结点
                q.Next = p;
                r.Next = q;
                //表长加1
                ++count;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                LinkList<int> g = new LinkList<int>();
                for (int i = 0; i <= 10; i++)
                {
                    g.Append(i);
                }
                g.Insert(20,8);
                Console.WriteLine("单链表长度为:{0}\n单链表是否为空:{1}", g.Count(), g.IsEmpty());
            }
        }
    }
    请问各位高手!这样的单链表如何遍历?(最好能有代码!)小弟在此谢过!
      

  2.   

    http://www.cnblogs.com/JeffreyZhao/archive/2010/07/02/1769605.html