Teaching Kids Programming – Convert Array to Linked List and Vice Versa


Teaching Kids Programming: Videos on Data Structures and Algorithms

Here’s a step-by-step tutorial on how to convert an array to a linked list and vice versa in Python. This can be useful for different scenarios where you need to change data structures based on specific use cases, such as memory optimization, ease of data manipulation, or iteration.

Part 1: Convert an Array to a Linked List

A linked list is a linear data structure where each element (node) contains a reference (link) to the next element. We will implement a simple singly linked list.

Define the Node Class

Each node in the linked list will have two components: the data and the next reference to the next node.

1
2
3
4
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

Define the LinkedList Class

The LinkedList class will manage the nodes and include methods to add nodes and print the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class LinkedList:
    def __init__(self):
        self.head = None
 
    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node
 
    def display(self):
        current = self.head
        while current:
            print(current.data, end="-")
            current = current.next
        print("None")
class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node

    def display(self):
        current = self.head
        while current:
            print(current.data, end="-")
            current = current.next
        print("None")

Convert the Array to a Linked List

We will create a function that takes an array as input and adds each element to the linked list.

1
2
3
4
5
def array_to_linked_list(arr):
    linked_list = LinkedList()
    for item in arr:
        linked_list.append(item)
    return linked_list
def array_to_linked_list(arr):
    linked_list = LinkedList()
    for item in arr:
        linked_list.append(item)
    return linked_list

Test the Conversion

Now, we can test our code by converting an array to a linked list and displaying it.

1
2
3
arr = [1, 2, 3, 4, 5]
linked_list = array_to_linked_list(arr)
linked_list.display()
arr = [1, 2, 3, 4, 5]
linked_list = array_to_linked_list(arr)
linked_list.display()

Output:
1-2-3-4-5-None

Part 2: Convert a Linked List to an Array

We will now create a function that traverses a linked list and converts it back to an array.

Create the Conversion Function

This function will iterate over each node in the linked list, collect the data values, and append them to an array.

1
2
3
4
5
6
7
def linked_list_to_array(linked_list):
    arr = []
    current = linked_list.head
    while current:
        arr.append(current.data)
        current = current.next
    return arr
def linked_list_to_array(linked_list):
    arr = []
    current = linked_list.head
    while current:
        arr.append(current.data)
        current = current.next
    return arr

Test the Conversion

Let’s test converting our linked list back into an array.

1
2
arr_converted_back = linked_list_to_array(linked_list)
print(arr_converted_back)
arr_converted_back = linked_list_to_array(linked_list)
print(arr_converted_back)

Output:
[1, 2, 3, 4, 5]

Full Example: Convert Array to Linked List and Vice Versa

Here’s the complete code for both conversions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
class LinkedList:
    def __init__(self):
        self.head = None
 
    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node
 
    def display(self):
        current = self.head
        while current:
            print(current.data, end=" -> ")
            current = current.next
        print("None")
 
def array_to_linked_list(arr):
    linked_list = LinkedList()
    for item in arr:
        linked_list.append(item)
    return linked_list
 
def linked_list_to_array(linked_list):
    arr = []
    current = linked_list.head
    while current:
        arr.append(current.data)
        current = current.next
    return arr
 
# Testing
arr = [1, 2, 3, 4, 5]
linked_list = array_to_linked_list(arr)
linked_list.display()
 
arr_converted_back = linked_list_to_array(linked_list)
print(arr_converted_back)
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node

    def display(self):
        current = self.head
        while current:
            print(current.data, end=" -> ")
            current = current.next
        print("None")

def array_to_linked_list(arr):
    linked_list = LinkedList()
    for item in arr:
        linked_list.append(item)
    return linked_list

def linked_list_to_array(linked_list):
    arr = []
    current = linked_list.head
    while current:
        arr.append(current.data)
        current = current.next
    return arr

# Testing
arr = [1, 2, 3, 4, 5]
linked_list = array_to_linked_list(arr)
linked_list.display()

arr_converted_back = linked_list_to_array(linked_list)
print(arr_converted_back)

This tutorial demonstrates how to easily convert an array to a linked list and vice versa. This technique can help you switch between data structures based on your specific needs.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
767 words
Last Post: Simply Explained: Matthew Effect
Next Post: Tetration Operator in Math Simply Explained with Python Algorithms

The Permanent URL is: Teaching Kids Programming – Convert Array to Linked List and Vice Versa

Leave a Reply