Sei sulla pagina 1di 3

Stacks

Stacks are used to implement a number of things, in particular subprogram calls. It is therefore quite natural to define an Abstract Data Type Stack that can be used in a number of applications. A Stack is an ordered collection of items, where only one item is accessible: the last one entered in the stack. A Stack is a homogeneous structure in which all the elements are of the same type, so the values in a Stack are all of a given type. We can have Stacks of Integers, Stacks of Real Numbers, Stacks of Characters, Stacks of a given record type, and so on. Figure 8.48 shows a small stack of Integers, as well as the operations of the Stack Abstract Data Type. The top item in a Stack is the last one that was entered into the Stack. The first item entered into the Stack is at the bottom of the Stack. The behavior of a Stack is often described as LIFO: Last-In First- Out.

Abstract data type Stacks Values All of the values of the stack element type Operations Create a stack. Push an item onto a stack: the new stack top contains the pushed item. Pop an item from a stack: the top stack item is deleted from the stack and returned as result. Check if a stack is empty. Check if a stack is full. Count the number of elements in a stack. As we have already mentioned, stacks are very useful in computing. Lets use the Abstract Data Type we have just defined to detect palindromes.

We need to develop an algorithm able to detect whether a given String is a palindrome. Palindromes are sequences of characters that read the same forwards as backwards (usually the spaces are not taken into account). For example, the sequence RADAR is a palindrome, while RADIO is not. The sentence EVIL DID LIVE is a palindrome. Some other well known palindromes are ABLE WAS I ERE I SAW ELBA, and A MAN A PLAN A CANAL PANAMA. Figure 8.49 shows how we can test a String for this property. A string STOPS is input character by character into both Stacks S1 and S2. This could be done by a sequence of pushes, as shown in Pseudocode 8.29.

The Pour operation transfers the contents of Stack S1 into Stack S3. Notice that doing this reverses the order of the contents of the Stack: the top item of S1becomes the bottom item of S3. This reversing transfer is done by the algorithm in Pseudocode 8.30.

Finally the character sequence from Stack S2 can be compared to the reversed sequence in Stack S3 by the following pseudocode.

As another example, the Big Adder of Figure 8.50 shows how two Integers of any length can be added digit by digit. 1. The two numbers are input with most significant digits first, and put into stacks S1 and S2 with the least significant digits on top. The two numbers are 1 and 5 in the given example. 2. The Adder begins with a Carry of 0 on a one-digit-Stack S3. 3. The digit in S3 is added to the sum of the top values of Stacks S1 and S2. 4. The sum digit is placed onto another stack S4, with the new carry replacing the previous carry digit in S3. 5. The output stack S4 is finally output, with the most significant digits leading. The algorithm for Big Adder is shown in Pseudocode 8.32.

Potrebbero piacerti anche