{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chapter5 - CHAPTER 5 Stacks Chapter Objectives To learn...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 5 Stacks
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter Objectives To learn about the stack data type and how to use its four functions: push pop top empty To understand how C++ implements a stack To learn how to implement a stack using an underlying array or linked list To see how to use a stack to perform various applications, including finding palindromes, testing for balanced (properly nested) parentheses, and evaluating arithmetic expressions
Image of page 2
Stacks are Specialized Lists A client using a list can access any element, remove any element, and insert an element anywhere in the list A client using a stack can access (and remove) only the most recently inserted element, and can insert an element only at the “top” of the stack Stacks are among the most commonly used data structures in computer science
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Section 5.1 The Stack Abstract Data Type
Image of page 4
Stack Abstract Data Type A stack is one of the most commonly used data structures in computer science A stack can be compared to a Pez dispenser Only the top item can be accessed You can extract only one item at a time The top element in the stack is the last added to the stack (most recently) The stack’s storage policy is
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Specification of the Stack Abstract Data Type Only the top element of a stack is visible; therefore the number of operations performed by a stack are few We need the ability to test for an empty stack (empty) retrieve the top element (top) remove the top element (pop) put a new element on the stack (push)
Image of page 6
Specification of the Stack Abstract Data Type (cont.)
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
A Stack of Strings We create the (a) stack using the following statements: stack<string> names; names.push("Rich"); names.push("Debbie"); names.push("Robin"); names.push("Dustin"); names.push("Jonathan");
Image of page 8
A Stack of Strings (cont.) For stack names in (a), the value of names.empty () is false The statement string last = names.top(); stores "Jonathan" in last without changing names The statement names.pop() removes " Jonathan " from names The stack names now contains four elements as shown in (b)
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
A Stack of Strings (cont.) The statement names.push("Philip"); pushes "Philip" onto the stack The stack names now contains five elements and is shown in (c)
Image of page 10
Section 5.2 Stack Applications
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Finding Palindromes Palindrome: a string that reads identically in either direction, letter by letter (ignoring case) kayak "I saw I was I" “Able was I ere I saw Elba” "Level madam level" Problem : Write a program that reads a string and determines whether it is a palindrome
Image of page 12
Finding Palindromes (cont.) Analysis : Possible solutions: Set up a loop in which we compare the characters at each end of a string as we work towards the middle If any pair of characters is different, the string can’t be a palindrome Scan a string backward (from right to left) and append each character to the end of a new string, which would become the reverse of the original string. Then we can determine see if the strings were equal
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern