lecture3-1 - Dr.YingwuZhu HowdoesaStackWork...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
    Review of Stacks and Queues Dr. Yingwu Zhu
Background image of page 1

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

View Full Document Right Arrow Icon
    How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation  Removing an item Pop operation Properties Ordered collection of items Be accessed at only the top
Background image of page 2
    Building a Stack Class Design a stack class   stack.h (header file) Implement the stack class   stack.cpp (implementation  file)
Background image of page 3

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

View Full Document Right Arrow Icon
    Building a Stack Class Need to determine the  data structure  to store data items  (array or linked list?) Need to determine the  algorithms  to perform operations  on the data items
Background image of page 4
    Building a Stack Class What data items do we need? An array/linked-list to hold stack elements An integer/pointer to indicate the top of stack What operations do we need? Constructor: build an empty stack Empty: check if a stack is empty Push: add an element on the top Top: return the top element Pop: remove the top element Display: show all the stack elements
Background image of page 5

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

View Full Document Right Arrow Icon
    A Static Array-Based Stack (p327) A static array to hold the stack elements, the position 0 as  the bottom of the stack Problem?   the stack capacity cannot be changed during  run time (waste of space or insufficient room for more  data items)
Background image of page 6
    Dynamic Array-Based Stack Advantage:  allow users to specify the stack capacity in its  declaration But, we need to do more ! (Due to the dynamically- allocated memory) Constructor: memory allocation and data member initialization  Destructor: reclaim the dynamically-allocated memory, avoid  memory leak Copy constructor:  NO  “shallow copy” (p339) Assignment operator: assign one object to another,  NO  “shallow  copy”
Background image of page 7

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

View Full Document Right Arrow Icon
  Example Codes Explanation (p336) Figure7.6: 
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 34

lecture3-1 - Dr.YingwuZhu HowdoesaStackWork...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online