天天看点

C#实现链表

今天受一个帖子的刺激,再次复习起了数据结构与算法,那本《数据结构与算法(java版)》我还剩图和高级排序的几章没看,工作上也没我的事需要处理,就用c#重新写了一遍链表结构,权作复习。

定义list接口:

   public  interface list

    {

        bool isempty();

        void unshift(object obj);

        object shift();

        void push(object obj);

        object pop();

        bool contain(object obj);

        void delete(object obj);

        void printall();

        object gethead();

        object gettail();

        void clear();

    }

实现单向链表:

 //单向链表

    public class slist:list

        private snode head, tail;

        public slist()

        {

            this.head = this.tail = null;

        }

        public bool isempty()

            return head == null;

        public void unshift(object obj)

            head = new snode(obj, head);

            if (tail == null)

                tail = head;

        public object shift()

            if (head == null)

                throw new nullreferenceexception();

            object value = head.value;

            if (head == tail)

                head = tail = null;

            else

                head = head.next;

            return value;

        public void push(object obj)

            if (!isempty())

            {

                tail.next = new snode(obj);

                tail = tail.next;

            }

                head = tail = new snode(obj);

        public object pop()

            object obj = tail.value;

                //查找前驱节点

                for (snode temp = head; temp.next != null && !temp.next.equals(tail); temp = temp.next)

                    tail = temp;

                tail.next = null;

            return obj;

        public void printall()

            string result = "";

            for (snode temp = head; temp != null; temp = temp.next)

                result += " " + temp.value.tostring();

            console.writeline(result);

        public bool contain(object obj)

                return false;

                for (snode temp = head; temp != null; temp = temp.next)

                {

                    if (temp.value.equals(obj))

                        return true;

                }

            return false;

        public void delete(object obj)

                if (head == tail && head.value.equals(obj))

                    head = tail = null;

                else if (head.value.equals(obj))

                    head = head.next;

                else

                    //temp_prev为删除值的前驱节点

                    for (snode temp_prev = head, temp = head.next; temp != null; temp_prev = temp_prev.next, temp = temp.next)

                    {

                        if (temp.value.equals(obj))

                        {

                            temp_prev.next = temp.next;  //设置前驱节点的next为下个节点 

                            if (temp == tail)

                               tail = temp_prev;

                            temp = null;

                            break;

                        }

                    }

        public  object gethead()

            return this.head.value;

        public object gettail()

            return this.tail.value;

        public void clear()

        {

            do

            {

                delete(head.value);

            } while (!isempty());

        }

    class snode

        public object value;

        public snode next;

        public snode(object value, snode next)

            this.value = value;

            this.next = next;

        public snode(object value)

            this.next = null;

实现双向链表:

 //双向链表

    public class linkedlist:list

        private linkednode head, tail;

        public linkedlist()

            head = tail = null;

            if (isempty())

                head = tail = new linkednode(obj);

                head = new linkednode(obj, null, head);

                head.next.prev = head;

            object obj = head.value;

                head.prev = null;

                tail = new linkednode(obj, tail, null);

                tail.prev.next = tail;

            object value = tail.value;

                tail = tail.prev;

                for (linkednode temp = head; temp != null; temp = temp.next)

                            {

                                tail = tail.prev;

                                tail.next = null;

                                break;

                            }

                            else if (temp == head)

                                head.next.prev = null;

                                head = head.next;

                            else

                                temp.prev.next = temp.next;

            for(linkednode temp=head;temp!=null;temp=temp.next)

        public object gethead()

    }

    class linkednode

        public linkednode prev;

        public linkednode next;

        public linkednode(object value, linkednode prev, linkednode next)

            this.prev = prev;

        public linkednode(object value)

文章转自庄周梦蝶  ,原文发布时间5.17