Data-structures-algorithms-circular-linked-list-algorithm

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

データ構造-循環リンクリスト

循環リンクリストは、最初の要素が最後の要素を指し、最後の要素が最初の要素を指すリンクリストのバリエーションです。 単一リンクリストと二重リンクリストの両方を循環リンクリストにすることができます。

循環としての単一リンクリスト

単一リンクリストでは、最後のノードの次のポインターは最初のノードを指します。

循環リンクリストとしての単一リンクリスト

循環としての二重リンクリスト

二重リンクリストでは、最後のノードの次のポインターは最初のノードを指し、最初のノードの前のポインターは最後のノードを指し、両方向に循環します。

循環リンクリストとしての二重リンクリスト

上記の図に従って、考慮すべき重要な点を次に示します。

  • 最後のリンクの次は、単一リンクリストと二重リンクリストの両方の場合のリストの最初のリンクを指します。
  • 二重にリンクされたリストの場合、最初のリンクの前のポイントはリストの最後を指します。

基本操作

以下は、循環リストでサポートされる重要な操作です。

  • 挿入-リストの先頭に要素を挿入します。
  • delete -リストの先頭から要素を削除します。
  • display -リストを表示します。

挿入操作

次のコードは、単一のリンクリストに基づく循環リンクリストへの挿入操作を示しています。

insertFirst(data):
Begin
   create a new node
   node -> data := data
   if the list is empty, then
      head := node
      next of node = head
   else
      temp := head
      while next of temp is not head, do
      temp := next of temp
      done
      next of node := head
      next of temp := node
      head := node
   end if
End

削除操作

次のコードは、単一のリンクリストに基づく循環リンクリストでの削除操作を示しています。

deleteFirst():
Begin
   if head is null, then
      it is Underflow and return
   else if next of head = head, then
      head := null
      deallocate head
   else
      ptr := head
      while next of ptr is not head, do
         ptr := next of ptr
      next of ptr = next of head
      deallocate head
      head := next of ptr
   end if
End

リスト表示操作

次のコードは、循環リンクリストでのリスト表示操作を示しています。

display():
Begin
   if head is null, then
      Nothing to print and return
   else
      ptr := head
      while next of ptr is not head, do
         display data of ptr
         ptr := next of ptr
      display data of ptr
   end if
End

Cプログラミング言語での実装については、リンクしてください:/data_structures_algorithms/circular_linked_list_program_in_c [ここをクリック]。