سنتعلم في هذه المقالة كيفية إدراج عقدة في قائمة مرتبطة دائرية. يعد الإدراج عملية أساسية في القوائم المرتبطة والتي تتضمن إضافة عقدة جديدة إلى القائمة. في القائمة المرتبطة الدائرية، تتصل العقدة الأخيرة مرة أخرى بالعقدة الأولى لإنشاء حلقة.
هناك أربع طرق رئيسية لإضافة العناصر:
- الإدراج في قائمة فارغة
- الإدراج في بداية القائمة
- الإدراج في نهاية القائمة
- الإدراج في موضع محدد في القائمة
مزايا استخدام مؤشر الذيل بدلاً من مؤشر الرأس
نحن بحاجة إلى اجتياز القائمة بأكملها لإدراج عقدة في البداية. أيضًا للإدراج في النهاية، يجب اجتياز القائمة بأكملها. إذا بدلا من يبدأ المؤشر نأخذ مؤشرًا إلى العقدة الأخيرة، وفي كلتا الحالتين لن تكون هناك حاجة لاجتياز القائمة بأكملها. لذا فإن الإدراج في البداية أو النهاية يستغرق وقتًا ثابتًا بغض النظر عن طول القائمة.
1. الإدراج في قائمة فارغة في القائمة المرتبطة الدائرية
لإدراج عقدة في قائمة مرتبطة دائرية فارغة، يتم إنشاء ملف عقدة جديدة باستخدام البيانات المعطاة، يقوم بتعيين المؤشر التالي للإشارة إلى نفسه وتحديث الملف آخر المؤشر للإشارة إلى هذا عقدة جديدة .
الإدراج في قائمة فارغةنهج خطوة بخطوة:
- تحقق مما إذا آخر ليس كذلك nullptr . لو حقيقي يعود آخر (القائمة ليست فارغة).
- وإلا قم بإنشاء عقدة جديدة مع البيانات المقدمة.
- تعيين عقدة جديدة المؤشر التالي للإشارة إلى نفسه (رابط دائري).
- تحديث آخر للإشارة إلى عقدة جديدة وإعادته.
لقراءة المزيد حول الإدراج في قائمة فارغة، راجع: الإدراج في قائمة فارغة في القائمة المرتبطة الدائرية
2. الإدراج في البداية في قائمة مرتبطة دائرية
لإدراج عقدة جديدة في بداية القائمة المرتبطة الدائرية
- نقوم أولا بإنشاء عقدة جديدة وتخصيص الذاكرة لذلك.
- إذا كانت القائمة فارغة (يُشار إليها بالمؤشر الأخير باطل ) نحن نصنع عقدة جديدة تشير إلى نفسها.
- إذا كانت القائمة تحتوي بالفعل على العقد، فإننا نقوم بتعيين عقدة جديدة المؤشر التالي للإشارة إلى الرأس الحالي من القائمة (وهو الأخير->التالي )
- ثم قم بتحديث المؤشر التالي للعقدة الأخيرة للإشارة إلى عقدة جديدة . وهذا يحافظ على البنية الدائرية للقائمة.
الإدراج في البداية في قائمة مرتبطة دائرية لقراءة المزيد عن الإدراج في البداية، راجع: الإدراج في البداية في قائمة مرتبطة دائرية
3. الإدراج في النهاية في قائمة مرتبطة دائرية
لإدراج عقدة جديدة في نهاية القائمة المرتبطة الدائرية، نقوم أولاً بإنشاء العقدة الجديدة وتخصيص الذاكرة لها.
- إذا كانت القائمة فارغة (يعني آخر أو ذيل كائن المؤشر باطل ) نقوم بتهيئة القائمة باستخدام عقدة جديدة وجعلها تشير إلى نفسها لتشكل هيكلاً دائرياً.
- إذا كانت القائمة تحتوي بالفعل على العقد، فإننا نقوم بتعيين عقدة جديدة المؤشر التالي للإشارة إلى الرأس الحالي (وهو الذيل->التالي )
- ثم قم بتحديث الذيل الحالي المؤشر التالي للإشارة إلى عقدة جديدة .
- وأخيرا نقوم بتحديث مؤشر الذيل إلى عقدة جديدة.
- وهذا سيضمن أن عقدة جديدة هو الآن العقدة الأخيرة في القائمة مع الحفاظ على الارتباط الدائري.
الإدراج في النهاية في قائمة مرتبطة دائرية لقراءة المزيد حول الإدراج في النهاية، راجع: الإدراج في النهاية في قائمة مرتبطة دائرية
4. الإدراج في موضع محدد في القائمة المرتبطة الدائرية
لإدراج عقدة جديدة في موضع محدد في قائمة مرتبطة دائرية، علينا أولاً التحقق مما إذا كانت القائمة فارغة.
- إذا كان و موضع ليس كذلك 1 ثم نطبع رسالة خطأ لأن المنصب غير موجود في القائمة. أنا
- و موضع يكون 1 ثم نقوم بإنشاء عقدة جديدة وجعله يشير إلى نفسه.
- إذا لم تكن القائمة فارغة نقوم بإنشاء عقدة جديدة واجتياز القائمة للعثور على نقطة الإدراج الصحيحة.
- إذا موضع يكون 1 نقوم بإدخال عقدة جديدة في البداية عن طريق ضبط المؤشرات وفقًا لذلك.
- بالنسبة للمواضع الأخرى، نتنقل عبر القائمة حتى نصل إلى الموضع المطلوب ونقوم بإدخال الملف عقدة جديدة عن طريق تحديث المؤشرات
- إذا تم إدراج العقدة الجديدة في النهاية، فإننا نقوم أيضًا بتحديث ملف آخر مؤشر للإشارة إلى العقدة الجديدة التي تحافظ على البنية الدائرية للقائمة.
الإدراج في موضع محدد في القائمة المرتبطة الدائريةنهج خطوة بخطوة:
- لو آخر يكون nullptr و نقاط البيع ليس كذلك 1 مطبعة ' موقف غير صالح! '.
- بخلاف ذلك، قم بإنشاء عقدة جديدة ببيانات معينة.
- أدخل في البداية: إذا كان pos هو 1، قم بتحديث المؤشرات والعودة أخيرًا.
- قائمة العبور: حلقة للعثور على نقطة الإدراج؛ طباعة "موضع غير صالح!" إذا كان خارج الحدود.
- إدراج العقدة: قم بتحديث المؤشرات لإدراج العقدة الجديدة.
- التحديث الأخير: إذا تم إدراجها في التحديث النهائي آخر .
#include using namespace std; struct Node{ int data; Node *next; Node(int value){ data = value; next = nullptr; } }; // Function to insert a node at a specific position in a circular linked list Node *insertAtPosition(Node *last int data int pos){ if (last == nullptr){ // If the list is empty if (pos != 1){ cout << 'Invalid position!' << endl; return last; } // Create a new node and make it point to itself Node *newNode = new Node(data); last = newNode; last->next = last; return last; } // Create a new node with the given data Node *newNode = new Node(data); // curr will point to head initially Node *curr = last->next; if (pos == 1){ // Insert at the beginning newNode->next = curr; last->next = newNode; return last; } // Traverse the list to find the insertion point for (int i = 1; i < pos - 1; ++i) { curr = curr->next; // If position is out of bounds if (curr == last->next){ cout << 'Invalid position!' << endl; return last; } } // Insert the new node at the desired position newNode->next = curr->next; curr->next = newNode; // Update last if the new node is inserted at the end if (curr == last) last = newNode; return last; } void printList(Node *last){ if (last == NULL) return; Node *head = last->next; while (true){ cout << head->data << ' '; head = head->next; if (head == last->next) break; } cout << endl; } int main(){ // Create circular linked list: 2 3 4 Node *first = new Node(2); first->next = new Node(3); first->next->next = new Node(4); Node *last = first->next->next; last->next = first; cout << 'Original list: '; printList(last); // Insert elements at specific positions int data = 5 pos = 2; last = insertAtPosition(last data pos); cout << 'List after insertions: '; printList(last); return 0; }
C #include #include // Define the Node structure struct Node { int data; struct Node *next; }; struct Node* createNode(int value); // Function to insert a node at a specific position in a circular linked list struct Node* insertAtPosition(struct Node *last int data int pos) { if (last == NULL) { // If the list is empty if (pos != 1) { printf('Invalid position!n'); return last; } // Create a new node and make it point to itself struct Node *newNode = createNode(data); last = newNode; last->next = last; return last; } // Create a new node with the given data struct Node *newNode = createNode(data); // curr will point to head initially struct Node *curr = last->next; if (pos == 1) { // Insert at the beginning newNode->next = curr; last->next = newNode; return last; } // Traverse the list to find the insertion point for (int i = 1; i < pos - 1; ++i) { curr = curr->next; // If position is out of bounds if (curr == last->next) { printf('Invalid position!n'); return last; } } // Insert the new node at the desired position newNode->next = curr->next; curr->next = newNode; // Update last if the new node is inserted at the end if (curr == last) last = newNode; return last; } // Function to print the circular linked list void printList(struct Node *last) { if (last == NULL) return; struct Node *head = last->next; while (1) { printf('%d ' head->data); head = head->next; if (head == last->next) break; } printf('n'); } // Function to create a new node struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; return newNode; } int main() { // Create circular linked list: 2 3 4 struct Node *first = createNode(2); first->next = createNode(3); first->next->next = createNode(4); struct Node *last = first->next->next; last->next = first; printf('Original list: '); printList(last); // Insert elements at specific positions int data = 5 pos = 2; last = insertAtPosition(last data pos); printf('List after insertions: '); printList(last); return 0; }
Java class Node { int data; Node next; Node(int value){ data = value; next = null; } } public class GFG { // Function to insert a node at a specific position in a // circular linked list static Node insertAtPosition(Node last int data int pos){ if (last == null) { // If the list is empty if (pos != 1) { System.out.println('Invalid position!'); return last; } // Create a new node and make it point to itself Node newNode = new Node(data); last = newNode; last.next = last; return last; } // Create a new node with the given data Node newNode = new Node(data); // curr will point to head initially Node curr = last.next; if (pos == 1) { // Insert at the beginning newNode.next = curr; last.next = newNode; return last; } // Traverse the list to find the insertion point for (int i = 1; i < pos - 1; ++i) { curr = curr.next; // If position is out of bounds if (curr == last.next) { System.out.println('Invalid position!'); return last; } } // Insert the new node at the desired position newNode.next = curr.next; curr.next = newNode; // Update last if the new node is inserted at the // end if (curr == last) last = newNode; return last; } static void printList(Node last){ if (last == null) return; Node head = last.next; while (true) { System.out.print(head.data + ' '); head = head.next; if (head == last.next) break; } System.out.println(); } public static void main(String[] args) { // Create circular linked list: 2 3 4 Node first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); Node last = first.next.next; last.next = first; System.out.print('Original list: '); printList(last); // Insert elements at specific positions int data = 5 pos = 2; last = insertAtPosition(last data pos); System.out.print('List after insertions: '); printList(last); } }
Python class Node: def __init__(self value): self.data = value self.next = None # Function to insert a node at a specific position in a circular linked list def insertAtPosition(last data pos): if last is None: # If the list is empty if pos != 1: print('Invalid position!') return last # Create a new node and make it point to itself new_node = Node(data) last = new_node last.next = last return last # Create a new node with the given data new_node = Node(data) # curr will point to head initially curr = last.next if pos == 1: # Insert at the beginning new_node.next = curr last.next = new_node return last # Traverse the list to find the insertion point for i in range(1 pos - 1): curr = curr.next # If position is out of bounds if curr == last.next: print('Invalid position!') return last # Insert the new node at the desired position new_node.next = curr.next curr.next = new_node # Update last if the new node is inserted at the end if curr == last: last = new_node return last # Function to print the circular linked list def print_list(last): if last is None: return head = last.next while True: print(head.data end=' ') head = head.next if head == last.next: break print() if __name__ == '__main__': # Create circular linked list: 2 3 4 first = Node(2) first.next = Node(3) first.next.next = Node(4) last = first.next.next last.next = first print('Original list: ' end='') print_list(last) # Insert elements at specific positions data = 5 pos = 2 last = insertAtPosition(last data pos) print('List after insertions: ' end='') print_list(last)
JavaScript class Node { constructor(value){ this.data = value; this.next = null; } } // Function to insert a node at a specific position in a // circular linked list function insertAtPosition(last data pos) { if (last === null) { // If the list is empty if (pos !== 1) { console.log('Invalid position!'); return last; } // Create a new node and make it point to itself let newNode = new Node(data); last = newNode; last.next = last; return last; } // Create a new node with the given data let newNode = new Node(data); // curr will point to head initially let curr = last.next; if (pos === 1) { // Insert at the beginning newNode.next = curr; last.next = newNode; return last; } // Traverse the list to find the insertion point for (let i = 1; i < pos - 1; ++i) { curr = curr.next; // If position is out of bounds if (curr === last.next) { console.log('Invalid position!'); return last; } } // Insert the new node at the desired position newNode.next = curr.next; curr.next = newNode; // Update last if the new node is inserted at the end if (curr === last) last = newNode; return last; } // Function to print the circular linked list function printList(last){ if (last === null) return; let head = last.next; while (true) { console.log(head.data + ' '); head = head.next; if (head === last.next) break; } console.log(); } // Create circular linked list: 2 3 4 let first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); let last = first.next.next; last.next = first; console.log('Original list: '); printList(last); // Insert elements at specific positions let data = 5; let pos = 2; last = insertAtPosition(last data pos); console.log('List after insertions: '); printList(last);
الإخراج
Original list: 2 3 4 List after insertions: 2 5 3 4
تعقيد الوقت: O(n) يتعين علينا اجتياز القائمة للعثور على الموضع المحدد.
المساحة المساعدة: يا(1)