C#反转单向链表的两种实现

原文: http://realisedream.spaces.live.com/blog/cns!3253B93FF5730097!149.entry
下面是C#实现的两种方法(一个是for循环,一个是递归)
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            //初始化链表
            LineNote startnote = new LineNote( "1" );
            LineNote note = startnote;
            for( int i = 2; i<= 5 ;i ++ )
            {
                note.Next = new LineNote( i.ToString() );
                note = note.Next;
            }
            note = startnote;
            //输出
            while ( note!=null )
            {
                System.Console.WriteLine( note.Note );
                note = note.Next;
            }
            //开始反转
            note = startnote;
            LineNote temp = note;
            while ( temp != null )
            {
                note = startnote;
                if ( temp != startnote )
                {
                    startnote = temp;
                    temp = temp.Next;
                    startnote.Next = note;
                }
                else
                {
                    temp = temp.Next;
                    note.Next= null;
                }
            }
            再次输出
            note = startnote;
            while ( note!=null )
            {
                System.Console.WriteLine( note.Note );
                note = note.Next;
            }
        }
    }
    class LineNote
    {
        public string Note;
        public LineNote Next;
        public LineNote( string note)
        {
            Note=note;
        }
    }
-----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace ReverseList
{
    public class ListNode
    {
        private object data;
        private ListNode next;
        //constructor to create ListNode that refers to dataValue
        //and in last node in list
        public ListNode(object dataValue)
            : this(dataValue, null)
        {
        }
        //constructor to create ListNode that refers to dataValue
        //and refer to next ListNode in List
        public ListNode(object dataValue, ListNode nextNode)
        {
            data = dataValue;
            next = nextNode;
        }
        //property Next
        public ListNode Next
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
        //property Data
        public object Data
        {
            get
            {
                return data;
            }
        }
    }//end of ListNode class
    //class List definition
    public class MyList
    {
        private ListNode firstNode;
        private ListNode lastNode;
        private string name;  //string like "list" to display
        //construct empty list with specified name
        public MyList(string listName)
        {
            name = listName;
            firstNode = lastNode = null;
        }
        //construct empty list with "list" as its name
        public MyList()
            : this("list")
        {
        }
        //return true if list is empty
        public bool IsEmpty()
        {
            lock (this)
            {
                return firstNode == null;
            }
        }
        //Insert object at the end of list.If List is empty,
        //firstNode and lastNode will refer to same object.
        //otherwise,lastNode's Next property refers to new node
        public void InsertAtBack(object insertItem)
        {
            lock (this)
            {
                if (IsEmpty())
                {
                    firstNode = lastNode = new ListNode(insertItem);
                }
                else
                {
                    lastNode = lastNode.Next = new ListNode(insertItem);
                }
            }
        }
        //output list contents
        virtual public void Print()
        {
            lock (this)
            {
                if (IsEmpty())
                {
                    Console.WriteLine("Empty " + name);
                    return;
                }
                Console.Write("The " + name + " is: ");
                ListNode current = firstNode;
                //output current node data while not at end of list
                while (current != null)
                {
                    Console.Write(current.Data + "");
                    current = current.Next;
                }
                Console.WriteLine("\n");
            }
        }
        public void Reverse()
        {
            lastNode = firstNode;
            ReverseInner(firstNode, null);
        }
        private void ReverseInner(ListNode head,ListNode pre)
        {
            ListNode node = head.Next;
            head.Next = pre;
            if (node != null)
            {
                ReverseInner(node, head);
            }
            else
            {
                firstNode = head;
                return;
            }
        }
    }// end class List
    //class EmptyListException definition
    public class EmptyListException : ApplicationException
    {
        public EmptyListException(string name)
            : base("The " + name + "is empty")
        {
        }
    }//end class EmptyListException
    class Program
    {
        public static MyList Create(int n)
        {
            MyList list = new MyList("Alpha");          
            for (int i = 0; i < n; i++)
            {
                list.InsertAtBack((char)('A' + i));
            }
            return list;
        }

        static void Main(string[] args)
        {
            MyList list = new MyList();
            list = Create(6);
            list.Print();
            list.Reverse();     
            list.Print();
        }
    }
}
其实这个题原本是用C写的,还是C写的简单,如下:
#include
#include
typedef char eleType;  // 定义链表中的数据类型
typedef struct listnode  { // 定义单链表结构
 eleType data;
 struct listnode *next;
}node;
node *create(int n) {  // 创建单链表,n为节点个数
 node *p = (node *)malloc(sizeof(node)); 
 node *head = p;  head->data = 'A';
 for(int i='B'; i<'A'+n; i++) {    
  p = (p->next = (node *)malloc(sizeof(node)));
  p->data = i;
  p->next = NULL;  
 }
 return head;
}
void print(node *head) { // 按链表顺序输出链表中元素
 for(; head; head = head->next)
  printf("%c ", head->data); 
 printf("\n");
}
node *reverse(node *head, node *pre) { // 逆转单链表函数。这是笔试时需要写的最主要函数
 node *p=head->next;
 head->next = pre;
 if(p) return reverse(p, head);
 else  return head;
}
node *reverse(node *head)
{
 node *p = head->next;
 head->next= NULL;
 node *q;
 for(node *ptr=p;ptr->next;ptr=q)
 {
  q= ptr->next;
  ptr->next = p;
  p = ptr;
 }
 ptr->next = p;
 return ptr;
}
int main(int argc, char* argv[]) {
 node *head = create(6);
 print(head);
 head = reverse(head);
 print(head);
}

转载于:https://www.cnblogs.com/tengs2000/articles/1384067.html

你可能感兴趣的:(C#反转单向链表的两种实现)