Returns the location of a node deleted from the Simple Connections list (modify)

Asked 2 years ago, Updated 2 years ago, 38 views

How do I modify the location of the node I deleted here?

class SinglyList:
    class Node:       
        Def __ init and __ (self, data, link) : #### node created.
            self.data = data
            self.next = link

    Def __ init and __ (self) constructor #### simple connection list.
        self.head = None
        self.size = 0

    def size(self): return self.Size
    def Empty(self):return self.size == 0

    Def insert front the (self, data) : first node into. ####
        Try :
            if self.Empty():
                self.head = self.Node(data, None)
                print("%s inserted as first node."%data)
            else:
                self.head = self.Node(data, self.head)
                print("%s inserted as first node."%data)
            self.size += 1
        except:
            print("The value was entered incorrectly.")

    def insert_after(self, data, p): #### Insert after p
        try:
            p.next = SinglyList.Node(data, p.next)
            self.size += 1
        except:
            print("The value was entered incorrectly.")

    def delete_front(self): #### Delete first node
        try:
            if self.Empty():
                raise EmptyError("underflow")
            else:
                self.head = self.head.next
                self.size -= 1
        except:
            print("The value was entered incorrectly.")

    def delete_after(self,p): ####p Delete next node
        try:
            if self.Empty(): 
                raise EmptyError("underflow")
            t = p.next
            p.next = t.next
            self.size -= 1
        except:
            print("The value was entered incorrectly.")

    def search (self, target): #### target navigation
        try:
            p = self.head
            for k in range(self.size):
                if target == p.data:
                    print(target, "data" is at "k",th.")
                    return k
                p = p.next
            print(target, "Data is not in the list.")
        except:
            print("The value was entered incorrectly.")
        return None

    def get(self): ##### Connection list output
        try:
            if self.size == 0:
                print("List is empty.")
            else:
                p = self.head
                while p:
                    if p.next != None:
                        print(p.data, ' -> ', end='')
                    else:
                        print(p.data)
                    p = p.next
        except:
            print("The value was entered incorrectly.")

    def isEmpty(self): #### Make sure the list is empty
        if self.size == 0:
            print("List is empty.")
        else:
            print("List is not empty.")

classEmptyError (Exception): #### Error handling of value errors
    pass

python python3

2022-09-22 11:15

1 Answers

Shouldn't I return each value? Haha

It is a little questionable whether the intention is correct.

def delete_front(self): #### Delete the first node
    Try :
        if self.Empty():
            raise EmptyError("underflow")
        else:
            self.head = self.head.next
            self.size -= 1

            # More
            return self.head
    except:
        print("The value was entered incorrectly.")    

        # More
        return None

def delete_after(self,p): ####p Delete next node
        Try :
            if self.Empty(): 
                raise EmptyError("underflow")
            t = p.next
            p.next = t.next
            self.size -= 1

            # More
            return p.next 
        except:
            print("The value was entered incorrectly.")

            # More
            return None


2022-09-22 11:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.