Einzelnen Beitrag anzeigen

blackdrake

Registriert seit: 21. Aug 2003
Ort: Bammental
618 Beiträge
 
Delphi 10.3 Rio
 
#11

Re: Wortumkehr mit ADT Stapel

  Alt 30. Mär 2009, 06:58
Length() und Copy() bzw. str[] entspricht jedoch nicht der Aufgabenstellung, die vorsieht, das ganze mit einem ADT (Abstrakter Datentyp = Klasse) sowie eines Stapels zu lösen.

Man braucht dafür
1. Eine Listenklasse (ggf. will der Prof. sehen, dass man eine eigene baut)
2. Eine TStapel-Klasse, die ein Feld der Listenklasse hat

Dieser TStapel hat dann die Add "push" und Get+Remove "pop" Funktion, mit der ein oberstes Element entfernt oder hinzugefügt wird.

Dann schreibst du das Wort ABC in den Stapel und liest es einfach wieder aus. Dann ist es automatisch in der umgekehrten Reihenfolge. Der Vorteil eines Stapels ist eben, dass du beliebige Datentypen reinpacken kannst.

Hier ein Code aus Java, den du portieren kannst:

Delphi-Quellcode:
package exercise05;

public class IntegerStack {

   /*
    * Fields
    */
   private IntegerList stack;

   /**
    * Constructor of the stack
    */
   public IntegerStack() {
      this.stack = new IntegerList();
   }


   /**
    * Inserts a element to the beginning
    * @param value The value for inserting
    */
   public void push(int value) {
      this.stack.addFirst(value);
   }


   /**
    * Takes away the first element
    * @return The value of the element
    */
   public int pop() {
      if (this.stack.isEmpty()) {
         // Simple "Exception" if the stack is empty
         println("Error: Stack underflow!");
         return -1;
      }
 else {
         return this.stack.removeFirst();
      }

   }

   /**
    * Determinate the element at the top of the stack
    * @return The value of the element
    */
   public int top() {
      if (isEmpty()) {
         // Simple "Exception" if the stack is empty
         println("Error: Stack is empty!");
         return -1;
      }
 else {
         return this.stack.getFirstValue();
      }

   }

   /**
    * Clears the stack
    */
   public void empty() {
      this.stack.clear();
   }


   /**
    * Determinate if the stack is empty
    * @return True if the stack is empty
    */
   public boolean isEmpty() {
      return this.stack.isEmpty();
   }

   
   /**
    * Print the stack to the screen
    */
   public void print() {
      this.stack.printList();      
   }


   /**
    * Returns the size of the stack
    * @return Size of the stack
    */
   public int size() {
      return this.stack.size();      
   }

}
Daniel Marschall
  Mit Zitat antworten Zitat