AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Wortumkehr mit ADT Stapel

Ein Thema von seppel19901 · begonnen am 28. Mär 2009 · letzter Beitrag vom 30. Mär 2009
Antwort Antwort
Seite 2 von 2     12   
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
Antwort Antwort
Seite 2 von 2     12   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 03:13 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz