Skip to main content

Convert linked list to array

Here given code implementation process.

// C Program 
// Convert linked list to array
#include <stdio.h>
#include <stdlib.h>

//Linked List Node
struct ListNode
{
	int data;
	struct ListNode *next;
};

// Define custom linked list
struct MyLinkedList
{
	struct ListNode *head;
	struct ListNode *tail;
	int size;
};

// Returns new linked list
struct MyLinkedList *newLinkedList()
{
	struct MyLinkedList *linked_list = (struct MyLinkedList *) malloc(sizeof(struct MyLinkedList));
	if (linked_list != NULL)
	{
		linked_list->head = NULL;
		linked_list->tail = NULL;
		linked_list->size = 0;
	}
	else
	{
		printf("\n New Linked List not created (Memory Overflow) \n");
	}
	return linked_list;
}

// Returns new node of linked list
struct ListNode *newNode(int data)
{
	struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode));
	if (node != NULL)
	{
		node->data = data;
		node->next = NULL;
	}
	else
	{
		printf("\n New Node not created (Memory Overflow) \n");
	}
	return node;
}

// Add new node of linked list
void addNode(struct MyLinkedList *lists, int data)
{
	if (lists->head == NULL)
	{
		// First node of linked list
		lists->head = newNode(data);
		lists->tail = lists->head;
		lists->size++;
	}
	else
	{
		// Add node at last position
		lists->tail->next = newNode(data);
		lists->tail = lists->tail->next;
		lists->size++;
	}
}

// Display linked list elements
void printLinkedList(struct MyLinkedList *lists)
{
	struct ListNode *temp = lists->head;
	while (temp != NULL)
	{
		// Display value of node 
		printf("  %d", temp->data);
		// Visit to next node
		temp = temp->next;
	}
	printf("\n");
}

//Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}

// Fill the linked list node value to array
void setElement(struct MyLinkedList *lists, int arr[])
{
	int i = 0;
	// Get first node of linked list
	struct ListNode *temp = lists->head;
	// Assign linked list element into array
	while (temp != NULL)
	{
		arr[i] = temp->data;
		// visit to next node
		temp = temp->next;
		// next index
		i++;
	}
}
int main()
{
	// Create a linked list
	struct MyLinkedList *lists = newLinkedList();
	// Add element in linked list 
	addNode(lists, 1);
	addNode(lists, 2);
	addNode(lists, 3);
	addNode(lists, 4);
	addNode(lists, 5);
	addNode(lists, 6);
	// Display linked list element
	printf(" Linked List \n");
	printLinkedList(lists);
	if (lists->size > 0)
	{
        int size = lists->size;
		// Create array
		int arr[size];
		// Assign the value of linked list element into array
		setElement(lists, arr);
		// Display array elements
		printf(" Array Element \n");
		printArray(arr,size);
	}
	else
	{
		printf("\n Empty Linked List \n");
	}
	return 0;
}

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6
/*
    Java Program
    Convert linked list to array
*/
//Linked List Node
class ListNode
{
	public int data;
	public ListNode next;
	public ListNode(int data)
	{
		this.next = null;
		this.data = data;
	}
}
// Define custom linked list
class MyLinkedList
{
	public ListNode head;
	public ListNode tail;
	public int size;
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
	// Add new node of linked list
	public void addNode(int data)
	{
		if (this.head == null)
		{
			// First node of linked list
			this.head = new ListNode(data);
			this.tail = this.head;
			this.size++;
		}
		else
		{
			// Add node at last position
			this.tail.next = new ListNode(data);
			this.tail = this.tail.next;
			this.size++;
		}
	}
	// Display linked list elements
	public void printLinkedList()
	{
		ListNode temp = this.head;
		while (temp != null)
		{
			// Display value of node 
			System.out.print("  " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		System.out.print("\n");
	}
}
public class Transform
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Fill the linked list node value to array
	public void setElement(ListNode head, int[] arr)
	{
		int i = 0;
		// Get first node of linked list
		ListNode temp = head;
		// Assign linked list element into array
		while (temp != null)
		{
			arr[i] = temp.data;
			// visit to next node
			temp = temp.next;
			// next index
			i++;
		}
	}
	public static void main(String[] args)
	{
		Transform task = new Transform();
		MyLinkedList lists = new MyLinkedList();
		// Add element in linked list 
		lists.addNode(1);
		lists.addNode(2);
		lists.addNode(3);
		lists.addNode(4);
		lists.addNode(5);
		lists.addNode(6);
		// Display linked list element
		System.out.print(" Linked List \n");
		lists.printLinkedList();
		if (lists.size > 0)
		{
          	int size = lists.size;
			// Create array
			int[] arr = new int[size];
			// Assign the value of linked list element into array
			task.setElement(lists.head, arr);
			// Display array elements
			System.out.print(" Array Element \n");
			task.printArray(arr,size);
		}
		else
		{
			System.out.print("\n Empty Linked List \n");
		}
	}
}

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Convert linked list to array
*/
//Linked List Node
class ListNode
{
	public: 
    int data;
	ListNode *next;
	ListNode(int data)
	{
		this->next = NULL;
		this->data = data;
	}
};
// Define custom linked list
class MyLinkedList
{
	public: 
    ListNode *head;
	ListNode *tail;
	int size;
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
		this->size = 0;
	}
	// Add new node of linked list
	void addNode(int data)
	{
		if (this->head == NULL)
		{
			// First node of linked list
			this->head = new ListNode(data);
			this->tail = this->head;
			this->size++;
		}
		else
		{
			// Add node at last position
			this->tail->next = new ListNode(data);
			this->tail = this->tail->next;
			this->size++;
		}
	}
	// Display linked list elements
	void printLinkedList()
	{
		ListNode *temp = this->head;
		while (temp != NULL)
		{
			// Display value of node
			cout << "  " << temp->data;
			// Visit to next node
			temp = temp->next;
		}
		cout << "\n";
	}
};
class Transform
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// Fill the linked list node value to array
	void setElement(ListNode *head, int arr[])
	{
		int i = 0;
		// Get first node of linked list
		ListNode *temp = head;
		// Assign linked list element into array
		while (temp != NULL)
		{
			arr[i] = temp->data;
			// visit to next node
			temp = temp->next;
			// next index
			i++;
		}
	}
};
int main()
{
	Transform task     = Transform();
	MyLinkedList lists = MyLinkedList();
	// Add element in linked list
	lists.addNode(1);
	lists.addNode(2);
	lists.addNode(3);
	lists.addNode(4);
	lists.addNode(5);
	lists.addNode(6);
	// Display linked list element
	cout << " Linked List \n";
	lists.printLinkedList();
	if (lists.size > 0)
	{
		int size = lists.size;
		// Create array
		int arr[size];
		// Assign the value of linked list element into array
		task.setElement(lists.head, arr);
		// Display array elements
		cout << " Array Element \n";
		task.printArray(arr, size);
	}
	else
	{
		cout << "\n Empty Linked List \n";
	}
	return 0;
}

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6
// Include namespace system
using System;
/*
    C# Program
    Convert linked list to array
*/
//Linked List Node
public class ListNode
{
	public int data;
	public ListNode next;
	public ListNode(int data)
	{
		this.next = null;
		this.data = data;
	}
}
// Define custom linked list
public class MyLinkedList
{
	public ListNode head;
	public ListNode tail;
	public int size;
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
	// Add new node of linked list
	public void addNode(int data)
	{
		if (this.head == null)
		{
			// First node of linked list
			this.head = new ListNode(data);
			this.tail = this.head;
			this.size++;
		}
		else
		{
			// Add node at last position
			this.tail.next = new ListNode(data);
			this.tail = this.tail.next;
			this.size++;
		}
	}
	// Display linked list elements
	public void printLinkedList()
	{
		ListNode temp = this.head;
		while (temp != null)
		{
			// Display value of node
			Console.Write("  " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		Console.Write("\n");
	}
}
public class Transform
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Fill the linked list node value to array
	public void setElement(ListNode head, int[] arr)
	{
		int i = 0;
		// Get first node of linked list
		ListNode temp = head;
		// Assign linked list element into array
		while (temp != null)
		{
			arr[i] = temp.data;
			// visit to next node
			temp = temp.next;
			// next index
			i++;
		}
	}
	public static void Main(String[] args)
	{
		Transform task = new Transform();
		MyLinkedList lists = new MyLinkedList();
		// Add element in linked list
		lists.addNode(1);
		lists.addNode(2);
		lists.addNode(3);
		lists.addNode(4);
		lists.addNode(5);
		lists.addNode(6);
		// Display linked list element
		Console.Write(" Linked List \n");
		lists.printLinkedList();
		if (lists.size > 0)
		{
			int size = lists.size;
			// Create array
			int[] arr = new int[size];
			// Assign the value of linked list element into array
			task.setElement(lists.head, arr);
			// Display array elements
			Console.Write(" Array Element \n");
			task.printArray(arr, size);
		}
		else
		{
			Console.Write("\n Empty Linked List \n");
		}
	}
}

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6
<?php
/*
    Php Program
    Convert linked list to array
*/
//Linked List Node
class ListNode
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->next = null;
		$this->data = $data;
	}
}
// Define custom linked list
class MyLinkedList
{
	public $head;
	public $tail;
	public $size;

	function __construct()
	{
		$this->head = null;
		$this->tail = null;
		$this->size = 0;
	}
	// Add new node of linked list
	public	function addNode($data)
	{
		if ($this->head == null)
		{
			// First node of linked list
			$this->head = new ListNode($data);
			$this->tail = $this->head;
			$this->size++;
		}
		else
		{
			// Add node at last position
			$this->tail->next = new ListNode($data);
			$this->tail = $this->tail->next;
			$this->size++;
		}
	}
	// Display linked list elements
	public	function printLinkedList()
	{
		$temp = $this->head;
		while ($temp != null)
		{
			// Display value of node
			echo "  ". $temp->data;
			// Visit to next node
			$temp = $temp->next;
		}
		echo "\n";
	}
}
class Transform
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Fill the linked list node value to array
	public	function setElement($head, & $arr)
	{
		$i = 0;
		// Get first node of linked list
		$temp = $head;
		// Assign linked list element into array
		while ($temp != null)
		{
			$arr[$i] = $temp->data;
			// visit to next node
			$temp = $temp->next;
			// next index
			$i++;
		}
	}
}

function main()
{
	$task = new Transform();
	$lists = new MyLinkedList();
	// Add element in linked list
	$lists->addNode(1);
	$lists->addNode(2);
	$lists->addNode(3);
	$lists->addNode(4);
	$lists->addNode(5);
	$lists->addNode(6);
	// Display linked list element
	echo " Linked List \n";
	$lists->printLinkedList();
	if ($lists->size > 0)
	{
		$size = $lists->size;
		// Create array
		$arr = array_fill(0, $size, 0);
		// Assign the value of linked list element into array
		$task->setElement($lists->head, $arr);
		// Display array elements
		echo " Array Element \n";
		$task->printArray($arr, $size);
	}
	else
	{
		echo "\n Empty Linked List \n";
	}
}
main();

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6
/*
    Node Js Program
    Convert linked list to array
*/
//Linked List Node
class ListNode
{
	constructor(data)
	{
		this.next = null;
		this.data = data;
	}
}
// Define custom linked list
class MyLinkedList
{
	constructor()
	{
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
	// Add new node of linked list
	addNode(data)
	{
		if (this.head == null)
		{
			// First node of linked list
			this.head = new ListNode(data);
			this.tail = this.head;
			this.size++;
		}
		else
		{
			// Add node at last position
			this.tail.next = new ListNode(data);
			this.tail = this.tail.next;
			this.size++;
		}
	}
	// Display linked list elements
	printLinkedList()
	{
		var temp = this.head;
		while (temp != null)
		{
			// Display value of node
			process.stdout.write("  " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write("\n");
	}
}
class Transform
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Fill the linked list node value to array
	setElement(head, arr)
	{
		var i = 0;
		// Get first node of linked list
		var temp = head;
		// Assign linked list element into array
		while (temp != null)
		{
			arr[i] = temp.data;
			// visit to next node
			temp = temp.next;
			// next index
			i++;
		}
	}
}

function main()
{
	var task = new Transform();
	var lists = new MyLinkedList();
	// Add element in linked list
	lists.addNode(1);
	lists.addNode(2);
	lists.addNode(3);
	lists.addNode(4);
	lists.addNode(5);
	lists.addNode(6);
	// Display linked list element
	process.stdout.write(" Linked List \n");
	lists.printLinkedList();
	if (lists.size > 0)
	{
		var size = lists.size;
		// Create array
		var arr = Array(size).fill(0);
		// Assign the value of linked list element into array
		task.setElement(lists.head, arr);
		// Display array elements
		process.stdout.write(" Array Element \n");
		task.printArray(arr, size);
	}
	else
	{
		process.stdout.write("\n Empty Linked List \n");
	}
}
main();

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6
#  Python 3 Program
#  Convert linked list to array

# Linked List Node
class ListNode :
	
	def __init__(self, data) :
		self.next = None
		self.data = data
	

#  Define custom linked list
class MyLinkedList :
	
	def __init__(self) :
		self.head = None
		self.tail = None
		self.size = 0
	
	#  Add new node of linked list
	def addNode(self, data) :
		if (self.head == None) :
			#  First node of linked list
			self.head = ListNode(data)
			self.tail = self.head
			self.size += 1
		else :
			#  Add node at last position
			self.tail.next = ListNode(data)
			self.tail = self.tail.next
			self.size += 1
		
	
	#  Display linked list elements
	def printLinkedList(self) :
		temp = self.head
		while (temp != None) :
			#  Display value of node
			print("  ", temp.data, end = "")
			#  Visit to next node
			temp = temp.next
		
		print(end = "\n")
	

class Transform :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Fill the linked list node value to array
	def setElement(self, head, arr) :
		i = 0
		#  Get first node of linked list
		temp = head
		#  Assign linked list element into array
		while (temp != None) :
			arr[i] = temp.data
			#  visit to next node
			temp = temp.next
			#  next index
			i += 1
		
	

def main() :
	task = Transform()
	lists = MyLinkedList()
	#  Add element in linked list
	lists.addNode(1)
	lists.addNode(2)
	lists.addNode(3)
	lists.addNode(4)
	lists.addNode(5)
	lists.addNode(6)
	#  Display linked list element
	print(" Linked List ")
	lists.printLinkedList()
	if (lists.size > 0) :
		size = lists.size
		#  Create array
		arr = [0] * (size)
		#  Assign the value of linked list element into array
		task.setElement(lists.head, arr)
		#  Display array elements
		print(" Array Element ")
		task.printArray(arr, size)
	else :
		print("\n Empty Linked List ")
	

if __name__ == "__main__": main()

Output

 Linked List
   1   2   3   4   5   6
 Array Element
   1   2   3   4   5   6
#  Ruby Program
#  Convert linked list to array

# Linked List Node
class ListNode  
	# Define the accessor and reader of class ListNode  
	attr_reader :data, :next
	attr_accessor :data, :next
 
	
	def initialize(data) 
		self.next = nil
		self.data = data
	end

end

#  Define custom linked list
class MyLinkedList  
	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail, :size
	attr_accessor :head, :tail, :size
 
	
	def initialize() 
		self.head = nil
		self.tail = nil
		self.size = 0
	end

	#  Add new node of linked list
	def addNode(data) 
		if (self.head == nil) 
			#  First node of linked list
			self.head = ListNode.new(data)
			self.tail = self.head
			self.size += 1
		else 
			#  Add node at last position
			self.tail.next = ListNode.new(data)
			self.tail = self.tail.next
			self.size += 1
		end

	end

	#  Display linked list elements
	def printLinkedList() 
		temp = self.head
		while (temp != nil) 
			#  Display value of node
			print("  ", temp.data)
			#  Visit to next node
			temp = temp.next
		end

		print("\n")
	end

end

class Transform 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Fill the linked list node value to array
	def setElement(head, arr) 
		i = 0
		#  Get first node of linked list
		temp = head
		#  Assign linked list element into array
		while (temp != nil) 
			arr[i] = temp.data
			#  visit to next node
			temp = temp.next
			#  next index
			i += 1
		end

	end

end

def main() 
	task = Transform.new()
	lists = MyLinkedList.new()
	#  Add element in linked list
	lists.addNode(1)
	lists.addNode(2)
	lists.addNode(3)
	lists.addNode(4)
	lists.addNode(5)
	lists.addNode(6)
	#  Display linked list element
	print(" Linked List \n")
	lists.printLinkedList()
	if (lists.size > 0) 
		size = lists.size
		#  Create array
		arr = Array.new(size) {0}
		#  Assign the value of linked list element into array
		task.setElement(lists.head, arr)
		#  Display array elements
		print(" Array Element \n")
		task.printArray(arr, size)
	else 
		print("\n Empty Linked List \n")
	end

end

main()

Output

 Linked List 
  1  2  3  4  5  6
 Array Element 
  1  2  3  4  5  6
/*
    Scala Program
    Convert linked list to array
*/
//Linked List Node
class ListNode(var data: Int , var next: ListNode)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
// Define custom linked list
class MyLinkedList(var head: ListNode , var tail: ListNode , var size: Int)
{
	def this()
	{
		this(null, null, 0);
	}
	// Add new node of linked list
	def addNode(data: Int): Unit = {
		if (this.head == null)
		{
			// First node of linked list
			this.head = new ListNode(data);
			this.tail = this.head;
			this.size += 1;
		}
		else
		{
			// Add node at last position
			this.tail.next = new ListNode(data);
			this.tail = this.tail.next;
			this.size += 1;
		}
	}
	// Display linked list elements
	def printLinkedList(): Unit = {
		var temp: ListNode = this.head;
		while (temp != null)
		{
			// Display value of node
			print("  " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print("\n");
	}
}
class Transform
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Fill the linked list node value to array
	def setElement(head: ListNode, arr: Array[Int]): Unit = {
		var i: Int = 0;
		// Get first node of linked list
		var temp: ListNode = head;
		// Assign linked list element into array
		while (temp != null)
		{
			arr(i) = temp.data;
			// visit to next node
			temp = temp.next;
			// next index
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Transform = new Transform();
		var lists: MyLinkedList = new MyLinkedList();
		// Add element in linked list
		lists.addNode(1);
		lists.addNode(2);
		lists.addNode(3);
		lists.addNode(4);
		lists.addNode(5);
		lists.addNode(6);
		// Display linked list element
		print(" Linked List \n");
		lists.printLinkedList();
		if (lists.size > 0)
		{
			var size: Int = lists.size;
			// Create array
			var arr: Array[Int] = Array.fill[Int](size)(0);
			// Assign the value of linked list element into array
			task.setElement(lists.head, arr);
			// Display array elements
			print(" Array Element \n");
			task.printArray(arr, size);
		}
		else
		{
			print("\n Empty Linked List \n");
		}
	}
}

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6
/*
    Swift 4 Program
    Convert linked list to array
*/
//Linked List Node
class ListNode
{
	var data: Int;
	var next: ListNode? ;
	init(_ data: Int)
	{
		self.next = nil;
		self.data = data;
	}
}
// Define custom linked list
class MyLinkedList
{
	var head: ListNode? ;
	var tail: ListNode? ;
	var size: Int;
	init()
	{
		self.head = nil;
		self.tail = nil;
		self.size = 0;
	}
	// Add new node of linked list
	func addNode(_ data: Int)
	{
		if (self.head == nil)
		{
			// First node of linked list
			self.head = ListNode(data);
			self.tail = self.head;
			self.size += 1;
		}
		else
		{
			// Add node at last position
			self.tail!.next = ListNode(data);
			self.tail = self.tail!.next;
			self.size += 1;
		}
	}
	// Display linked list elements
	func printLinkedList()
	{
		var temp: ListNode? = self.head;
		while (temp  != nil)
		{
			// Display value of node
			print("  ", temp!.data, terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(terminator: "\n");
	}
}
class Transform
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Fill the linked list node value to array
	func setElement(_ head: ListNode? , _ arr : inout[Int])
	{
		var i: Int = 0;
		// Get first node of linked list
		var temp: ListNode? = head;
		// Assign linked list element into array
		while (temp  != nil)
		{
			arr[i] = temp!.data;
			// visit to next node
			temp = temp!.next;
			// next index
			i += 1;
		}
	}
}
func main()
{
	let task: Transform = Transform();
	let lists: MyLinkedList = MyLinkedList();
	// Add element in linked list
	lists.addNode(1);
	lists.addNode(2);
	lists.addNode(3);
	lists.addNode(4);
	lists.addNode(5);
	lists.addNode(6);
	// Display linked list element
	print(" Linked List ");
	lists.printLinkedList();
	if (lists.size > 0)
	{
		let size: Int = lists.size;
		// Create array
		var arr: [Int] = Array(repeating: 0, count: size);
		// Assign the value of linked list element into array
		task.setElement(lists.head, &arr);
		// Display array elements
		print(" Array Element ");
		task.printArray(arr, size);
	}
	else
	{
		print("\n Empty Linked List ");
	}
}
main();

Output

 Linked List
   1   2   3   4   5   6
 Array Element
   1   2   3   4   5   6
/*
    Kotlin Program
    Convert linked list to array
*/
//Linked List Node
class ListNode
{
	var data: Int;
	var next: ListNode? ;
	constructor(data: Int)
	{
		this.next = null;
		this.data = data;
	}
}
// Define custom linked list
class MyLinkedList
{
	var head: ListNode ? ;
	var tail: ListNode ? ;
	var size: Int;
	constructor()
	{
		this.head = null;
		this.tail = null;
		this.size = 0;
	}
	// Add new node of linked list
	fun addNode(data: Int): Unit
	{
		if (this.head == null)
		{
			// First node of linked list
			this.head = ListNode(data);
			this.tail = this.head;
			this.size += 1;
		}
		else
		{
			// Add node at last position
			this.tail?.next = ListNode(data);
			this.tail = this.tail?.next;
			this.size += 1;
		}
	}
	// Display linked list elements
	fun printLinkedList(): Unit
	{
		var temp: ListNode ? = this.head;
		while (temp != null)
		{
			// Display value of node
			print("  " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print("\n");
	}
}
class Transform
{
	//Display elements of given array
	fun printArray(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Fill the linked list node value to array
	fun setElement(head: ListNode? , arr : Array<Int>): Unit
	{
		var i: Int = 0;
		// Get first node of linked list
		var temp: ListNode? = head;
		// Assign linked list element into array
		while (temp != null)
		{
			arr[i] = temp.data;
			// visit to next node
			temp = temp.next;
			// next index
			i += 1;
		}
	}
}
fun main(args: Array<String>): Unit
{
	var task: Transform = Transform();
	var lists: MyLinkedList = MyLinkedList();
	// Add element in linked list
	lists.addNode(1);
	lists.addNode(2);
	lists.addNode(3);
	lists.addNode(4);
	lists.addNode(5);
	lists.addNode(6);
	// Display linked list element
	print(" Linked List \n");
	lists.printLinkedList();
	if (lists.size > 0)
	{
		var size: Int = lists.size;
		// Create array
		var arr: Array<Int> = Array(size){0};
		// Assign the value of linked list element into array
		task.setElement(lists.head, arr);
		// Display array elements
		print(" Array Element \n");
		task.printArray(arr, size);
	}
	else
	{
		print("\n Empty Linked List \n");
	}
}

Output

 Linked List
  1  2  3  4  5  6
 Array Element
  1  2  3  4  5  6




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment