2017-06-02 8 views
-3

私の頭を壁に1週間叩いていて、どこにも見えないようです。スタックから最大値を取得してスタックに保持できるようにしたいので、最後にスタックに1つの値があり、それが最大値です。私は最大を維持するためのアルゴリズムは正しいと信じていますが、私はpopメソッドが誤動作していると思っています。どんな指導も高く評価されます。私が働いている両方のファイルがスタックの最大値を保持するにはどうしたらいいですか?

import java.io.File; 
    import java.io.IOException; 
    import java.util.ListIterator; 
    import java.util.Random; 

    public class LinkedListStack { 

     DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); 

     public int size() { 
      if (list.isEmpty()) { 
       System.out.println("Stack is empty"); 
      } 
      return list.size(); 

     } 

     public void push(Integer s) { 
      list.add(s); 

     } 

     public Integer pop() { 
      /* need help with this method */ 

     } 

     public Integer top() { 
      return list.iterator().next(); 

     } 

     public boolean isEmpty() { 
      return list.isEmpty(); 

     } 

     public String displayStack() { 
      return (list.toString()); 
     } 

     public static void main(String[] args) throws IOException { 
      LinkedListStack stack = new LinkedListStack(); 

      int n, seed; 
      File outputFile; 
      File dir = new File("."); 

      if (args.length > 0) { 
       n = Integer.parseInt(args[0]); 
       seed = Integer.parseInt(args[1]); 
      }else { 
       n = 10; 
       seed = 1; 
      } 

      if (args.length == 3) { 
       outputFile = new File(args[2]); 
      } else { 
       outputFile = new File(dir.getCanonicalPath() + File.separator + "Files/testOut_Stack"); 
      } 
      OutputWriter out = new OutputWriter(outputFile); 

      Random r = new Random(seed); 

      Integer nextval; 
      for (int i = 0; i < n; i++) { 
       nextval = r.nextInt(10000); 

       if (stack.isEmpty()) { 
        stack.push(nextval); 
       } 

       if (nextval > stack.top()) { 
        stack.pop(); 
        stack.push(nextval);  
       } 



       /* retain the max value that you see among the integers generated in the stack. 
       * In the end there should be only one integer in stack, which is the max value 
       */ 
      } 

      // write the content of stack -- which is the max value -- to file 
      out.writeOutput(stack.displayStack()); 
     } 
    } 


import java.io.File; 
import java.io.IOException; 
import java.util.ListIterator; 
import java.util.NoSuchElementException; 
import java.util.Random; 

import csci3230.hw3.OutputWriter; 

public class DoublyLinkedList<Item> implements Iterable<Item> { 
    private int n;  // number of elements on list 
    private Node pre;  // sentinel before first item 
    private Node post; // sentinel after last item 

    public DoublyLinkedList() { 
     pre = new Node(); 
     post = new Node(); 
     pre.next = post; 
     post.prev = pre; 
    } 

    // linked list node helper data type 
    private class Node { 
     private Item item; 
     private Node next; 
     private Node prev; 
    } 

    public boolean isEmpty() { 
     return (n == 0); 
     // your code 
    } 
    public int size() { 
     return n; 
     // your code 
    } 

    // add the item to the list 
    public void add(Item item) { 
     Node last = post.prev; 
     Node x = new Node(); 
     x.item = item; 
     x.next = post; 
     x.prev = last; 
     post.prev = x; 
     last.next = x; 
     n++; 
     // your code 
    } 

    public ListIterator<Item> iterator() { return new DoublyLinkedListIterator(); } 

    // assumes no calls to DoublyLinkedList.add() during iteration 
    private class DoublyLinkedListIterator implements ListIterator<Item> { 
     private Node current  = pre.next; // the node that is returned by next() 
     private Node lastAccessed = null;  // the last node to be returned by prev() or next() 
               // reset to null upon intervening remove() or add() 
     private int index = 0; 

     public boolean hasNext() { 
      return (index < n); // your code 
     } 
     public boolean hasPrevious() { 
      return (index > 0); 
      // your code 
     } 
     public int previousIndex() { 
      return (index - 1); 
      // your code 
     } 
     public int nextIndex() { 
      return (index + 1); 
      // your code 
     } 

     public Item next() { 
      if (!hasNext()) throw new NoSuchElementException(); 
      lastAccessed = current; 
      Item item = current.item; 
      current = current.next; 
      index++; 
      return item; 
      // your code 
     } 

     public Item previous() { 
      if (!hasPrevious()) throw new NoSuchElementException(); 
      current = current.prev; 
      index--; 
      lastAccessed = current; 
      return current.item; 
      // your code 
     } 

     // replace the item of the element that was last accessed by next() or previous() 
     // condition: no calls to remove() or add() after last call to next() or previous() 
     public void set(Item item) { 
      if (lastAccessed == null) throw new IllegalStateException(); 
      lastAccessed.item = item; 
      // your code 
     } 

     // remove the element that was last accessed by next() or previous() 
     // condition: no calls to remove() or add() after last call to next() or previous() 
     public void remove() { 
      if (lastAccessed == null) throw new IllegalStateException(); 
      Node x = lastAccessed.prev; 
      Node y = lastAccessed.next; 
      x.next = y; 
      y .prev = x; 
      n--; 
      if (current == lastAccessed) { 
       current = y; 
      } 
      else { 
       index--; 
      } 
      lastAccessed = null; 
      // your code 
     } 

     // add element to list 
     public void add(Item item) { 
      Node x = current.prev; 
      Node y = new Node(); 
      Node z = current; 
      y.item = item; 
      x.next = y; 
      y.next = z; 
      z.prev = y; 
      y.prev = x; 
      n++; 
      index++; 
      lastAccessed = null; 
      // your code 
     } 

    } 

    public String toString() { 
     StringBuilder s = new StringBuilder(); 
     for (Item item : this) 
      s.append(item + " "); 
     return s.toString(); 




    } 
} 
+0

「スタック」にすべての値を追加する必要がありますか?それ以外の場合は、追加されている値を比較し、 'peek'の値よりも小さい場合は追加しないでください(おそらくあなたの場合は' top')。 – KevinO

+0

スタックに追加された値が 'top'より大きい場合は、 'top'を引き出して新しい値を 'push'する必要がありますか? – jkgram43

+0

'DoublyLinkedList.add()'はリストの最後に追加されます。 'list.iterator()。remove()'は、リストの最初の要素を削除します。これは* stack *ではなく* queue *の機能です。その上に、 'list.iterator()。previous()'の後に*値を削除すると失敗します。つまり、何かを返すか、死んでしまいます。 'DoublyLinkedList'がリストの最初の要素と最後の要素の両方に簡単にアクセスできるので、removeFirst()とremoveLast()メソッドを追加して、それぞれが削除された要素を戻さないようにしましょう。そうすれば、イテレータを使用する必要がなくなり、スタックロジックが非常に簡単になります。 – Andreas

答えて

0

はあなたが一時的な「largestValue」としてポップ最初の整数を維持し、あなたはそれを置き換える必要があるかどうかを確認するためにあなたがそれでポップ後続のすべての整数を比較することができます含まれています。あなたの関数の最中に、すべての値をポップし、その上に最大値を戻します。

+0

私の「ポップ」がうまくいかない理由を理解することができれば、それを行うことができます。それを修正するためのアドバイスはありますか? – jkgram43

+0

あなたがその罰金を知らないならば、私はちょうど全く手がかりがありません – jkgram43

+0

遅く応答して申し訳ありません。ポップするには、単に最後に追加したエントリを削除します。リンクされたリストの場合は、最後の項目の前の項目を何も指定しないでください。 (例えば、Item1→Item2→Item3はItem1→Item2→nullとなる)。しかし、最後のアイテムの内容を保存することを忘れないでください。 – Bustinout

関連する問題