C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

ソートされた循環二重リンクリストを実装するC++プログラム


データ構造では、リンクリストはデータ要素の線形コレクションです。リストの各要素またはノードは、データと次のノードへの参照の2つの項目で構成されます。最後のノードにはnullへの参照があります。リンクリストへのエントリポイントは、リストの先頭と呼ばれます。

Circular Doubly Linked Listでは、2つの連続する要素が前と次のポインターによってリンクまたは接続され、最後のノードは次のポインターによって最初のノードを指し、最初のノードも前のポインターによって最後のノードを指します。

循環二重リンクリストでは、ノードデータフィールドのすべてのデータ値が並べ替えられたままになります。これは、そのようなリンクリストに新しいノードを挿入するときに処理されます。

アルゴリズム

Begin
   Create a class circulardoublylist within which we have following functions:
   nod *create_node(int) = To memory allocated for node dynamically.
   insert_begin() = To Insert elements at beginning of the list.
      If the list is empty, then insert the node and set next and previous pointer as NULL.
      If the list is not empty, insert the data and set next and previous pointer and update them.
   insert_end() = To Insert elements at end of the list.
      If the list is empty create a node as circular doubly list.
      Find last node.
      Create node dynamically.
      Start going to be the next of new node.
      Make new node as previous node.
      Make last previous of new node
      Make new node next of old last
         insert_pos() = To insert elements at a specified position of the list.
      insert the data.
      Enter the position at which element to be inserted.
      If the list is empty insert node at first.
      If list is not empty find node having position and next node.
      Insert the node between them.
   delete_pos() = To delete elements from specified position of the list.
      if list is empty then return.
      Enter the position from which node need to be deleted.
      If list has one node delete it and update next and prev pointers.
      If list has more than one node node then delete the node at particular position and update next and prev pointer.
   sort() = To sort elements in the list.
      if the list is empty return.
      Sort the elements in the list.
   display() = To display the list.
   reverse() = To reverse the list.
End

サンプルコード

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct nod {
   int info;
   struct nod *n;
   struct nod *p;
}*start, *last;
int count = 0;
class circulardoublylist {
   public:
      nod *create_node(int);
      void insert_begin();
      void insert_end();
      void insert_pos();
      void delete_pos();
      void sort();
      void display();
      void reverse();
      circulardoublylist() {
         start = NULL;
         last = NULL;
      }
};
int main() {
   int c;
   circulardoublylist cdl;
   while (1) {
      cout<<"1.Insert at Beginning"<<endl;
      cout<<"2.Insert at End"<<endl;
      cout<<"3.Insert at Position"<<endl;
      cout<<"4.Delete at Position"<<endl;
      cout<<"5.sort the list"<<endl;
      cout<<"6.Display List"<<endl;
      cout<<"7.Reverse List"<<endl;
      cout<<"8.Exit"<<endl;
      cout<<"Enter your choice : ";
      cin>>c;
      switch(c) {
         case 1:
            cdl.insert_begin();
         break;
         case 2:
            cdl.insert_end();
         break;
         case 3:
            cdl.insert_pos();
         break;
         case 4:
            cdl.delete_pos();
         break;
         case 5:
            cdl.sort();
         break;
         case 6:
            cdl.display();
         break;
         case 7:
            cdl.reverse();
         break;
         case 8:
            exit(1);
         default:
            cout<<"Wrong choice"<<endl;
      }
   }
   return 0;
}
nod* circulardoublylist::create_node(int v) {
   count++;
   struct nod *t;
   t = new(struct nod);
   t->info = v;
   t->n = NULL;
   t->p = NULL;
   return t;
}
void circulardoublylist::insert_begin() {
   int v;
   cout<<endl<<"Enter the element to be inserted: ";
   cin>>v;
   struct nod *t;
   t = create_node(v);
   if (start == last && start == NULL) {
      cout<<"Element inserted in empty list"<<endl;
      start = last = t;
      start->n = last->n = NULL;
      start->p = last->p = NULL;
   } else {
      t->n = start;
      start->p = t;
      start = t;
      start->p = last;
      last->n = start;
      cout<<"Element inserted"<<endl;
   }
}
void circulardoublylist::insert_end() {
   int v;
   cout<<endl<<"Enter the element to be inserted: ";
   cin>>v;
   struct nod *t;
   t = create_node(v);
   if (start == last && start == NULL) {
      cout<<"Element inserted in empty list"<<endl;
      start = last = t;
      start->n= last->n = NULL;
      start->p = last->p= NULL;
   } else {
      last->n= t;
      t->p= last;
      last = t;
      start->p = last;
      last->n= start;
   }
}
void circulardoublylist::insert_pos() {
   int v, pos, i;
   cout<<endl<<"Enter the element to be inserted: ";
   cin>>v;
   cout<<endl<<"Enter the position of element inserted: ";
   cin>>pos;
   struct nod *t, *s, *ptr;
   t = create_node(v);
   if (start == last && start == NULL) {
      if (pos == 1) {
         start = last = t;
         start->n = last->n = NULL;
         start->p = last->p = NULL;
      } else {
         cout<<"Position out of range"<<endl;
         count--;
         return;
      }
   } else {
      if (count < pos) {
         cout<<"Position out of range"<<endl;
         count--;
         return;
      }
      s = start;
      for (i = 1;i <= count;i++) {
         ptr = s;
         s = s->n;
         if (i == pos - 1) {
            ptr->n = t;
            t->p= ptr;
            t->n= s;
            s->p = t;
            cout<<"Element inserted"<<endl;
            break;
         }
      }
   }
}
void circulardoublylist::delete_pos() {
   int pos, i;
   nod *ptr, *s;
   if (start == last && start == NULL) {
      cout<<"List is empty, nothing to delete"<<endl;
      return;
   }
   cout<<endl<<"Enter the position of element to be deleted: ";
   cin>>pos;
   if (count < pos) {
      cout<<"Position out of range"<<endl;
      return;
   }
   s = start;
   if(pos == 1) {
      count--;
      last->n = s->n;
      s->n->p = last;
      start = s->n;
      free(s);
      cout<<"Element Deleted"<<endl;
      return;
   }
   for (i = 0;i < pos - 1;i++ ) {
      s = s->n;
      ptr = s->p;
   }
   ptr->n = s->n;
   s->n->p = ptr;
   if (pos == count) {
      last = ptr;
   }
   count--;
   free(s);
   cout<<"Element Deleted"<<endl;
}
void circulardoublylist::sort() {
   struct nod *t, *s;
   int v, i;
   if (start == last && start == NULL) {
      cout<<"The List is empty, nothing to sort"<<endl;
      return;
   }
   s = start;
   for (i = 0;i < count;i++) {
      t= s->n;
      while (t != start) {
         if (s->info > t->info) {
            v = s->info;
            s->info = t->info;
            t->info = v;
         }
         t = t->n;
      }
      s = s->n;
   }
   cout<<"List sorted"<<endl;
}
void circulardoublylist::display() {
   int i;
   struct nod *s;
   if (start == last && start == NULL) {
      cout<<"The List is empty, nothing to display"<<endl;
      return;
   }
   s = start;
   for (i = 0;i < count-1;i++) {
      cout<<s->info<<"<->";
      s = s->n;
   }
   cout<<s->info<<endl;
}
void circulardoublylist::reverse() {
   if (start == last && start == NULL) {
      cout<<"The List is empty, nothing to reverse"<<endl;
      return;
   }
   struct nod *p1, *p2;
   p1 = start;
   p2 = p1->n;
   p1->n = NULL;
   p1->p= p2;
   while (p2 != start) {
      p2->p = p2->n;
      p2->n = p1;
      p1 = p2;
      p2 = p2->p;
   }
   last = start;
   start = p1;
   cout<<"List Reversed"<<endl;
}

出力

1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 1
Enter the element to be inserted: 7
Element inserted in empty list
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 1
Enter the element to be inserted: 6
Element inserted
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 2
Enter the element to be inserted: 4
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 2
Enter the element to be inserted: 5
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 6
6<->7<->4<->5
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 5
List sorted
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 4
Enter the position of element to be deleted: 3
Element Deleted
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 6
4<->5<->7
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 7
List Reversed
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 6
7<->5<->4
1.Insert at Beginning
2.Insert at End
3.Insert at Position
4.Delete at Position
5.sort the list
6.Display List
7.Reverse List
8.Exit
Enter your choice : 8

  1. C++で二重リンクリストのサイズを見つけるプログラム

    この問題では、二重にリンクされたリストが与えられます。私たちのタスクは、C++で二重リンクリストのサイズを見つけるプログラムを作成することです。 二重リンクリストは特殊なタイプのリンクリストであり、単一リンクリストと比較して、順方向と逆方向の両方の方法で簡単にナビゲーションできます。以下は、二重リンクリストの概念を理解するための重要な用語です。 リンク-リンクリストの各リンクには、要素と呼ばれるデータを格納できます。 次へ-リンクリストの各リンクには、次と呼ばれる次のリンクへのリンクが含まれています。 前-リンクリストの各リンクには、前と呼ばれる前のリンクへのリンクが含ま

  2. 隣接リストを実装するC++プログラム

    グラフの隣接リスト表現は、リンクリスト表現です。この表現では、リストの配列があります。配列のサイズはVです。ここで、Vは頂点の数です。つまり、V個の異なるリストを格納する配列があると言えます。リストヘッダーが頂点uの場合、uの隣接するすべての頂点を保持することを意味します。 隣接リスト表現の複雑さ この表現は、無向グラフの場合はO(V + 2E)を取り、有向グラフの場合はO(V + E)を取ります。エッジの数を増やすと、必要なスペースも増えます。 入力 : 出力 : アルゴリズム add_edge(adj_list、u、v) 入力 :エッジ{u、v}のuとv、およ