0% found this document useful (0 votes)
14 views3 pages

Module 4 CCD

Uploaded by

Saalif Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Module 4 CCD

Uploaded by

Saalif Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.Python Program to Check if Singly self.

first = new_node
Linked List is Palindrome else:
class Node: new_node.prev = self.last
def __init__(self, data): self.last.next = new_node
self.data = data self.last = new_node
self.next = None def find_largest(dllist):
class LinkedList: if dllist.first is None:
def __init__(self): return None
self.head = None largest = dllist.first.data
self.last_node = None current = dllist.first.next
def append(self, data): while current:
if self.last_node is None: if current.data > largest:
self.head = Node(data) largest = current.data
self.last_node = self.head current = current.next
else: return largest
self.last_node.next = Node(data) a_dllist = DoublyLinkedList()
self.last_node = self.last_node.next data_list = input('Please enter the elements
def get_prev_node(self, ref_node): in the doubly linked list: ').split()
current = self.head for data in data_list:
while (current and current.next != a_dllist.append(int(data))
ref_node): largest = find_largest(a_dllist)
current = current.next if largest:
return current print('The largest element is
def is_palindrome(llist): {}.'.format(largest))
start = llist.head else:
end = llist.last_node print('The list is empty.')
while (start != end and end.next != start): 3.write a python program to count the
if start.data != end.data: number of items of a given doubly linked
return False list
start = start.next class Node(object):
end = llist.get_prev_node(end) def __init__(self, data=None, next=None,
return True prev=None):
a_llist = LinkedList() self.data = data
data_list = input('Please enter the elements self.next = next
in the linked list: ').split() self.prev = prev
for data in data_list: class doubly_linked_list(object):
a_llist.append(int(data)) def __init__(self):
if is_palindrome(a_llist): self.head = None
print('The linked list is palindromic.') self.tail = None
else: self.count = 0
print('The linked list is not palindromic.') def append_item(self, data):
2)Python Program to Find the Largest # Append an item
Element in a Doubly Linked List new_item = Node(data, None, None)
class Node: if self.head is None:
def __init__(self, data): self.head = new_item
self.data = data self.tail = self.head
self.next = None else:
self.prev = None new_item.prev = self.tail
class DoublyLinkedList: self.tail.next = new_item
def __init__(self): self.tail = new_item
self.first = None self.count += 1
self.last = None items = doubly_linked_list()
def append(self, data): items.append_item('PHP')
self.insert_at_end(Node(data)) items.append_item('Python')
def insert_at_end(self, new_node): items.append_item('C#')
if self.last is None: items.append_item('C++')
self.last = new_node items.append_item('Java')
items.append_item('SQL') items.reverse()
print("Number of items of the Doubly linked items.print_foward()
list:",items.count) 5.Python Program to Find the First
4. Write a Python program to print a Common Element in Two Linked Lists
given doubly linked list in reverse order class Node:
class Node(object): def __init__(self, data):
def __init__(self, data=None, next=None, self.data = data
prev=None): self.next = None
self.data = data class LinkedList:
self.next = next def __init__(self):
self.prev = prev self.head = None
self.last_node = None
class doubly_linked_list(object): def append(self, data):
def __init__(self): if self.last_node is None:
self.head = None self.head = Node(data)
self.tail = None self.last_node = self.head
self.count = 0 else:
def append_item(self, data): self.last_node.next = Node(data)
# Append an item self.last_node = self.last_node.next
new_item = Node(data, None, None) def first_common(llist1, llist2):
if self.head is None: current1 = llist1.head
self.head = new_item while current1:
self.tail = self.head data = current1.data
else: current2 = llist2.head
new_item.prev = self.tail while current2:
self.tail.next = new_item if data == current2.data:
self.tail = new_item return data
self.count += 1 current2 = current2.next
def iter(self): current1 = current1.next
# Iterate the list return None
current = self.head llist1 = LinkedList()
while current: llist2 = LinkedList()
item_val = current.data data_list = input('Please enter the elements
current = current.next in the first linked list: ').split()
yield item_val for data in data_list:
def print_foward(self): llist1.append(int(data))
for node in self.iter(): data_list = input('Please enter the elements
print(node) in the second linked list: ').split()
def reverse(self): for data in data_list:
""" Reverse linked list. """ llist2.append(int(data))
current = self.head common = first_common(llist1, llist2)
while current: if common:
temp = current.next print('The element that appears first in the
current.next = current.prev first linked list that'
current.prev = temp ' is common to both is
current = current.prev {}.'.format(common))
temp = self.head else:
self.head = self.tail print('The two lists have no common
self.tail = temp elements.')
items = doubly_linked_list()
items.append_item('PHP')
items.append_item('Python')
items.append_item('C#')
items.append_item('C++')
items.append_item('Java')
items.append_item('SQL')
print("Reverse list ")
6.Python Program to Segregate Even and current = temp
Odd Nodes in a Linked List a_llist = LinkedList()
class Node: data_list = input('Please enter the elements
def __init__(self, data): in the linked list: ').split()
self.data = data for data in data_list:
self.next = None a_llist.append(int(data))
class LinkedList: move_even_before_odd(a_llist)
def __init__(self): print('The new list: ')
self.head = None a_llist.display()
self.last_node = None 7. Python Program to Check if Two Lists
def append(self, data): are Equal
if self.last_node is None: class Node:
self.head = Node(data) def __init__(self, data):
self.last_node = self.head self.data = data
else: self.next = None
self.last_node.next = Node(data) class LinkedList:
self.last_node = self.last_node.next def __init__(self):
def display(self): self.head = None
current = self.head self.last_node = None
while current: def append(self, data):
print(current.data, end = ' ') if self.last_node is None:
current = current.next self.head = Node(data)
def get_node(self, index): self.last_node = self.head
current = self.head else:
for i in range(index): self.last_node.next = Node(data)
if current is None: self.last_node = self.last_node.next
return None def is_equal(llist1, llist2):
current = current.next current1 = llist1.head
return current current2 = llist2.head
def get_prev_node(self, ref_node): while (current1 and current2):
current = self.head if current1.data != current2.data:
while (current and current.next != return False
ref_node): current1 = current1.next
current = current.next current2 = current2.next
return current if current1 is None and current2 is None:
def insert_at_beg(self, new_node): return True
if self.head is None: else:
self.head = new_node return False
else: llist1 = LinkedList()
new_node.next = self.head llist2 = LinkedList()
self.head = new_node data_list = input('Please enter the elements
def remove(self, node): in the first linked list: ').split()
prev_node = self.get_prev_node(node) for data in data_list:
if prev_node is None: llist1.append(int(data))
self.head = self.head.next data_list = input('Please enter the elements
else: in the second linked list: ').split()
prev_node.next = node.next for data in data_list:
def move_even_before_odd(llist): llist2.append(int(data))
current = llist.head if is_equal(llist1, llist2):
while current: print('The two linked lists are the same.')
temp = current.next else:
if current.data % 2 == 0: print('The two linked list are not the
llist.remove(current) same.', end = '')
llist.insert_at_beg(current)

You might also like