Answer to Question #237583 in Python for Simran

Question #237583

Implement your own version of the linked list using Python with mentioned functionalities: insert, insert at position, delete, delete at position, center, reverse, size, iterator, traverse/print.

Use of similar data structures already present in the language/framework is not allowed.



1
Expert's answer
2021-09-15T17:24:01-0400
class Node(object):
    def __init__(s, info):
        s.info = info
        s.next = None
   
class Linked_List(object):
    def __init__(s, head=None):
        s.head = head
    def insert(s, info):
            node = Node(info)
            if not s.head:
                s.head = node
            else:
                node.next = s.head
                s.head = node
    def insertAtPosition(s, info_in):
      New_Node = Node(info_in)
      New_Node.next = s.head
      s.head = New_Node
    def delete(s, info):
        if not s.head:
            return
        
        
        temp = s.head
        if head.info == info:
            temp = s.head
            head = temp.next
            print ("Deleted node is " + str(head.info))
            return


        while(temp.next):
            if (temp.next.data == info):
                print ("Node deleted is " + str(temp.next.info))
                temp.next = temp.next.next
                return
            temp = temp.next
        print ("Node not found")
        return
    def deleteAtPosition(s,info):
        head=temp.next
        print("Deleted node is" +str(head.info))
        return
    def center(s):
        s_ptr = s.head
        f_ptr = s.head
  
        if s.head is not None:
            while (f_ptr is not None and f_ptr.next is not None):
                f_ptr = f_ptr.next.next
                s_ptr = s_ptr.next
            print("The middle element is: ", s_ptr.info)
    def reverseList(s, head):
      return s.solve(head,None)
    
    
    def iterate(s,info):
        while(True):
         print(cur.info)
         if(cur.next is None):
            break
         cur = cur.next
    def traverse(s):
      s.head_dat = None


    
    def search(s, head, info, index):
        if head.info == info:
            print (index)
        else:
            if head.next:
                return s.search(head.next, info, index+1)
            else:
                raise ValueError("Node not found in the linked list")
    def print_list(s):
        if s.head == None:
            raise ValueError("List is empty")


        cur = s.head 
        while(cur):
            print (cur.info, end="  ")
            cur = cur.next
        print ('\n')
    
    def size(s):
        if s.head == None:
            return 0


        size = 0
        cur = s.head
        while(cur):
            size += 1
            cur = cur.next
        return size
L = Linked_List()
L.insert("Jan")
L.insert("Feb")
L.insert("March")
L.insert("April")
L.size
L.print_list()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS