h.stack-queue.pdf - Programming with C COMP2011 Data Structures \u2014 Stack Queue Cecia Chan Cindy Li Brian Mak Department of Computer Science Engineering

h.stack-queue.pdf - Programming with C COMP2011 Data...

This preview shows page 1 - 7 out of 17 pages.

Programming with C++ COMP2011: Data Structures — Stack & Queue Cecia Chan Cindy Li Brian Mak Department of Computer Science & Engineering The Hong Kong University of Science and Technology Hong Kong SAR, China { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2019) p.1
Image of page 1
Data Structures Computer science is the study of how to process information (data) efficiently using computers. A data structure helps store, organize, and manipulate data in a particular way so that they can be processed efficiently by computers. Different applications require different data structures . Examples: array , linked list , (binary) tree , stack , queue , etc. { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2019) p.2
Image of page 2
Stack and Queue Stack and queue let you insert and remove items at the ends only, not in the middle. { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2019) p.3
Image of page 3
Part I Stack { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2019) p.4
Image of page 4
Stack: How it Works Consider a pile of cookies. more cookies: new cookies are added on top , one at a time. fewer cookies: cookies are consumed one at a time, starting at the top . As an ADT , insertions and removals of items on a stack are based on the last-in first-out (LIFO) policy. It supports: Data : an ordered list of data/items. Operations (major ones): top : get the value of the top item push : add a new item to the top pop : remove an item from the top { kccecia, lixin, mak } @cse.ust.hk COMP2011 (Fall 2019) p.5
Image of page 5
Stack of int Data — stack.h #include <iostream> /* File: int-stack.h */ #include <cstdlib> using namespace std; const int BUFFER_SIZE = 5; class int_stack { private : int data[BUFFER_SIZE]; // Use an array to store data int top_index; // Starts from 0; -1 when empty public : // CONSTRUCTOR member functions int_stack(); // Default constructor // ACCESSOR member functions: const => won’t modify data members bool empty() const ; // Check if the stack is empty bool full() const ; // Check if the stack is full int size() const ; // Give the number of data currently stored
Image of page 6
Image of page 7

You've reached the end of your free preview.

Want to read all 17 pages?

  • Fall '14
  • Method, Binary numeral system, Brian Mak, Stack of int Data

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes