2016-01-13 82 views
-1

我有點卡住了讓我的頭繞着堆棧算法。 堆棧算法的僞代碼是:python中的堆棧實現,索引位置如何工作以及如何檢查堆棧是否爲空?

  1. 檢查堆棧是否已滿。
  2. 如果堆棧已滿,則報告錯誤。
  3. 遞增堆棧指針。
  4. 將新數據項插入堆棧指針所指向的單元並停止。

1)我一直認爲列表或數組從0開始。如果我們在添加數據項之前遞增堆棧指針,那麼我們如何開始添加第一個數據項,如果堆棧指針總是從0開始,第一個項目應該進入索引0 - 但是如果我們在添加之前增加,那麼它不會進入1並且將索引0留空?

2)當我看到在python中實現的實際代碼時,它看起來像這樣:在我的代碼中是否需要檢查堆棧是否已滿並報告錯誤。爲什麼在代碼中我們不增加堆棧指針?

3)如何在我的彈出功能中產生一個錯誤,如果我的堆棧是空的,它說棧是空的?

stack = [] 

def view(): 
     for x in range (len(stack)): 
      print(stack[x]) 
def push(): 
    item = input("Please enter the item you wishto add to the stack: ") 
    stack.append(item) 

def pop(): 
    if stack < 0: 
     print ("stack is empty") 
    else: 
     item = stack.pop(-1) 
     print ("you just popped out: ", item) 

while True: 
    print ("") 
    print("Python implementation of a stack") 
    print("********************************") 
    print("1. view Stack") 
    print("2. Push onto Stack") 
    print("3. Pop out of Stack") 
    print("********************************") 
    print("") 
    menu_choice = int (input("Please enter your menu choice: ")) 
    print ("") 
    print ("") 

    if menu_choice == 1: 
     view() 
    elif menu_choice == 2: 
     push() 
    elif menu_choice == 3: 
     pop() 
+0

如果您有多個問題,您應該孤立他們,並分別問他們。請參閱http://www.stackoverflow.com/help/mcve –

+0

,但他們都參考這個代碼 –

回答

0

這是一個有點難以回答這樣廣泛的問題,但希望這將有助於你:

1)請注意,您的堆棧是否爲「空」的條件是與否指針小於0:if stack < 0。這表明您在-1處啓動堆棧指針,並且在添加項目時,將其增加到0,以便將第一個項目放入「零」框。但是,如果您更改了算法的其餘部分以解決此問題,則更改檢查順序是有效的。

2)你引用的代碼不包括在問題中?問題底部的代碼顯然是錯誤的。無論如何,當且僅當堆棧的「頂部」正在移動時,您應該增加堆棧指針,並且您應該檢查新位置是否有效。

3)通常在Python中,您希望'引發'適當的異常。您可以使用the standard ones之一或創建自己的。一個IndexError在這裏將是適當的。

總之,你想要的東西更多這樣的(注:未經):

def class Stack: 
    stack_ptr = -1 
    stack = [] 
    max_length = 10 

    def push(self, item): 
     if stack_ptr < max_length: 
      stack[stack_ptr] = item 
      stack_ptr = stack_ptr + 1 
     else: 
      raise IndexError("Maximum stack length reached!") 

    def pop(self): 
     if stack_ptr < 0: # Need to refer to the pointer, not the stack 
      raise IndexError("Stack is empty!") 
     else: 
      item = stack[stack_ptr) # get item 
      stack_ptr = stack_ptr - 1 # Decrement pointer 
      return item 

    def manipulate_stack(self): 
     while True: 
      print_options() # Put all the printing mishagus in it's own function 
      menu_choice = int (input("Please enter your menu choice: ")) 
      print("") 
      print("") 

      if menu_choice == 1: 
       view() # unimplemented 
      elif menu_choice == 2: 
       try: 
        item = input("Please enter the item you wish to add to the stack: ") 
        push(item) 
       except IndexError: 
        print("The stack is full! You cannot add an item.") 
      elif menu_choice == 3: 
       try: 
        item = pop() 
        print("You have popped item={}".format(item)) 
       except IndexError: # Handle it when something goes wrong 
        print("The stack is empty. You cannot pop!") 

stack = Stack() 
stack.manipulate_stack() 
+0

在問題2我指的是在底部的代碼3)。 –

+0

這段代碼顯然是錯誤的。如果'stack'是一個數組,則不能有'stack <0'。 –

+0

3)是否有可能,而不是索引irror我可以返回一個打印語句給用戶,如「你試圖從空列表中彈出」。如果是的話,我該怎麼做? –