C#寫的排序鏈表

在看《Programming C#》的時候看到了一個排序鏈表的例子,其實作者原本要講的是約束,碰巧最近在上數據結構,對這個比較敏感,沒想到用C#寫鏈表真是奧妙啊,下面有貼代碼。

還有就是發現大多數關於數據結構和算法的書大多都是用C或者Java實現的,C++的好書很少,更談不上C#了。所以小研究了一下,大概一方面是因爲C++概念多,對讀者要求比較高。還有一方面就是C++和C#都有內置的STL,裏面有很多數據結構的實現,Java 和 C 都沒有這種標準。不過話說回來,學數據結構重要的是學習其思想,看看《算法導論》(Introduction to Algorithms)就知道了。

using System;
using System.Collections.Generic;
using System.Text;

namespace UsingConstraints
{
    
public class Employee : IComparable<Employee>
    
{
        
private string name;
        
public Employee(string name)
        
{
            
this.name = name;
        }

        
public override string ToString()
        
{
            
return this.name;
        }


        
public int CompareTo(Employee rhs)
        
{
            
return this.name.CompareTo(rhs.name);
        }

        
public bool Equals(Employee rhs)
        
{
            
return this.name == rhs.name;
        }

    }


    
public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
    
{
        
private T data;
        
private Node<T> next = null;
        
private Node<T> prev = null;

        
public Node(T data)
        
{
            
this.data = data;
        }

        
public T Data
        
{
            
get
            
{
                
return this.data;
            }

        }

        
public Node<T> Next
        
{
            
get
            
{
                
return this.next;
            }

        }


        
public int CompareTo(Node<T> rhs)
        
{
            
return data.CompareTo(rhs.data);
        }

        
public bool Equals(Node<T> rhs)
        
{
            
return this.data.Equals(rhs.data);
        }


        
public Node<T> Add(Node<T> newNode)
        
{
            
if (this.CompareTo(newNode) > 0)
            
{
                newNode.next 
= this;
                
if (this.prev != null)
                
{
                    
this.prev.next = newNode;
                    newNode.prev 
= this.prev;
                }

                
this.prev = newNode;
                
return newNode;
            }

            
else
            
{
                
if (this.next != null)
                
{
                    
this.next.Add(newNode);
                }

                
else
                
{
                    
this.next = newNode;
                    newNode.prev 
= this;
                }

                
return this;
            }

        }


        
public override string ToString()
        
{
            
string output = data.ToString();
            
if (next != null)
                output 
+= "" + next.ToString();
            
return output;
        }

    }


    
public class LinkedList<T> where T : IComparable<T>
    
{
        
private Node<T> headNode = null;

        
public T this[int index]
        
{
            
get
            
{
                
int ctr = 0;
                Node
<T> node = headNode;
                
while (node != null && ctr <= index)
                
{
                    
if (ctr == index)
                    
{
                        
return node.Data;
                    }

                    
else
                    
{
                        node 
= node.Next;
                    }

                    
++ctr;
                }

                
throw new ArgumentOutOfRangeException();
            }

        }


        
public LinkedList()
        
{
        }


        
public void Add(T data)
        
{
            
if (headNode == null)
            
{
                headNode 
= new Node<T>(data);
            }

            
else
            
{
                headNode 
=headNode.Add (new Node<T>(data));
            }

        }


        
public override string ToString()
        
{
            
if (this.headNode != null)
                
return this.headNode.ToString();
            
else
                
return string.Empty;
        }

    }


    
class Test
    
{
        
static void Main(string[] args)
        
{
            Test t 
= new Test();
            t.Run();
        }

        
public void Run()
        
{
            LinkedList
<int> myLinkedList = new LinkedList<int>();
            Random rand 
= new Random();
            Console.Write(
"Adding: ");
            
for (int i = 0; i < 10; i++)
            
{
                
int nextInt = rand.Next(10);
                Console.Write(
"{0} ", nextInt);
                myLinkedList.Add(nextInt);
            }


            LinkedList
<Employee> employees = new LinkedList<Employee>();
            employees.Add(
new Employee ("John"));
            employees.Add(
new Employee("Paul"));
            employees.Add(
new Employee("George"));
            employees.Add(
new Employee("Ringo"));

            Console.WriteLine(
" Retrieving collections...");

            Console.WriteLine(
"Integers: " + myLinkedList);
            Console.WriteLine(
"Employees: " + employees);

            Console.ReadKey();
        }

    }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章