Mfc-linked-lists

提供:Dev Guides
移動先:案内検索

MFC-リンクリスト

  • リンクリスト*は、各要素が個別のオブジェクトである線形データ構造です。 リストの各要素(ノードと呼びます)は、データと次のノードへの参照という2つの項目で構成されます。 最後のノードにはnullへの参照があります。

リンクされたリストは、一緒にシーケンスを表すノードのグループで構成されるデータ構造です。 プログラマが必要なときにいつでもデータを保存するための新しい場所を自動的に作成できるように、データを構造とともに保存する方法です。 その顕著な特徴のいくつかは-

  • リンクリストは、アイテムを含む一連のリンクです。
  • 各リンクには、別のリンクへの接続が含まれています。
  • リスト内の各アイテムはノードと呼ばれます。
  • リストに少なくとも1つのノードが含まれている場合、新しいノードがリストの最後の要素として配置されます。
  • リストにノードが1つしかない場合、そのノードは最初と最後のアイテムを表します。

リンクリストには2種類あります-

単一リンクリスト

単一リンクリストは、データ構造の一種です。 単一リンクリストでは、リスト内の各ノードに、ノードの内容とリスト内の次のノードへのポインターまたは参照が格納されます。

単一のリンクリスト

二重リンクリスト

二重リンクリストは、ノードと呼ばれる一連のリンクされたレコードで構成されるリンクされたデータ構造です。 各ノードには、ノードのシーケンス内の前のノードと次のノードへの参照である2つのフィールドが含まれています。

二重リンクリスト

CListクラス

MFCは、テンプレートリンクリスト実装であり、完全に機能するクラス CList を提供します。 CListリストは、二重リンクリストのように動作します。 POSITION型の変数は、リストのキーです。 POSITION変数は、リストを順番にたどるイテレータとして、および場所を保持するブックマークとして使用できます。

____CListクラスのメソッドのリストです。

Sr.No. Name & Description
1

AddHead

要素(または別のリストのすべての要素)をリストの先頭に追加します(新しい先頭を作成します)。

2

AddTail

要素(または別のリストのすべての要素)をリストの末尾に追加します(新しい末尾を作成します)。

3

Find

ポインター値で指定された要素の位置を取得します。

4

FindIndex

ゼロから始まるインデックスで指定された要素の位置を取得します。

5

GetAt

指定された位置にある要素を取得します。

6

GetCount

リスト内の要素数を返します。

7

GetHead

リストの先頭要素を返します(空にはできません)。

8

GetHeadPosition

リストの先頭要素の位置を返します。

9

GetNext

反復する次の要素を取得します。

10

GetPrev

反復する前の要素を取得します。

11

GetSize

リスト内の要素数を返します。

12

GetTail

リストの末尾要素を返します(空にはできません)。

13

GetTailPosition

リストの末尾要素の位置を返します。

14

InsertAfter

指定された位置の後に新しい要素を挿入します。

15

InsertBefore

指定された位置の前に新しい要素を挿入します。

16

IsEmpty

空のリスト条件(要素なし)をテストします。

17

RemoveAll

このリストからすべての要素を削除します。

18

RemoveAt

位置で指定されたこのリストから要素を削除します。

19

RemoveHead

リストの先頭から要素を削除します。

20

RemoveTail

リストの末尾から要素を削除します。

21

SetAt

指定された位置に要素を設定します。

以下は、CListオブジェクトのさまざまな操作です-

CListオブジェクトを作成

CList値またはオブジェクトのコレクションを作成するには、最初にコレクションの値のタイプを決定する必要があります。 int、CString、doubleなど、既存のプリミティブデータ型のいずれかを使用できます。 以下のコードで以下に示すように。

CList<double, double>m_list;

アイテムを追加

アイテムを追加するには、CList
AddTail()関数を使用できます。 リストの最後にアイテムを追加します。 リストの先頭に要素を追加するには、CList :: AddHead()関数を使用できます。 OnInitDialog()CListで、次のコードに示すように、オブジェクトが作成され、4つの値が追加されます。
CList<double, double>m_list;

//Add items to the list
m_list.AddTail(100.75);
m_list.AddTail(85.26);
m_list.AddTail(95.78);
m_list.AddTail(90.1);

アイテムを取得する

POSITION型の変数は、リストのキーです。 POSITION変数を反復子として使用して、リストを順番に走査できます。

  • ステップ1 *-リストから要素を取得するには、すべての値を取得する次のコードを使用できます。
//iterate the list
POSITION pos = m_list.GetHeadPosition();
while (pos) {
   double nData = m_list.GetNext(pos);
   CString strVal;
   strVal.Format(L"%.2f\n", nData);
   m_strText.Append(strVal);
}
*ステップ2 *-完全なCMFCCListDemoDlg
OnInitDialog()関数です。
BOOL CMFCCListDemoDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

  //Set the icon for this dialog. The framework does this automatically
  //when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);            //Set big icon
   SetIcon(m_hIcon, FALSE);            //Set small icon

  //TODO: Add extra initialization here
   CList<double, double>m_list;

  //Add items to the list
   m_list.AddTail(100.75);
   m_list.AddTail(85.26);
   m_list.AddTail(95.78);
   m_list.AddTail(90.1);

  //iterate the list
   POSITION pos = m_list.GetHeadPosition();
   while (pos) {
      double nData = m_list.GetNext(pos);
      CString strVal;
      strVal.Format(L"%.f\n", nData);
      m_strText.Append(strVal);
   }

   UpdateData(FALSE);

   return TRUE;//return TRUE unless you set the focus to a control
}
  • ステップ3 *-上記のコードをコンパイルして実行すると、次の出力が表示されます。

取得

中間にアイテムを追加

リストの中央にアイテムを追加するには、CList ::。InsertAfter()およびCList ::。InsertBefore()関数を使用できます。 2つのパラメーターが必要です。1つ目は位置(追加できる場所)、2つ目は値です。

  • ステップ1 *-次のコードに示すように、新しいアイテムを挿入しましょう。
BOOL CMFCCListDemoDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

  //Set the icon for this dialog. The framework does this automatically
  //when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);            //Set big icon
   SetIcon(m_hIcon, FALSE);         //Set small icon

  //TODO: Add extra initialization here
   CList<double, double>m_list;

  //Add items to the list
   m_list.AddTail(100.75);
   m_list.AddTail(85.26);
   m_list.AddTail(95.78);
   m_list.AddTail(90.1);

   POSITION position = m_list.Find(85.26);
   m_list.InsertBefore(position, 200.0);
   m_list.InsertAfter(position, 300.0);

  //iterate the list
   POSITION pos = m_list.GetHeadPosition();
   while (pos) {
      double nData = m_list.GetNext(pos);
      CString strVal;
      strVal.Format(L"%.2f\n", nData);
      m_strText.Append(strVal);
   }

   UpdateData(FALSE);

   return TRUE;//return TRUE unless you set the focus to a control
}
  • ステップ2 *-最初に値85.26の位置を取得し、その値の前に1つの要素を挿入し、その値の後に1つの要素を挿入したことがわかります。
  • ステップ3 *-上記のコードをコンパイルして実行すると、次の出力が表示されます。

アイテムの追加

アイテムの値を更新

配列の中央にあるアイテムを更新するには、CArray ::。SetAt()関数を使用できます。 これには2つのパラメーターが必要です。1つ目は位置、2つ目は値です。

次のコードに示すように、リスト内の300.00を400に更新します。

BOOL CMFCCListDemoDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

  //Set the icon for this dialog. The framework does this automatically
  //when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);             //Set big icon
   SetIcon(m_hIcon, FALSE);           //Set small icon

  //TODO: Add extra initialization here
   CList<double, double>m_list;

  //Add items to the list
   m_list.AddTail(100.75);
   m_list.AddTail(85.26);
   m_list.AddTail(95.78);
   m_list.AddTail(90.1);

   POSITION position = m_list.Find(85.26);
   m_list.InsertBefore(position, 200.0);
   m_list.InsertAfter(position, 300.0);

   position = m_list.Find(300.00);
   m_list.SetAt(position, 400.00);

  //iterate the list
   POSITION pos = m_list.GetHeadPosition();
   while (pos) {
      double nData = m_list.GetNext(pos);
      CString strVal;
      strVal.Format(L"%.2f\n", nData);
      m_strText.Append(strVal);
   }

   UpdateData(FALSE);

   return TRUE;//return TRUE unless you set the focus to a control
}

上記のコードをコンパイルして実行すると、次の出力が表示されます。 300.00の値が400.00に更新されていることがわかります。

アイテムの更新

アイテムを削除

特定のアイテムを削除するには、CList
RemoveAt()関数を使用できます。 リストからすべての要素を削除するには、CList :: RemoveAll()関数を使用できます。

値として95.78を持つ要素を削除しましょう。

BOOL CMFCCListDemoDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

  //Set the icon for this dialog. The framework does this automatically
  //when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);             //Set big icon
   SetIcon(m_hIcon, FALSE);            //Set small icon

  //TODO: Add extra initialization here
   CList<double, double>m_list;

  //Add items to the list
   m_list.AddTail(100.75);
   m_list.AddTail(85.26);
   m_list.AddTail(95.78);
   m_list.AddTail(90.1);

   POSITION position = m_list.Find(85.26);
   m_list.InsertBefore(position, 200.0);
   m_list.InsertAfter(position, 300.0);

   position = m_list.Find(300.00);
   m_list.SetAt(position, 400.00);

   position = m_list.Find(95.78);
   m_list.RemoveAt(position);

  //iterate the list
   POSITION pos = m_list.GetHeadPosition();
   while (pos) {
      double nData = m_list.GetNext(pos);
      CString strVal;
      strVal.Format(L"%.2f\n", nData);
      m_strText.Append(strVal);
   }
   UpdateData(FALSE);

   return TRUE;//return TRUE unless you set the focus to a control
}

上記のコードをコンパイルして実行すると、次の出力が表示されます。 これで、95.78の値がリストの一部ではなくなっていることがわかります。

アイテムの削除