12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

124 Chap. 4 Lists, Stacks, <strong>and</strong> Queuesa = a + b;b = a - b; // Now b contains original value of aa = a - b; // Now a contains original value of bA similar effect can be had by using the exclusive-or opera<strong>to</strong>r. This factis widely used in computer graphics. A region of the computer screen canbe highlighted by XORing the outline of a box around it. XORing the boxoutline a second time res<strong>to</strong>res the original contents of the screen.4.2 StacksThe stack is a list-like structure in which elements may be inserted or removedfrom only one end. While this restriction makes stacks less flexible than lists, italso makes stacks both efficient (for those operations they can do) <strong>and</strong> easy <strong>to</strong> implement.Many applications require only the limited form of insert <strong>and</strong> removeoperations that stacks provide. In such cases, it is more efficient <strong>to</strong> use the simplerstack data structure rather than the generic list. For example, the freelist ofSection 4.1.2 is really a stack.Despite their restrictions, stacks have many uses. Thus, a special vocabularyfor stacks has developed. Accountants used stacks long before the invention of thecomputer. They called the stack a “LIFO” list, which st<strong>and</strong>s for “Last-In, First-Out.” Note that one implication of the LIFO policy is that stacks remove elementsin reverse order of their arrival.It is traditional <strong>to</strong> call the accessible element of the stack the <strong>to</strong>p element. Elementsare not said <strong>to</strong> be inserted; instead they are pushed on<strong>to</strong> the stack. Whenremoved, an element is said <strong>to</strong> be popped from the stack. Figure 4.17 shows asample stack ADT.As with lists, there are many variations on stack implementation. The two approachespresented here are array-based <strong>and</strong> linked stacks, which are analogous<strong>to</strong> array-based <strong>and</strong> linked lists, respectively.4.2.1 Array-Based StacksFigure 4.18 shows a complete implementation for the array-based stack class. Aswith the array-based list implementation, listArray must be declared of fixedsize when the stack is created. In the stack construc<strong>to</strong>r, size serves <strong>to</strong> indicatethis size. Method <strong>to</strong>p acts somewhat like a current position value (because the“current” position is always at the <strong>to</strong>p of the stack), as well as indicating the numberof elements currently in the stack.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!