배열 및 연결 목록과 마찬가지로 펼쳐진 연결 목록도 선형 데이터 구조이며 연결 목록의 변형입니다.
펼쳐진 연결 목록이 필요한 이유는 무엇입니까?
배열에 비해 연결 목록의 가장 큰 장점 중 하나는 임의의 위치에 요소를 삽입하는 데 O(1)만 소요된다는 것입니다. 그러나 여기서 중요한 점은 연결된 목록의 요소를 검색하려면 O(n)이 필요하다는 것입니다. 그래서 검색 문제를 해결하기 위해, 즉 요소 검색 시간을 줄이기 위해 펼쳐진 연결 목록(unrolled linked list)이라는 개념이 제시되었습니다. 언롤링 연결리스트는 배열과 연결리스트의 장점을 모두 갖고 있어 각 노드에 여러 개의 요소를 저장함으로써 단순 연결리스트에 비해 메모리 오버헤드를 줄이고, 연결리스트처럼 빠른 삽입과 삭제가 가능한 장점도 있다.
우선순위 대기열

장점:
- 캐시 동작으로 인해 선형 검색은 펼쳐진 연결 목록에서 훨씬 빠릅니다.
- 일반 연결 리스트에 비해 포인터/참조를 위한 저장 공간이 덜 필요합니다.
- 일반 연결 목록보다 삽입 삭제 및 순회와 같은 작업을 더 빠르게 수행합니다(검색이 더 빠르기 때문).
단점:
knn 알고리즘
- 노드당 오버헤드는 단일 연결 목록보다 상대적으로 높습니다. 아래 코드의 예제 노드를 참조하세요.
예: 8개의 요소가 있다고 가정하면 sqrt(8)=2.82는 3으로 반올림됩니다. 따라서 각 블록은 3개의 요소를 저장합니다. 따라서 8개의 요소를 저장하려면 3개의 블록이 생성되며 그 중 처음 두 블록은 3개의 요소를 저장하고 마지막 블록은 2개의 요소를 저장합니다.
펼쳐진 연결 목록에서 검색이 어떻게 향상됩니까?
따라서 위의 예를 사용하면 목록에서 7번째 요소를 검색하려면 블록 목록을 7번째 요소가 포함된 목록으로 이동합니다. sqrt(n) 블록을 넘지 않고 발견했기 때문에 O(sqrt(n))만 필요합니다.
간단한 구현:
아래 프로그램은 각각 가변 개수의 요소를 포함하는 3개의 노드가 있는 간단한 펼쳐진 연결 목록을 만듭니다. 또한 생성된 목록을 순회합니다.
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>
산출
1 2 3 4 5 6 7 8 9
복잡성 분석:
이 기사에서는 전체 목록과 그 장점을 소개했습니다. 또한 목록을 탐색하는 방법도 보여주었습니다. 다음 글에서는 삽입삭제와 maxElements/numElements 값에 대해 자세히 다루겠습니다.
펼쳐진 연결 목록에 삽입
이메일