Skip to content

Latest commit

 

History

History
338 lines (251 loc) · 7.5 KB

ch02-02.md

File metadata and controls

338 lines (251 loc) · 7.5 KB

2.2 Arrays and linked lists

# Array

import array

array1 = array.array('i', [10, 20, 30, 40, 50])  # integer (2-byte) array

# Traverse − print all the array elements one by one
for x in array1:
   print(x)

print('\n')

# Accessing Array Element
print (array1[0])
print (array1[2])

print('\n')

# Insertion − add an element at the given index
array1.insert(1, 60)

for x in array1:
   print(x)

print('\n')

# Deletion − delete an element at the given index
array1.remove(40)

for x in array1:
   print(x)

print('\n')

# Search − search an element using the given index or by the value
print (array1.index(20))  # if there is no searching item exists, error (exception) will occur 

print('\n')

# Update − update an element at the given index
array1[2] = 80

for x in array1:
   print(x)
  • 🐍 python practice
# Linked List

# In fact, you don't need to design a class for linked list.
# 'Python list' is alread designed for linked list.
# However, it is good to know how 'Python list' works as a linked list by writing your own code.

# Creation of Linked list and traverse

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
        
    def list_print(self):
        current_node = self.head
        while current_node is not None:
            print(current_node.data)
            current_node = current_node.next
    
# Todo list test
todo_node0 = Node("BRUNCH")
todo_node1 = Node("BOCCE")
todo_node2 = Node("DRINK TEA")

todo_list = LinkedList()
todo_list.head = todo_node0
todo_node0.next = todo_node1
todo_node1.next = todo_node2

todo_list.list_print()
  • 🐍 python practice
# Linked List
# Insert a node at the beginning

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
        
    def list_print(self):
        current_node = self.head
        while current_node is not None:
            print(current_node.data)
            current_node = current_node.next
    
    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node

# Todo list test
todo_node0 = Node("BRUNCH")
todo_node1 = Node("BOCCE")
todo_node2 = Node("DRINK TEA")

todo_list = LinkedList()
todo_list.head = todo_node0
todo_node0.next = todo_node1
todo_node1.next = todo_node2

todo_list.insert_at_beginning("BUY TEA")

todo_list.list_print()
  • 🐍 python practice
# Linked List
# Inserting a node at the end

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
        
    def list_print(self):
        current_node = self.head
        while current_node is not None:
            print(current_node.data)
            current_node = current_node.next
    
    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node
    
    def insert_at_end(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        last_node = self.head
        while last_node.next is not None:
            last_node = last_node.next
        last_node.next = new_node

# Todo list test
todo_node0 = Node("BRUNCH")
todo_node1 = Node("BOCCE")
todo_node2 = Node("DRINK TEA")

todo_list = LinkedList()
todo_list.head = todo_node0
todo_node0.next = todo_node1
todo_node1.next = todo_node2

todo_list.insert_at_end("BUY TEA")

todo_list.list_print()
  • 🐍 python practice
# Linked List
# Inserting a node in the middle

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
        
    def list_print(self):
        current_node = self.head
        while current_node is not None:
            print(current_node.data)
            current_node = current_node.next
    
    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node
    
    def insert_at_end(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        last_node = self.head
        while last_node.next is not None:
            last_node = last_node.next
        last_node.next = new_node
    
    def insert_in_middle(self, middle_node, data):
        if middle_node is None:
            print("There is no such a middle node!")
            return
        new_node = Node(data)
        new_node.next = middle_node.next
        middle_node.next = new_node

# Todo list test
todo_node0 = Node("BRUNCH")
todo_node1 = Node("BOCCE")
todo_node2 = Node("DRINK TEA")

todo_list = LinkedList()
todo_list.head = todo_node0
todo_node0.next = todo_node1
todo_node1.next = todo_node2

todo_list.insert_in_middle(todo_node1, "BUY TEA")

todo_list.list_print()
  • 🐍 python practice
# Linked List
# Removing a node

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None
        
    def list_print(self):
        current_node = self.head
        while current_node is not None:
            print(current_node.data)
            current_node = current_node.next
    
    def insert_at_beginning(self, data):
        new_node = Node(data)
        new_node.next = self.head
        self.head = new_node
    
    def insert_at_end(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
        last_node = self.head
        while last_node.next is not None:
            last_node = last_node.next
        last_node.next = new_node
    
    def insert_in_middle(self, middle_node, data):
        if middle_node is None:
            print("There is no such a middle node!")
            return
        new_node = Node(data)
        new_node.next = middle_node.next
        middle_node.next = new_node
    
    def remove(self, data):
        previous_node = None
        current_node = self.head
        if current_node is None:
            return
        if current_node.data == data:
            self.head = current_node.next
            current_node = None
            return
        while current_node is not None:
            if current_node.data == data:
                break
            previous_node = current_node
            current_node = current_node.next
        if current_node == None:
            return
        else:
            previous_node.next = current_node.next
            current_node = None
            

# Todo list test
todo_node0 = Node("BRUNCH")
todo_node1 = Node("BOCCE")
todo_node2 = Node("DRINK TEA")

todo_list = LinkedList()
todo_list.head = todo_node0
todo_node0.next = todo_node1
todo_node1.next = todo_node2

todo_list.remove("BOCCE")

todo_list.list_print()