2

親愛なるみんな;

こんにちは、私はC++の初心者です。私が理解するのを手伝ってください:

リンクリストクラスにはどの関数を含める必要がありますか?演算子<<と>>;がオーバーロードされているはずだと思います。コードの改善(スタイル、エラーなど)を手伝ってください。よろしくお願いします。イガル。

編集:これは最初の段階にすぎません。次の段階は(うまくいけば)テンプレートを使用する予定です。

整数リスト(MyNODE.hとListDriver1.cppで囲まれている)の小さなコードを確認してください。MyNODE.h

    // This is my first attempt to write linked list. Igal Spector, June 2010.

#include <iostream.h>
#include <assert.h>

//Forward Declaration of the classes:
class ListNode;
class TheLinkedlist;

// Definition of the node (WITH IMPLEMENTATION !!!, without test drive):


class ListNode{
 friend class TheLinkedlist;
public:
 // constructor:
 ListNode(const int& value, ListNode *next= 0);
 // note: no destructor, as this handled by TheLinkedList class.

 // accessor: return data in the node.
// int Show() const {return theData;}

private:
 int theData;  //the Data
 ListNode* theNext; //points to the next node in the list.
};

//Implementations:
//constructor:
inline ListNode::ListNode(const int &value,ListNode *next)
:theData(value),theNext(next){}


//end of ListNode class, now for the LL class:

class TheLinkedlist
{
public:
 //constructors:
 TheLinkedlist();
 virtual ~TheLinkedlist();
 // Accessors:
 void InsertAtFront(const &);
 void AppendAtBack(const &);

// void InOrderInsert(const &);
 bool IsEmpty()const;//predicate function
 void Print() const;
private:
 ListNode * Head; //pointer to first node
 ListNode * Tail; //pointer to last node.
};

//Implementation:

//Default constructor
inline TheLinkedlist::TheLinkedlist():Head(0),Tail(0) {}

//Destructor
inline TheLinkedlist::~TheLinkedlist(){
 if(!IsEmpty()){  //list is not empty
 cout<<"\n\tDestroying Nodes"<<endl;
 ListNode *currentPointer=Head, *tempPtr;

  while(currentPointer != 0){ //Delete remaining Nodes.
   tempPtr=currentPointer;
  cout<<"The node: "<<tempPtr->theData <<" is Destroyed."<<endl<<endl;
  currentPointer=currentPointer->theNext;
  delete tempPtr;
  }
 Head=Tail = 0;  //don't forget this, as it may be checked one day.
 }
}

//Insert the Node to the beginning of the list:
void TheLinkedlist::InsertAtFront(const int& value){
 ListNode *newPtr = new ListNode(value,Head);
 assert(newPtr!=0);

 if(IsEmpty())  //list is empty
  Head = Tail = newPtr;
 else {    //list is NOT empty
  newPtr->theNext = Head;
  Head = newPtr;
 }
}

//Insert the Node to the beginning of the list:
void TheLinkedlist::AppendAtBack(const int& value){
 ListNode *newPtr = new ListNode(value, NULL);
 assert(newPtr!=0);

 if(IsEmpty())  //list is empty
  Head = Tail = newPtr;
 else {    //list is NOT empty
  Tail->theNext = newPtr;
  Tail = newPtr;
 }
}

//is the list empty?
inline bool TheLinkedlist::IsEmpty() const
  { return (Head == 0); }

// Display the contents of the list
void TheLinkedlist::Print()const{
 if ( IsEmpty() ){
  cout << "\n\t The list is empty!!"<<endl;
  return;
 }

 ListNode *tempPTR = Head;
 cout<<"\n\t The List is: ";

 while ( tempPTR != 0 ){
  cout<< tempPTR->theData <<"  ";
  tempPTR = tempPTR->theNext;
 }
 cout<<endl<<endl;
}
//////////////////////////////////////

テストドライバー:

//Driver test for integer Linked List.

#include <iostream.h>
#include "MyNODE.h"

// main Driver
int main(){

 cout<< "\n\t This is the test for integer LinkedList."<<endl;

 const int arraySize=11,
   ARRAY[arraySize]={44,77,88,99,11,2,22,204,50,58,12};

 cout << "\n\tThe array is: "; //print the numbers.
 for (int i=0;i<arraySize; i++)
  cout<<ARRAY[i]<<",  ";

 TheLinkedlist list;   //declare the list

 for(int index=0;index<arraySize;index++)
  list.AppendAtBack( ARRAY[index] );//create the list

 cout<<endl<<endl;
 list.Print();    //print the list

 return 0;     //end of the program.
}
4

2 に答える 2

7

リンクリストクラスにはどの関数を含める必要がありますか?

それはあなたがそれで何をする必要があるかに依存します。少なくとも、要素を追加して、リスト内の要素を確認できるはずです。

(これは常識です。リストを変更したり読み取ったりできない場合、リストは何に使用できるでしょうか?)


演算子<<と>>;がオーバーロードされているはずだと思います。

なんで?彼らは何をしますか?operator <<オブジェクトがC++IOストリームに挿入されるのと同じように、挿入を行うつもりだと思います。しかし、正確には何をすべきoperator >>でしょうか?ある種の要素の抽出/削除?この方法で挿入と抽出(?)を実装すると、おそらく誰もリンクリストクラスを理解できなくなります。リンクリストはIOストリームではありません。(簡潔にするために、IOストリームを持つオペレーターが選択されました。)

操作の意味が明確でない場合は、演算子のオーバーロードを避けることをお勧めします。addたとえば、メソッドを提供するなどして、操作にもっと明確に名前を付けることをお勧めしますremove(後者の操作の意味はまだ推測してい>>ます)。


コードを改善するのを手伝ってください(スタイル、エラーなど)

私はこれを私の答えの要点にしたくないので、ほんの少し頭から離れて、いくつかの問題があります:

  • #include <iostream>の代わりに#include <iostream.h>、を追加するか、の代わりにusing namespace std;(eg)と書く必要があります。std::coutcout

  • を取り除くようにしてくださいfriend。これを必要としない方法でクラスを設計できるはずです。friend適切なカプセル化を回避するために簡単に誤用されます。しかし、カプセル化は、OOPで確実に検討する必要があるものです。

  • これはC++の初心者にアドバイスすることではありませんが、リンクリストクラスをテンプレートクラスにすると、単なるintsとは異なる値が格納される可能性があります。これを将来の改善のためのヒントとしてとらえてください。


そして最後に:

  • C ++標準ライブラリに含まれているSTL(「標準テンプレートライブラリ」)コンテナを使用するだけです。「自分でローリングする」ことは、これらのデータ構造がどのように機能するかを理解するのに役立つことを知っていますが、C++標準ライブラリにはすでに堅牢で効率的なデータコンテナのセットが含まれていることに注意してください。
于 2010-06-13T11:22:40.883 に答える
1
  1. 0はNULLである必要があります

  2. コードが公開されることを気にしない場合にのみインラインで、通常、実装は別のファイルMylist.cppファイルに入れます。

  3. なぜあなたのデストラクタは仮想です、あなたは相続を持っていますか?

  4. stlのように、クラスを分離する代わりにstructノードを定義するだけで、練習用のリストをより適切に定義できます。http://www.sgi.com/tech/stl/List.html http://www.cplusplus.com/reference/stl/list/

C ++では、Javaでベクターとリンクリストを使用するのが一般的です http://www.yolinux.com/tokyoS/LinuxTutorialC++STL.html

于 2010-06-13T18:06:26.237 に答える