اذهب إلى المحتوى
  • 0

كيف يمكني عمل circular doubly linked list

Mari Carmen

السؤال

لكل node لها next و prev 

وpointer واحد الي هو "head "  باشر على اول node  و head->next باشر على اخر node  

وnext  تاع اخر node باشر على اول node

وكل node عندها pointer اضافي باشر على اخر node وpointer اخر ياشر على  head 

وهذه رسمت لينكد ليست 

وكيف يمكني عمل addfront و addback 

Screenshot 2021-03-24 161456.png

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

لنفترض تريد بنائها على لغة c++

البناء يتطب عمل struct او class:

#include<iostream>
using namespace std;

class double_linkedlist
{
	public:
  		//structure of the node
  		struct node
        {
            string data;
            node* next;
            node* prev;
            node(string x)
            {
                data = x;
                next = NULL;
                prev = NULL;
            }
        };
  		//the head and tail nodes
        node* head;
        node* tail;
  
  		//The functions
  		double_linkedlist();		//constructor
  		~double_linkedlist();		//destructor
     	void addFront(string);      //add in the front
        void addBack(string); 		//add in the back
}

//constructor
doubleLinkedList::doubleLinkedList(){
    head = tail = NULL;
}


//destructor
doubleLinkedList::~doubleLinkedList(){
    node* current = head;

    while(current != NULL)
    {
        node* previous = current;
        current = current->next;
        delete previous;
    }

    head = tail = NULL;
}
//Your functions is in below
void doubleLinkedList::addFront(int x){

    node* n = new node(x);
    n->next = head;
    n->prev = NULL;

    if (head != NULL)
        head->prev = n;
        head = n;

    if (tail == NULL)
        tail = n;
}

void doubleLinkedList::addBack(int x){
    node* n = new node(x);

    n->next = NULL;
    n->prev = tail;
    tail = n;
}

//The main fucntion
int main()
{
  doubleLinkedList list1;
 	return 0; 
}

يمكنك استدعاء الدالة في main بالشكل التالي:

list1->addFront("ypur name");
list1->addBack("Example"):

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...