链表排序

using System;

namespace ConsoleApplication1
{
 

    class node
    {
        public int value;
        public node next;
        public node previous;
        public node maxNode(node x,node y)
        {
            if(y.value>=x.value)
                return y;
            else
                return x;
        }

    }
    class Class1
    {
        /// <summary>
        ///
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //create a double linked list

            //each input of data should be ended with enter
            //if the input in finished,please press space,than enter
            string buffer;
            int nodeAccount=0;
            node superHead,superTail;
            node head=new node();
            superHead=head;
            superHead.previous=null;
            while((buffer=Console.ReadLine())!=" ")
            {
                head.value=Int32.Parse(buffer);
                node tail=new node();
                head.next=tail;
                tail.previous=head;
                head=tail;
                nodeAccount++;
            }
            superTail=head;
            superTail.next=null;
 
            //sort
            int outer,inner;
            node temp;
            node maxIndex;
           
            for(outer=nodeAccount;outer>0;outer--)
            {
                head=superHead;
                maxIndex=superHead;
                for(inner=0;inner<outer;inner++)
                {
                    maxIndex=head.maxNode(maxIndex,head);
                    head=head.next;
                }
                if(head!=null&&maxIndex.previous!=null)
                {
                    head=head.previous;
                   
                    //8 pointer exchanged
                    temp=maxIndex.next;
                    maxIndex.next=head.next;
                    head.next=temp;
               
                    temp=maxIndex.previous;
                    maxIndex.previous=head.previous;
                    head.previous=temp;

                    temp=maxIndex.next.previous;
                    maxIndex.next.previous=head.next.previous;
                    head.next.previous=temp;

                    temp=maxIndex.previous.next;
                    maxIndex.previous.next=head.previous.next;
                    head.previous.next=temp;               
                }

            }

            head=superHead;
            while(head!=superTail)//head is used as temporary variable
            {
                Console.WriteLine(head.value);
                head=head.next;

            }
                
        }
    }
}
/*
用单链表,只交换节点中的数据更方便的
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值