Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Not Able To Delete First Node From Python Linked List
#1
I have a linked list in python that I am presently implementing. However the scenario of deleting the first node isnt working for me.

Deleting the node at any other point in the list works fine.

I have searched youtube tutorials and other online posts but it seems to have the exact same lines of code that I have.

I have also tried putting print statements in before the lines - self.head = HeadVal.nextval - HeadVal = None

From the output, it does seem to pass the headnode onto the second element of the list and clear the original first item, but when I print the list again the original first node hasnt been deleted. Its almost like the new list structure hasnt been committed to memory if that makes sense?

class Node: 
            def init(self, dataval): 
                    self.dataval = dataval 
                    self.nextval = None

class LinkedList: 
            def init(self): 
                     self.headval = None


            def listprint(self):
                      printval = self.headval

                      while printval is not None:
                           print (printval.dataval)
                           printval = printval.nextval

             def removeNode(self):

                       nodeToRemove = input("Enter Node To Remove: ")
                       HeadVal = self.headval

                       if (HeadVal is not None):
                         if (HeadVal.dataval == nodeToRemove):
                             self.head = HeadVal.nextval
                             HeadVal = None
                             return
                         else:
                              while (HeadVal is not None):
                                 if HeadVal.dataval == nodeToRemove:
                                 break
                                 prev = HeadVal
                                 HeadVal = HeadVal.nextval

                         prev.nextval = HeadVal.nextval
                         HeadVal = None
                    else:
                         print("Nothing To Delete")




def MainMenu():

print("1. Create A Linked List")
print("2. Delete A Linked List")
print("3. Check If A Linked List Is Empty")
print("4. Print Out The Values In The List")
print("5. Find A Node In A Linked List")
print("6. Insert A Node In A Linked List")
print("7. Delete A Node In A Linked List")
print("99. Exit")

anotherOption = True

while anotherOption == True:

    print("")
    selection=int(input("Main Menu --- Enter Choice: "))

    if selection==1:    
        myList = LinkedList()
        myList.init()
        print("Linked List Created")

    elif selection==6:
        print("")
        print("A. Insert A Node Into The Front Of The Linked List")
        print("B. Insert A Node Into The End Of The Linked List")       
        print("")
        entrySelection=input("Enter Insert Node Choice: ")

        if entrySelection in ["A", "a"]:
            myList.insertAtBegining()
        elif entrySelection in ["B", "b"]:
            myList.insertAtEnd()

        else:
            print("")
            print("Enter A Valid Selection For Inserting Node")



    elif selection==4:
        myList.listprint()


    elif selection==7:
        myList.removeNode()

    elif selection==99:
        anotherOption = False
        print("")
        print("Exiting Main Menu")

    else:
        print("")
        print("Enter A Valid Selection On Main Menu")

MainMenu()
Quote

Top Page

Messages In This Thread
Not Able To Delete First Node From Python Linked List - by ribena1980 - Mar-04-2019, 07:17 PM

Possibly Related Threads...
Thread Author Replies Views Last Post
  delete a Python object that matches an atribute portafreak 2 109 Feb-19-2020, 12:48 PM
Last Post: portafreak
  delete from list metro17 8 344 Nov-19-2019, 06:42 AM
Last Post: buran
  How do I delete symbols in a list of strings? Than999 1 199 Nov-16-2019, 09:37 PM
Last Post: ibreeden
  circle node with horizontal line in python graphviz mandana 0 357 May-05-2019, 10:39 AM
Last Post: mandana
  Object type of Node to json bhojendra 4 711 Apr-28-2019, 01:36 PM
Last Post: bhojendra
  How to list number of times element is in defaultdict and delete it mrapple2020 3 433 Apr-15-2019, 07:34 AM
Last Post: perfringo
  Deleting the first item in linked list dan789 7 641 Mar-05-2019, 06:34 PM
Last Post: ichabod801
  How to get a random item from a list, print it and delete it? giorgosmarga 3 522 Feb-22-2019, 05:33 PM
Last Post: Yoriz
  Send The output of Gyroscope sensor to node red jenkins43 1 395 Feb-07-2019, 11:00 AM
Last Post: Larz60+
  xml element inside text node SgtPembry 9 720 Oct-21-2018, 11:07 PM
Last Post: SgtPembry

Forum Jump:


Users browsing this thread: 1 Guest(s)