Remove vowels node from linked list in python

Python program for Remove vowels node from linked list. Here more information.

#  Python 3 program for
#  Remove vowels node from in linked list

#  Node of Linked List
class LinkNode :
	def __init__(self, data) :
		#  Set node value
		self.data = data
		self.next = None
	

class SingleLL :
	def __init__(self) :
		self.head = None
		self.tail = None
	
	#  Add new node at the end of linked list
	def insert(self, value) :
		#  Create a new node
		node = LinkNode(value)
		if (self.head == None) :
			self.head = node
		else :
			self.tail.next = node
		
		self.tail = node
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			return
		
		temp = self.head
		#  iterating linked list elements
		while (temp != None) :
			print(temp.data , end = " → ")
			#  Visit to next node
			temp = temp.next
		
		print("null")
	
	#  Check that whether given element is vowel or not
	def isVowel(self, element) :
		if (element == 'a'
			or element == 'e'
			or element == 'i'
			or element == 'o'
			or element == 'u'
			or element == 'A'
			or element == 'E'
			or element == 'I'
			or element == 'O'
			or element == 'U') :
			#  When element is vowel
			return True
		
		return False
	
	#  This are removing volvel data node in linked list
	def deleteVowels(self) :
		if (self.head == None) :
			return
		
		#  Define some useful auxiliary variables 
		temp = self.head
		back = None
		auxiliary = None
		while (temp != None) :
			if (self.isVowel(temp.data) == True) :
				#  Get deleted node
				auxiliary = temp
				if (back == None) :
					#  When delete first node
					self.head = self.head.next
				else :
					#  Modify the connection of 
					#  linked list before removing deleted node
					back.next = temp.next
				
				if (auxiliary == self.tail) :
					#  Last node remove
					self.tail = back
				
				#  Visit to next node
				temp = temp.next
			else :
				#  Get current node
				back = temp
				#  Visit to next node
				temp = temp.next
			
		
	

def main() :
	sll = SingleLL()
	#  Create linked list
	sll.insert('o')
	sll.insert('k')
	sll.insert('h')
	sll.insert('a')
	sll.insert('a')
	sll.insert('p')
	sll.insert('y')
	sll.insert('E')
	sll.insert('n')
	sll.insert('d')
	sll.insert('i')
	sll.insert('n')
	sll.insert('g')
	sll.insert('u')
	print("Before Delete Vowels ")
	sll.display()
	sll.deleteVowels()
	print("After Delete Vowels ")
	sll.display()

if __name__ == "__main__": main()

Output

Before Delete Vowels
o → k → h → a → a → p → y → E → n → d → i → n → g → u → null
After Delete Vowels
k → h → p → y → n → d → n → g → null



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







© 2022, kalkicode.com, All rights reserved