I am newbie in programming and starting out in Python. My question is regarding linked lists, I wrote a class for the linked list, what I need to do is to have a function with an input as a reference pointing towards the head of the list. 'linked_list.head' as I understand, with linked_list being the name of the list in question. Specifically using recursion, I am trying to find the length of the list as the output of this function. Here's my code, I don't quite understand how I could move to the next node and return the number of nodes with recursion in this case.
import re
def special_match(strg, search=re.compile(r'[^A-Za-z.]').search):
return not bool(search(strg))
class node:
def __init__(self, data, next):
self.data = data
self.next = next
def get_data(self):
return self.data
def set_data(self,value):
self.data = value
def get_next_node(self):
return self.next
def set_next_node(self,val):
self.next = val
class linked_list:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def add_first(self,e):
newest = node(e,None)
newest.next = self.head
self.head = newest
self.size = self.size+1
if self.size == 1:
self.tail = newest
def add_last(self,e):
newest = node(e,None)
if self.size > 0:
self.tail.next = newest
else:
self.head = newest
self.tail = newest
self.size = self.size+1
def remove_first(self):
if self.size == 0:
print('The linked list is empty')
elif self.size == 1:
answer = self.head.data
self.head = None
self.tail = None
self.size -= 1
return answer
else:
answer = self.head.data
self.head = self.head.next
self.size = self.size - 1
return answer
def remove_last(self):
if self.size == 0:
print('The linked list is empty')
elif self.size == 1:
answer = self.tail.data
self.head = None
self.tail = None
self.size -= 1
return answer
else:
temp = self.head
while(temp.next is not None):
temp = temp.next
temp.next = None
def node_number(self,reference):
reference = str(reference)
count = 0
temp = self.head
if special_match(reference) == True:
count =+ 1
temp = temp.next
return self.node_number
else:
print('You have made wrong input')
def printe(self):
curr = self.head
while curr:
print(curr.data)
curr = curr.get_next_node()
if self.size == 0:
print('The list is empty')