2014-10-07 2 views
0

이 python 프로그램은 줄의 이름을 추적하는 데 사용되는 정렬 된 링크 목록입니다. joinFriend 함수로 고민하고 있습니다. "person first"다음에 "person second"를 추가해야합니다. 첫 번째 줄이 아니라면 오류 메시지를 제공해야합니다. 누구든지 도와 줄 수 있습니까? 나는 클래스와 위대한 일을하고 있지 않다 '(정렬 링크 된 목록에 요소 삽입

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() 
+0

예상 출력은 무엇입니까? –

+0

관련 : [Pyth 연결된 목록에 O (1) 삽입/제거] (http://stackoverflow.com/q/2154946/608639). – jww

답변

0

이 함께 시도하십시오 :

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) 

출력 :

1 John 
2 Mike 
3 Alec 
4 Celia 
5 Jim 

출력 : (제외)

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

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