0% found this document useful (0 votes)
2 views9 pages

Assignment 2 LL

The document contains a series of Python classes and methods for implementing linked lists and doubly linked lists, including functionalities for appending nodes, printing lists, merging two lists, separating even and odd elements, reversing a doubly linked list, and deleting negative values. Each section provides code examples demonstrating the implementation of these data structures and their respective operations. The document serves as an assignment for a Data Structure and Algorithms course.

Uploaded by

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

Assignment 2 LL

The document contains a series of Python classes and methods for implementing linked lists and doubly linked lists, including functionalities for appending nodes, printing lists, merging two lists, separating even and odd elements, reversing a doubly linked list, and deleting negative values. Each section provides code examples demonstrating the implementation of these data structures and their respective operations. The document serves as an assignment for a Data Structure and Algorithms course.

Uploaded by

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

Kanishk deshpande

Div – A

Roll No. – 2401042

Data Structure and Algorithms

Assignment 2

Q. 1 :

class Node:

def __init__(self, data):

self.data = data

self.next = None

class LinkedList:

def __init__(self):

self.head = None

def append(self, new_node):

temp = self.head

if self.head is None:

self.head = new_node

else:

while temp.next is not None:

temp = temp.next

temp.next = new_node

def printList(self):

print("linked list")

temp = self.head

while temp is not None:

print(temp.data,end=" ")
temp = temp.next

print()

#merging two list

def merge(self,l2):

temp=self.head

while temp.next is not None:

temp=temp.next

temp.next=l2.head

n1=Node(10)

n2=Node(20)

n3=Node(30)

n4=Node(40)

n5=Node(50)

n6=Node(60)

n7=Node(70)

#creation and insertion in list 1

ll1=LinkedList()

ll1.append(n1)

ll1.append(n2)

ll1.append(n3)

ll1.append(n4)

ll1.printList()

#creation and insertion in list 2

ll2=LinkedList()

ll2.append(n5)

ll2.append(n6)

ll2.append(n7)

ll2.printList()
#merging two lists

ll1.merge(ll2)

ll1.printList()

Q. 2 :

class Node:

def __init__(self, data):

self.data = data

self.next = None

class LinkedList:

def __init__(self):

self.head = None

def append(self, new_node):

temp = self.head

if self.head is None:

self.head = new_node

else:

while temp.next is not None:

temp = temp.next

temp.next = new_node

def printList(self):

print("linked list")

temp = self.head

while temp is not None:

print(temp.data,end=" ")
temp = temp.next

print()

#sepration method for even and odd elements in two different lists

def sepration(self):

temp = self.head

even=LinkedList()

odd=LinkedList()

while temp is not None:

if temp.data % 2 == 0:

n=Node(temp.data)

even.append(n)

temp = temp.next

else:

n=Node(temp.data)

odd.append(n)

temp = temp.next

print("even",end=" ")

even.printList()

print("odd",end=" ")

odd.printList()

n1=Node(21)

n2=Node(22)

n3=Node(33)

n4=Node(42)

n5=Node(54)

n6=Node(66)

n7=Node(77)

ll1=LinkedList()
ll1.append(n1)

ll1.append(n2)

ll1.append(n3)

ll1.append(n4)

ll1.append(n5)

ll1.append(n6)

ll1.append(n7)

ll1.printList()

ll1.sepration()

Q. 3 :

class Node:

def __init__(self,data):

self.prev=None

self.next=None

self.data=data

class Doubly_linkedlist:

def __init__(self):

self.head=None

def append(self,new_node):

temp=self.head

if temp:

while temp.next:

temp=temp.next

temp.next=new_node

new_node.prev=temp

else:
self.head=new_node

def print(self):

temp=self.head

while temp:

print(temp.data,end=" ")

temp=temp.next

print()

#reverse function

def reverse(self):

print("reversed data")

temp=self.head

while temp.next:

temp=temp.next

while temp:

print(temp.data,end=" ")

temp=temp.prev

n1=Node(101)

n2=Node(102)

n3=Node(103)

n4=Node(104)

n5=Node(105)

n6=Node(106)

dll=Doubly_linkedlist()

dll.append(n1)

dll.append(n2)

dll.append(n3)

dll.append(n4)

dll.append(n5)
dll.append(n6)

dll.print()

dll.reverse()

Q. 4 :

class Node:

def __init__(self,data):

self.prev=None

self.next=None

self.data=data

class Doubly_linkedlist:

def __init__(self):

self.head=None

def append(self,new_node):

temp=self.head

if temp:

while temp.next:

temp=temp.next

temp.next=new_node

new_node.prev=temp

else:

self.head=new_node

def print(self):

temp=self.head

while temp:

print(temp.data,end=" ")
temp=temp.next

print()

#deleting all negative values

def delete_negative(self):

temp=self.head

next_node=temp.next

while temp:

next_node = temp.next

if temp.data<0:

if temp==self.head:

self.head=temp.next

if self.head:

self.head.prev=None

else:

temp.prev.next=temp.next

if temp.next:

temp.next.prev = temp.prev

temp=next_node

n1=Node(10)

n2=Node(-2000)

n3=Node(30)

n4=Node(-400)

n5=Node(505)

n6=Node(-603)

dll=Doubly_linkedlist()

dll.append(n1)

dll.append(n2)

dll.append(n3)

dll.append(n4)
dll.append(n5)

dll.append(n6)

dll.print()

dll.delete_negative()

dll.print()

You might also like