Python Forum
Not Able To Delete First Node From Python Linked List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not Able To Delete First Node From Python Linked List
#7
Hi Folks,

Apologies. I accidentally cut the code of how Im deleting the first node.

Thanks ichabod801, I take your point about __init__ but is that likely to be a factor in this instance?


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 insertAtBegining(self):
        
        nodeToPutAtStart = input("Enter Node To Put At The Start: ")
        
        startNode = Node()
        startNode.init(nodeToPutAtStart)
        
        
        startNode.nextval = self.headval
        self.headval = startNode
        
        print(nodeToPutAtStart, " Has Been Added To The Start Of The Linked List")


        
        
        
    def insertAtEnd(self):

        nodeToPutOnEnd = input("Enter Node To Put On The End: ")
        
        endNode = Node()
        endNode.init(nodeToPutOnEnd)
        
        if self.headval is None:
            self.headval = endNode
            return
        
        laste = self.headval
        
        while(laste.nextval):
            laste = laste.nextval
        
        laste.nextval=endNode
        
        print(nodeToPutOnEnd, " Has Been Added To The End Of The Linked List")
        
        
        
        
        
        
        
        
    def insertInbetween(self):
        
        middleNodeUserInput = input("Enter Middle Node: ")
        nodeToAddUserInput = input("Enter New Note To Add: ")
            
        middleNode = Node()
        middleNode.init(middleNodeUserInput)

        nodeToAdd = Node()
        nodeToAdd.init(nodeToAddUserInput)
        
        nodeToAdd.nextval = middleNode.nextval
        middleNode.nextval = nodeToAdd
        
        
        
    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:
        	           self.head = HeadVal.nextval
        	           HeadVal = None
        	           break
        	       prev = HeadVal
        	       HeadVal = HeadVal.nextval
        	       
        	    prev.nextval = HeadVal.nextval
	            HeadVal = None
	    else:
	        print("Nothing To Delete")
    
    
    
    
    
    def isEmpty(self):
        HeadVal = self.headval
        total = 0
        
        while (HeadVal is not None):
            total+=1
            HeadVal = HeadVal.nextval

        if(total == 0):
            print("List Is Empty")
        else:
            print("List Is Not Empty")
            
        print(total, " Elements In List")











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("C. Insert A Node In Between Nodes On The Linked List")
		    
		    print("")
		    entrySelection=input("Enter Insert Node Choice: ")
		    
		    if entrySelection in ["A", "a"]:
    			myList.insertAtBegining()
		    elif entrySelection in ["B", "b"]:
		        myList.insertAtEnd()
                
		    elif entrySelection in ["C", "c"]:
		        myList.insertInbetween()

		    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")
			
		elif selection==3:
		    myList.isEmpty()	
					
			
		else:
		    print("")
		    print("Enter A Valid Selection On Main Menu")
			
MainMenu()
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  Node Flow Generation in Python Linenloid 0 651 Feb-21-2023, 07:09 PM
Last Post: Linenloid
  Python Split json into separate json based on node value CzarR 1 5,580 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  DELETE Post using Python FaceBook Graph API BIG_PESH 0 1,452 Mar-24-2022, 08:28 PM
Last Post: BIG_PESH
  About linked lists ManoEl 2 1,597 Oct-17-2021, 03:21 PM
Last Post: ManoEl
  Delete list while iterating shantanu97 1 1,884 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  I cannot delete and the elements from the list quest 4 2,966 May-11-2021, 12:01 PM
Last Post: perfringo
Star --- Python lists and Linked Lists --- sabe 3 2,683 Nov-22-2020, 05:51 PM
Last Post: DeaD_EyE
  How to create a linked list and call it? loves 12 4,482 Nov-22-2020, 03:50 PM
Last Post: loves
  reference in pop function for linked list oloap 0 1,561 Mar-14-2020, 05:52 PM
Last Post: oloap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020