📜  将数组转换为圆形双链表

📅  最后修改于: 2021-04-17 09:08:26             🧑  作者: Mango

先决条件:双向链接列表,循环链接列表,循环双向链接列表

给定N个元素的数组。任务是编写一个程序,将数组转换为循环的双向链表。

图像表示

这个想法是开始遍历数组并为每个数组元素创建一个新的列表节点,并相应地分配此节点的上一个下一个指针。

  • 创建一个指针,开始指向列表的开始,该指针最初将指向NULL(空列表)。
  • 对于数组的第一个元素,创建一个新的节点,并把该节点的上一个下一个指针点开始,以保持列表的循环方式。
  • 对于其余的数组元素,请将这些元素插入到创建的圆形双向链表的末尾。

下面是上述想法的实现:

C++
// CPP program to convert array to 
// circular doubly linked list
  
#include
using namespace std;
  
// Doubly linked list node
struct node
{
    int data;
    struct node *next;
    struct node *prev;
};
  
// Utility function to create a node in memory
struct node* getNode()
{
    return ((struct node *)malloc(sizeof(struct node)));
}
  
// Function to display the list
int displayList(struct node *temp)
{
    struct node *t = temp;
    if(temp == NULL)
        return 0;
    else
    {   
        cout<<"The list is: ";
          
        while(temp->next != t)
        {
            cout<data<<" ";
            temp = temp->next;
        }
          
        cout<data;
          
        return 1;
    } 
}
  
// Function to convert array into list
void createList(int arr[], int n, struct node **start)
{
    // Declare newNode and temporary pointer
    struct node *newNode,*temp;
    int i;
      
    // Iterate the loop until array length
    for(i=0;idata = arr[i];
          
        // If it is first element
        // Put that node prev and next as start
        // as it is circular
        if(i==0)
        {
            *start = newNode;
            newNode->prev = *start;
            newNode->next = *start;
        }
        else
        {   
            // Find the last node
            temp = (*start)->prev;
              
            // Add the last node to make them
            // in circular fashion
            temp->next = newNode;
            newNode->next = *start;
            newNode->prev = temp;
            temp = *start;
            temp->prev = newNode;
        }
    }
}
  
// Driver Code
int main()
{
    // Array to be converted
    int arr[] = {1,2,3,4,5};
    int n = sizeof(arr) / sizeof(arr[0]);
      
    // Start Pointer
    struct node *start = NULL;
      
    // Create the List
    createList(arr, n, &start);
      
    // Display the list
    displayList(start);
      
    return 0;
}


Java
// Java program to convert array to 
// circular doubly linked list 
class GFG
{
      
// Doubly linked list node 
static class node 
{ 
    int data; 
    node next; 
    node prev; 
}; 
  
// Utility function to create a node in memory 
static node getNode() 
{ 
    return new node(); 
} 
  
// Function to display the list 
static int displayList( node temp) 
{ 
    node t = temp; 
    if(temp == null) 
        return 0; 
    else
    { 
        System.out.print("The list is: "); 
          
        while(temp.next != t) 
        { 
            System.out.print(temp.data+" ");
            temp = temp.next; 
        } 
          
        System.out.print(temp.data); 
          
        return 1; 
    } 
} 
  
// Function to convert array into list 
static node createList(int arr[], int n, node start) 
{ 
    // Declare newNode and temporary pointer 
    node newNode,temp; 
    int i; 
      
    // Iterate the loop until array length 
    for(i = 0; i < n; i++) 
    { 
        // Create new node 
        newNode = getNode(); 
          
        // Assign the array data 
        newNode.data = arr[i]; 
          
        // If it is first element 
        // Put that node prev and next as start 
        // as it is circular 
        if(i == 0) 
        { 
            start = newNode; 
            newNode.prev = start; 
            newNode.next = start; 
        } 
        else
        { 
            // Find the last node 
            temp = (start).prev; 
              
            // Add the last node to make them 
            // in circular fashion 
            temp.next = newNode; 
            newNode.next = start; 
            newNode.prev = temp; 
            temp = start; 
            temp.prev = newNode; 
        } 
    } 
    return start;
} 
  
// Driver Code 
public static void main(String args[])
{ 
    // Array to be converted 
    int arr[] = {1,2,3,4,5}; 
    int n = arr.length; 
      
    // Start Pointer 
    node start = null; 
      
    // Create the List 
    start = createList(arr, n, start); 
      
    // Display the list 
    displayList(start); 
} 
}
  
// This code is contributed by Arnab Kundu


Python3
# Python3 program to convert array to 
# circular doubly linked list
  
# Node of the doubly linked list 
class Node: 
      
    def __init__(self, data): 
        self.data = data 
        self.prev = None
        self.next = None
  
# Utility function to create a node in memory
def getNode():
  
    return (Node(0))
  
# Function to display the list
def displayList(temp):
  
    t = temp
    if(temp == None):
        return 0
    else:
          
        print("The list is: ", end = " ")
          
        while(temp.next != t):
          
            print(temp.data, end = " ")
            temp = temp.next
          
        print(temp.data)
          
        return 1
      
# Function to convert array into list
def createList(arr, n, start):
  
    # Declare newNode and temporary pointer
    newNode = None
    temp = None
    i = 0
      
    # Iterate the loop until array length
    while(i < n):
      
        # Create new node
        newNode = getNode()
          
        # Assign the array data
        newNode.data = arr[i]
          
        # If it is first element
        # Put that node prev and next as start
        # as it is circular
        if(i == 0):
          
            start = newNode
            newNode.prev = start
            newNode.next = start
          
        else:
              
            # Find the last node
            temp = (start).prev
              
            # Add the last node to make them
            # in circular fashion
            temp.next = newNode
            newNode.next = start
            newNode.prev = temp
            temp = start
            temp.prev = newNode
        i = i + 1
    return start
  
# Driver Code
if __name__ == "__main__": 
  
    # Array to be converted
    arr = [1, 2, 3, 4, 5]
    n = len(arr)
      
    # Start Pointer
    start = None
      
    # Create the List
    start = createList(arr, n, start)
      
    # Display the list
    displayList(start)
      
# This code is contributed by Arnab Kundu


C#
// C# program to convert array to 
// circular doubly linked list 
using System;
  
class GFG 
{ 
      
// Doubly linked list node 
public class node 
{ 
    public int data; 
    public node next; 
    public node prev; 
}; 
  
// Utility function to create a node in memory 
static node getNode() 
{ 
    return new node(); 
} 
  
// Function to display the list 
static int displayList( node temp) 
{ 
    node t = temp; 
    if(temp == null) 
        return 0; 
    else
    { 
        Console.Write("The list is: "); 
          
        while(temp.next != t) 
        { 
            Console.Write(temp.data + " "); 
            temp = temp.next; 
        } 
          
        Console.Write(temp.data); 
          
        return 1; 
    } 
} 
  
// Function to convert array into list 
static node createList(int []arr, 
                        int n, node start) 
{ 
    // Declare newNode and temporary pointer 
    node newNode,temp; 
    int i; 
      
    // Iterate the loop until array length 
    for(i = 0; i < n; i++) 
    { 
        // Create new node 
        newNode = getNode(); 
          
        // Assign the array data 
        newNode.data = arr[i]; 
          
        // If it is first element 
        // Put that node prev and next as start 
        // as it is circular 
        if(i == 0) 
        { 
            start = newNode; 
            newNode.prev = start; 
            newNode.next = start; 
        } 
        else
        { 
            // Find the last node 
            temp = (start).prev; 
              
            // Add the last node to make them 
            // in circular fashion 
            temp.next = newNode; 
            newNode.next = start; 
            newNode.prev = temp; 
            temp = start; 
            temp.prev = newNode; 
        } 
    } 
    return start; 
} 
  
// Driver Code 
public static void Main(String []args) 
{ 
    // Array to be converted 
    int []arr = {1,2,3,4,5}; 
    int n = arr.Length; 
      
    // Start Pointer 
    node start = null; 
      
    // Create the List 
    start = createList(arr, n, start); 
      
    // Display the list 
    displayList(start); 
} 
} 
  
// This code contributed by Rajput-Ji


输出:
The list is: 1 2 3 4 5