Razvit povezan seznam | 1. sklop (uvod)

Razvit povezan seznam | 1. sklop (uvod)

Tako kot niz in povezani seznam je tudi razviti povezani seznam linearna podatkovna struktura in je različica povezanega seznama. 

Zakaj potrebujemo razvit povezan seznam?

Ena največjih prednosti povezanih seznamov v primerjavi z nizi je, da vstavljanje elementa na poljubno mesto zahteva samo O(1). Vendar pa je ulov tukaj v tem, da iskanje elementa na povezanem seznamu zahteva O(n). Da bi rešili problem iskanja, tj. skrajšanje časa za iskanje elementa, je bil predstavljen koncept razvitih povezanih seznamov. Odprti povezani seznam pokriva prednosti matričnega in povezanega seznama, saj zmanjša obremenitev pomnilnika v primerjavi s preprostimi povezanimi seznami, tako da shrani več elementov v vsako vozlišče, poleg tega pa ima prednost hitrega vstavljanja in brisanja kot pri povezanem seznamu.

Razvit povezan seznam | 1. sklop (uvod) unrolledlinkedlist

Prednosti:

  • Zaradi obnašanja predpomnilnika je linearno iskanje veliko hitrejše v odvitih povezanih seznamih.
  • V primerjavi z navadnim povezanim seznamom potrebuje manj prostora za shranjevanje kazalcev/referenc.
  • Izvaja operacije, kot sta vstavljanje, brisanje in prehod hitreje kot običajni povezani seznami (ker je iskanje hitrejše).

Slabosti:

  • Stroški na vozlišče so sorazmerno visoki kot enojno povezani seznami. Oglejte si primer vozlišča v spodnji kodi

primer: Recimo, da imamo 8 elementov, torej sqrt(8)=2,82, kar zaokroži na 3. Torej bo vsak blok shranil 3 elemente. Zato bodo za shranjevanje 8 elementov ustvarjeni 3 bloki, od katerih bosta prva dva bloka shranila 3 elemente, zadnji blok pa 2 elementa.

Kako postane iskanje boljše v razvitih povezanih seznamih?

Če torej vzamemo zgornji primer, če želimo poiskati sedmi element na seznamu, prečkamo seznam blokov do tistega, ki vsebuje sedmi element. Potrebuje le O(sqrt(n)), saj smo ga našli tako, da ne gre več kot sqrt(n) blokov. 

Preprosta izvedba:

Spodnji program ustvari preprost razvit povezan seznam s 3 vozlišči, ki vsebujejo spremenljivo število elementov v vsakem. Prav tako prečka ustvarjen seznam.

C++
   // C++ program to implement unrolled linked list    // and traversing it.    #include          using     namespace     std  ;   #define maxElements 4    // Unrolled Linked List Node    class     Node      {         public  :      int     numElements  ;         int     array  [  maxElements  ];         Node     *  next  ;      };      /* Function to traverse an unrolled linked list    and print all the elements*/   void     printUnrolledList  (  Node     *  n  )      {         while     (  n     !=     NULL  )         {         // Print elements in current node       for     (  int     i  =  0  ;     i   <  n  ->  numElements  ;     i  ++  )         cout   < <  n  ->  array  [  i  ]   < <  ' '  ;         // Move to next node       n     =     n  ->  next  ;         }      }      // Program to create an unrolled linked list    // with 3 Nodes    int     main  ()      {         Node  *     head     =     NULL  ;         Node  *     second     =     NULL  ;         Node  *     third     =     NULL  ;         // allocate 3 Nodes       head     =     new     Node  ();      second     =     new     Node  ();      third     =     new     Node  ();      // Let us put some values in second node (Number       // of values must be less than or equal to       // maxElement)       head  ->  numElements     =     3  ;         head  ->  array  [  0  ]     =     1  ;         head  ->  array  [  1  ]     =     2  ;         head  ->  array  [  2  ]     =     3  ;         // Link first Node with the second Node       head  ->  next     =     second  ;         // Let us put some values in second node (Number       // of values must be less than or equal to       // maxElement)       second  ->  numElements     =     3  ;         second  ->  array  [  0  ]     =     4  ;         second  ->  array  [  1  ]     =     5  ;         second  ->  array  [  2  ]     =     6  ;         // Link second Node with the third Node       second  ->  next     =     third  ;         // Let us put some values in third node (Number       // of values must be less than or equal to       // maxElement)       third  ->  numElements     =     3  ;         third  ->  array  [  0  ]     =     7  ;         third  ->  array  [  1  ]     =     8  ;         third  ->  array  [  2  ]     =     9  ;         third  ->  next     =     NULL  ;         printUnrolledList  (  head  );         return     0  ;      }      // This is code is contributed by rathbhupendra   
C
   // C program to implement unrolled linked list   // and traversing it.   #include      #include      #define maxElements 4   // Unrolled Linked List Node   struct     Node   {      int     numElements  ;      int     array  [  maxElements  ];      struct     Node     *  next  ;   };   /* Function to traverse an unrolled linked list    and print all the elements*/   void     printUnrolledList  (  struct     Node     *  n  )   {      while     (  n     !=     NULL  )      {      // Print elements in current node      for     (  int     i  =  0  ;     i   <  n  ->  numElements  ;     i  ++  )      printf  (  '%d '       n  ->  array  [  i  ]);      // Move to next node       n     =     n  ->  next  ;      }   }   // Program to create an unrolled linked list   // with 3 Nodes   int     main  ()   {      struct     Node  *     head     =     NULL  ;      struct     Node  *     second     =     NULL  ;      struct     Node  *     third     =     NULL  ;      // allocate 3 Nodes      head     =     (  struct     Node  *  )  malloc  (  sizeof  (  struct     Node  ));      second     =     (  struct     Node  *  )  malloc  (  sizeof  (  struct     Node  ));      third     =     (  struct     Node  *  )  malloc  (  sizeof  (  struct     Node  ));      // Let us put some values in second node (Number      // of values must be less than or equal to      // maxElement)      head  ->  numElements     =     3  ;      head  ->  array  [  0  ]     =     1  ;      head  ->  array  [  1  ]     =     2  ;      head  ->  array  [  2  ]     =     3  ;      // Link first Node with the second Node      head  ->  next     =     second  ;      // Let us put some values in second node (Number      // of values must be less than or equal to      // maxElement)      second  ->  numElements     =     3  ;      second  ->  array  [  0  ]     =     4  ;      second  ->  array  [  1  ]     =     5  ;      second  ->  array  [  2  ]     =     6  ;      // Link second Node with the third Node      second  ->  next     =     third  ;      // Let us put some values in third node (Number      // of values must be less than or equal to      // maxElement)      third  ->  numElements     =     3  ;      third  ->  array  [  0  ]     =     7  ;      third  ->  array  [  1  ]     =     8  ;      third  ->  array  [  2  ]     =     9  ;      third  ->  next     =     NULL  ;      printUnrolledList  (  head  );      return     0  ;   }   
Java
   // Java program to implement unrolled   // linked list and traversing it.    import     java.util.*  ;   class   GFG  {       static     final     int     maxElements     =     4  ;   // Unrolled Linked List Node    static     class   Node      {         int     numElements  ;         int     []  array     =     new     int  [  maxElements  ]  ;         Node     next  ;      };      // Function to traverse an unrolled    // linked list and print all the elements   static     void     printUnrolledList  (  Node     n  )      {         while     (  n     !=     null  )         {             // Print elements in current node       for  (  int     i     =     0  ;     i      <     n  .  numElements  ;     i  ++  )         System  .  out  .  print  (  n  .  array  [  i  ]     +     ' '  );         // Move to next node       n     =     n  .  next  ;         }      }      // Program to create an unrolled linked list    // with 3 Nodes    public     static     void     main  (  String  []     args  )      {         Node     head     =     null  ;         Node     second     =     null  ;         Node     third     =     null  ;         // Allocate 3 Nodes       head     =     new     Node  ();      second     =     new     Node  ();      third     =     new     Node  ();      // Let us put some values in second       // node (Number of values must be       // less than or equal to maxElement)       head  .  numElements     =     3  ;         head  .  array  [  0  ]     =     1  ;         head  .  array  [  1  ]     =     2  ;         head  .  array  [  2  ]     =     3  ;         // Link first Node with the       // second Node       head  .  next     =     second  ;         // Let us put some values in       // second node (Number of values      // must be less than or equal to       // maxElement)       second  .  numElements     =     3  ;         second  .  array  [  0  ]     =     4  ;         second  .  array  [  1  ]     =     5  ;         second  .  array  [  2  ]     =     6  ;         // Link second Node with the third Node       second  .  next     =     third  ;         // Let us put some values in third       // node (Number of values must be      // less than or equal to maxElement)       third  .  numElements     =     3  ;         third  .  array  [  0  ]     =     7  ;         third  .  array  [  1  ]     =     8  ;         third  .  array  [  2  ]     =     9  ;         third  .  next     =     null  ;         printUnrolledList  (  head  );      }      }      // This code is contributed by amal kumar choubey    
Python3
   # Python3 program to implement unrolled   # linked list and traversing it.    maxElements   =   4   # Unrolled Linked List Node    class   Node  :   def   __init__  (  self  ):   self  .  numElements   =   0   self  .  array   =   [  0   for   i   in   range  (  maxElements  )]   self  .  next   =   None   # Function to traverse an unrolled linked list    # and print all the elements   def   printUnrolledList  (  n  ):   while   (  n   !=   None  ):   # Print elements in current node    for   i   in   range  (  n  .  numElements  ):   print  (  n  .  array  [  i  ]   end   =   ' '  )   # Move to next node    n   =   n  .  next   # Driver Code   if   __name__  ==  '__main__'  :   head   =   None   second   =   None   third   =   None   # Allocate 3 Nodes    head   =   Node  ()   second   =   Node  ()   third   =   Node  ()   # Let us put some values in second   # node (Number of values must be    # less than or equal to    # maxElement)    head  .  numElements   =   3   head  .  array  [  0  ]   =   1   head  .  array  [  1  ]   =   2   head  .  array  [  2  ]   =   3   # Link first Node with the second Node    head  .  next   =   second   # Let us put some values in second node   # (Number of values must be less than   # or equal to maxElement)    second  .  numElements   =   3   second  .  array  [  0  ]   =   4   second  .  array  [  1  ]   =   5   second  .  array  [  2  ]   =   6   # Link second Node with the third Node    second  .  next   =   third   # Let us put some values in third node   # (Number of values must be less than    # or equal to maxElement)    third  .  numElements   =   3   third  .  array  [  0  ]   =   7   third  .  array  [  1  ]   =   8   third  .  array  [  2  ]   =   9   third  .  next   =   None   printUnrolledList  (  head  )   # This code is contributed by rutvik_56   
C#
   // C# program to implement unrolled   // linked list and traversing it.    using     System  ;   class     GFG  {       static     readonly     int     maxElements     =     4  ;   // Unrolled Linked List Node    class     Node      {         public     int     numElements  ;         public     int     []  array     =     new     int  [  maxElements  ];         public     Node     next  ;      };      // Function to traverse an unrolled    // linked list and print all the elements   static     void     printUnrolledList  (  Node     n  )      {         while     (  n     !=     null  )         {         // Print elements in current node       for  (  int     i     =     0  ;     i      <     n  .  numElements  ;     i  ++  )         Console  .  Write  (  n  .  array  [  i  ]     +     ' '  );         // Move to next node       n     =     n  .  next  ;         }      }      // Program to create an unrolled linked list    // with 3 Nodes    public     static     void     Main  (  String  []     args  )      {         Node     head     =     null  ;         Node     second     =     null  ;         Node     third     =     null  ;         // Allocate 3 Nodes       head     =     new     Node  ();      second     =     new     Node  ();      third     =     new     Node  ();      // Let us put some values in second       // node (Number of values must be       // less than or equal to maxElement)       head  .  numElements     =     3  ;         head  .  array  [  0  ]     =     1  ;         head  .  array  [  1  ]     =     2  ;         head  .  array  [  2  ]     =     3  ;         // Link first Node with the       // second Node       head  .  next     =     second  ;         // Let us put some values in       // second node (Number of values      // must be less than or equal to       // maxElement)       second  .  numElements     =     3  ;         second  .  array  [  0  ]     =     4  ;         second  .  array  [  1  ]     =     5  ;         second  .  array  [  2  ]     =     6  ;         // Link second Node with the third Node       second  .  next     =     third  ;         // Let us put some values in third       // node (Number of values must be      // less than or equal to maxElement)       third  .  numElements     =     3  ;         third  .  array  [  0  ]     =     7  ;         third  .  array  [  1  ]     =     8  ;         third  .  array  [  2  ]     =     9  ;         third  .  next     =     null  ;         printUnrolledList  (  head  );      }      }      // This code is contributed by Rajput-Ji    
JavaScript
    <  script  >      // JavaScript program to implement unrolled      // linked list and traversing it.      const     maxElements     =     4  ;      // Unrolled Linked List Node      class     Node     {      constructor  ()     {      this  .  numElements     =     0  ;      this  .  array     =     new     Array  (  maxElements  );      this  .  next     =     null  ;      }      }      // Function to traverse an unrolled      // linked list and print all the elements      function     printUnrolledList  (  n  )     {      while     (  n     !=     null  )     {      // Print elements in current node      for     (  var     i     =     0  ;     i      <     n  .  numElements  ;     i  ++  )      document  .  write  (  n  .  array  [  i  ]     +     ' '  );      // Move to next node      n     =     n  .  next  ;      }      }      // Program to create an unrolled linked list      // with 3 Nodes      var     head     =     null  ;      var     second     =     null  ;      var     third     =     null  ;      // Allocate 3 Nodes      head     =     new     Node  ();      second     =     new     Node  ();      third     =     new     Node  ();      // Let us put some values in second      // node (Number of values must be      // less than or equal to maxElement)      head  .  numElements     =     3  ;      head  .  array  [  0  ]     =     1  ;      head  .  array  [  1  ]     =     2  ;      head  .  array  [  2  ]     =     3  ;      // Link first Node with the      // second Node      head  .  next     =     second  ;      // Let us put some values in      // second node (Number of values      // must be less than or equal to      // maxElement)      second  .  numElements     =     3  ;      second  .  array  [  0  ]     =     4  ;      second  .  array  [  1  ]     =     5  ;      second  .  array  [  2  ]     =     6  ;      // Link second Node with the third Node      second  .  next     =     third  ;      // Let us put some values in third      // node (Number of values must be      // less than or equal to maxElement)      third  .  numElements     =     3  ;      third  .  array  [  0  ]     =     7  ;      third  .  array  [  1  ]     =     8  ;      third  .  array  [  2  ]     =     9  ;      third  .  next     =     null  ;      printUnrolledList  (  head  );        <  /script>   

Izhod
1 2 3 4 5 6 7 8 9  

Analiza kompleksnosti:

    Časovna zahtevnost: O(n). Kompleksnost prostora: O(n).

V tem članku smo predstavili razvit seznam in njegove prednosti. Pokazali smo tudi, kako prečkati seznam. V naslednjem članku bomo podrobno razpravljali o izbrisu vstavljanja in vrednostih maxElements/numElements.

Vstavljanje v razvit povezani seznam

 

Ustvari kviz