JAVA与C#对照学习系列 —— 数据结构

    我个人的感觉:通过练习数据结构在不同语言中的实现,可以很快熟悉新学语言,了解彼此的差异。

    Java类中的嵌套类可以就是类中的一个field,可以在嵌套类中直接访问其它field,而嵌套类中的field也对外面暴露!而在C#中情况却与JAVA不同,其又没有C++的friend关键字!怎么办?我在网上看到一个网站号称《 Data Structures and Algorithms with Object-Oriented Design Patterns 》,起上有C++、JAVA、C#数据结构实现的源码,看看别人的定义:
以LinkedList为例:
//
//   This file contains the C++ code from Program 4.9 of
//   "Data Structures and Algorithms
//    with Object-Oriented Design Patterns in C++"
//   by Bruno R. Preiss.
//
//   Copyright (c) 1998 by Bruno R. Preiss, P.Eng.  All rights reserved.
//
//   http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus4/programs/pgm04_09.cpp
//
template //此处如不事先声明,C++编译器会提示(*)中找不到LinkedList
class LinkedList;//而C#不必如此!

template
class ListElement
{
    T datum;
    ListElement* next;

    ListElement (T const&, ListElement*);
public:
    T const& Datum () const;
    ListElement const* Next () const;

    friend LinkedList;//*
};

template
class LinkedList
{
    ListElement* head;
    ListElement* tail;
public:
    LinkedList ();
    ~LinkedList ();

    LinkedList (LinkedList const&);
    LinkedList& operator = (LinkedList const&);

    ListElement const* Head () const;
    ListElement const* Tail () const;
    bool IsEmpty () const;
    T const& First () const;
    T const& Last () const;

    void Prepend (T const&);
    void Append (T const&);
    void Extract (T const&);
    void Purge ();
    void InsertAfter (ListElement const*, T const&);
    void InsertBefore (ListElement const*, T const&);
};


//
//   This file contains the Java code from Program 4.13 of
//   "Data Structures and Algorithms
//    with Object-Oriented Design Patterns in Java"
//   by Bruno R. Preiss.
//
//   Copyright (c) 1998 by Bruno R. Preiss, P.Eng.  All rights reserved.
//
//   http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus5/programs/pgm04_13.txt
//
public class LinkedList
{
    protected Element head;
    protected Element tail;

    public final class Element//嵌套类,使用private修饰应该也可以
    {
 Object datum;
 Element next;

 Element (Object datum, Element next)
 {
     this.datum = datum;
     this.next = next;
 }

 public Object getDatum ()
     { return datum; }

 public Element getNext ()
     { return next; }
 // ...
    }
    // ...
}

 

//
//   This file contains the C# code from Program 4.12 of
//   "Data Structures and Algorithms
//    with Object-Oriented Design Patterns in C#"
//   by Bruno R. Preiss.
//
//   Copyright (c) 2001 by Bruno R. Preiss, P.Eng.  All rights reserved.
//     注意:C#的历史也有几年了!哈哈!
//   http://www.pads.uwaterloo.ca/Bruno.Preiss/books/opus6/programs/pgm04_12.txt
//
public class LinkedList
{
    protected Element head;
    protected Element tail;

    public sealed class Element//关键字sealed与Java的final类似,但是final功能多!
    {
 internal LinkedList list;//关键字internal
 internal object datum;
 internal Element next;

 internal Element(
     LinkedList list, object datum, Element next)
 {
     this.list = list;
     this.datum = datum;
     this.next = next;
 }

 public object Datum
     { get { return datum; } }

 public Element Next
     { get { return next; } }
 // ...
    }
    // ...
}

URL:http://www.brpreiss.com/
其网站上有在线文档,还有各个类与接口间的关系图。把它们都领会还是要费些功夫,我正在努力中!
我想在这里我们还可以学到如何组织类、接口,个语言间的异同。在学习数据结构时还可以接触Pattern的思想,何乐而不为!
如果你熟悉数据结构可以直接下载源代码来看,且当复习!


 

你可能感兴趣的:(语言/算法与基础)