Jump to content

python linked list

digitaldoughnut

So i need to write a linked list program in python and i am nearly finished except that i am stuck on 2 functions, being adding and removing names. I will paste the program below, and any help would be great. Thanks :) 

 

# linked list program
# uses 1st 2 records in list a system records for start of list pointer and end of list/nul.

class person (object):
    def __init__(self, givName, famName, ptr):
        self.givenName = givName
        self.familyName = famName
        self.ptr = ptr

#Search function
startptr = 0
def search(search_value):
    currentptr = startptr
    while linkedList[currentptr].familyName < search_value and linkedList[currentptr].ptr != NUL:
        currentptr = linkedList[currentptr].ptr
        
    if linkedList[currentptr].familyName == search_value:
        print("Name found!")
    else:
        print("Name not found.")
    
    #return currentptr
    

# initialise list with 3 entries    
NUL = 1
linkedList = [person("Start of list pointer", "@@@SYSTEM RECORD", 3),
              person("End of list/nul record", "{{{ SYSTEM RECORD",999),
              person("Joseph", "Stalin", 1 ),
              person("Winston", "Churchill", 4),
              person("Franklin D", "Roosevelt", 2)]
     


size = 3
newRecordPtr = 5

 

#user input

while True:
    choice = input("\nEnter:\n\n\t+ to add\n\t- to remove\n\tp to display in physical order\n\tf to display in family name order\n\ts to search\n\tx to exit\n\tchoice: ")

 

    #Add name choice
    if choice == "+":
        add_name("add")
        print('do this fourth')

    #Remove name choice.            
    elif choice =="-":
        print('do this fifth')
        
    #Physical order choice    
    elif choice.lower() == "p":
        for rec in linkedList:
            print(rec.familyName, rec.givenName, rec.ptr)
            
    #Link list order        
    elif choice.lower() == "f":
        current_pointer = linkedList[0].ptr
        while current_pointer != NUL:
            print("\n",linkedList[current_pointer].familyName)
            current_pointer = linkedList[current_pointer].ptr
            
    #Search choice    
    elif choice.lower() == "s":
        
        while True:
            search_value = input("Please enter the family name you would like to search for: ").lower().title()

        #Validation, ".isalpha" function will stop nul entries and blank entries.
            if search_value.isalpha():
                break
            else:
                print("Invalid name, please enter a valid family name.")
            
        print("\nSearching for name...")
        #print(currentptr)
        search(search_value)

        
        
    #quit option              
    elif choice.lower() =="x":
        print ("Quitting")
        break
   

#if all inputs are invalid then:     
    else:
        print("Invalid option, please try again!")

linked list starter program.py

Link to comment
Share on other sites

Link to post
Share on other sites

Code tags please.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Im not really sure what you are trying to achieve but I would suggest that you create a linked list class with add, remove and search methods. 

Something like this: 

class person:
    def __init__(self, givenName, familyName):
        self.givenName = givenName
        self.familyName = familyName
        self.next = None
class linked_list:
    def __init(self):
        self.head = None
    
    def add(self, person):
        if self.head == None:
            self.head = person
            person.next = None
        else:
            person.next = self.head
            self.head = person            

    def remove(self, person):
        ...        
    def search(self, value):
        ...

Then do

p1 = person('joseph','stalin')
linkedlist = linked_list()
linkedlist.add(p1)
linkedlist.remove(p1)

And so on. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×