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

Linked List

Uploaded by

BskTeja
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)
8 views3 pages

Linked List

Uploaded by

BskTeja
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/ 3

In [162]: class Node:

def __init__(self,data=None,next_=None):
self.data = data
self.next = next_
# print(self.data,self.next)

class LinkedList:

def __init__(self,head=None):
self.head = None

def insert_at_begining(self,data):
node = Node(data,self.head)
self.head=node
# print(node.data,node.next,self.head)

def insert_at_end(self,data):
node = Node(data)

if self.head == None:
self.head=node
else:
current = self.head
while current.next:
current= current.next
current.next = node

def insert_values(self,data_values):
for val in data_values:
self.insert_at_end(val)

def len_of_list(self):
len_ = 0
curr=self.head
while curr:
len_+=1
curr=curr.next
return len_

def insert_at_index(self,val_,index):
if (index+1) > self.len_of_list() :
print('not possible')
return

if index==0:
node = Node(val_)
node.next = self.head
self.head = node
return

cnt_=0
prev_=None
curr_=self.head
while cnt_<index:
prev_=curr_
curr_=prev_.next
cnt_+=1
node = Node(val_)
node.next =curr_
prev_.next = node
print(node)

def print_list(self):
if self.head == None:
print("Blank Linkedll ist")
return

itr=self.head
while itr:
print(itr.data,itr.next)
itr= itr.next

In [164]: if __name__ == '__main__':


# Node(2)
# Node(4)
# n = Node()
# print(n.data,n.next)
# n.data = 5
# print(n.data,n.next)

l = LinkedList()
# print(l.head)
# l.remove_index(3)
# l.len_of_list()
l.insert_at_begining(2)
l.insert_at_begining(3)
l.insert_at_begining(4)
l.insert_at_begining(5)

# l.print_list()
# l.remove_index(4)
# l.print_list()
# l.len_of_list()

l.insert_at_end(2)
l.insert_at_end(3)
l.insert_at_end(4)

# l.len_of_list()

l.insert_values([5,6,7])

# l.print_list()
# l.remove_index(10)
# l.print_list()

l.insert_at_index('ram',0)

l.print_list()
l.insert_at_index('435',5)

l.print_list()

# l.len_of_list()

# l.print_list()

ram <__main__.Node object at 0x000002C26C301EE0>


5 <__main__.Node object at 0x000002C26C7229D0>
4 <__main__.Node object at 0x000002C26C722280>
3 <__main__.Node object at 0x000002C26C722E20>
2 <__main__.Node object at 0x000002C26C301A60>
2 <__main__.Node object at 0x000002C26C301460>
3 <__main__.Node object at 0x000002C26C301850>
4 <__main__.Node object at 0x000002C26C3016A0>
5 <__main__.Node object at 0x000002C26C3013A0>
6 <__main__.Node object at 0x000002C26C3011F0>
7 None
<__main__.Node object at 0x000002C26C05F8E0>
ram <__main__.Node object at 0x000002C26C301EE0>
5 <__main__.Node object at 0x000002C26C7229D0>
4 <__main__.Node object at 0x000002C26C722280>
3 <__main__.Node object at 0x000002C26C722E20>
2 <__main__.Node object at 0x000002C26C05F8E0>
435 <__main__.Node object at 0x000002C26C301A60>
2 <__main__.Node object at 0x000002C26C301460>
3 <__main__.Node object at 0x000002C26C301850>
4 <__main__.Node object at 0x000002C26C3016A0>
5 <__main__.Node object at 0x000002C26C3013A0>
6 <__main__.Node object at 0x000002C26C3011F0>
7 None

In [ ]:

In [ ]:

You might also like