简体   繁体   English

在有序链接列表中插入元素

[英]Inserting an element in an ordered linked list

This python program is an ordered linked list that is used to keep track of names in a line. 这个python程序是一个有序的链表,用于跟踪一行中的名称。 I am struggling with the joinFriend function. 我正在努力与joinFriend函数。 I need to add "person second" at the position right after "person first". 我需要在“第一个人”之后的位置添加“第二个人”。 If first is not in the line, I have to provide an error message. 如果first不在行中,我必须提供一条错误消息。 Can anyone help? 有人可以帮忙吗? I am not doing that great with classes :'( 我在课堂上做得不好:'(

class PersonList(object):
#constructor initializes both instance variables
    def __init__(self, name):
        self.name = name
        self.next = None

    def getName(self):
    #returns the name of the person
        return self.name
    def getNext(self):
    #returns a PersonList object with the next person in line
        return self.next

    def setName(self, newname):
        self.name = newname
    def setNext(self, newnext):
        self.next = newnext

#Line class
class Line(object):
    def __init__(self):
        self.head = None

    def isEmpty(self):
        #constructor initializes an empty linked list
        return self.head == None

    def __str__(self):
    #prints the people currently in line, in order,
        #with a number indicating their position
        current = self.head
        string = ''
        count = 0
        while current != None:
            count = count + 1
            string += str(count) + " " + current.getName() + "\n"
            current = current.getNext()
        return string

    def add(self,name):
    #adds a person to the END of the line
        current = self.head        
        temp = PersonList(name)        
        if current == None:
            self.head = temp
        else:
            while current.getNext() != None:
                current = current.getNext()                
            current.setNext(temp)

    def remove(self, name):
    #removes a person from the line
        current = self.head
        previous = None
        found = False
        while not found:
            if current.getName() == name:
                found = True
            else:
                previous = current
                current = current.getNext()

        if previous == None:
            self.head = current.getNext()
        else:
            previous.setNext(current.getNext())

    def joinFriend(first, second):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.getName() == first:
                found = True
            else:
                previous = current
                current = current.getNext()

        second = PersonList(name)
        if previous == None:
            temp.setNext(self.head)
            self.head = temp
        else:
            temp.setNext(current)
            previous.setNext(second)


def main():
    ln = Line()
    ln.add("John")
    ln.add("Mary")
    ln.add("Alec")
    ln.add("Celia")
    ln.remove("Mary")
    ln.joinFriend("John", "Mike")
    ln.joinFriend("Celia", "Jim")
    print(ln)

main()

Please try with this: 请尝试以下操作:

  def joinFriend(self, first, second):
      current = self.head
      previous = None
      found = False
      while not found:
          try:
              if current.getName() == first:
                  found = True
              else:
                  previous = current
                  current = previous.getNext()
          except Exception:
              print "Error: First person [%s] not found in the list" % first
              sys.exit(1)

      temp = PersonList(second)
      next_item = current.getNext()
      current.setNext(temp)
      temp.setNext(next_item)

Output: 输出:

1 John
2 Mike
3 Alec
4 Celia
5 Jim

Output: (with exception) 输出:(例外)

ln.joinFriend("James", "Jim")

Error: First person [James] not found in the list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM