Lecture 15 h.stack-queue.pdf - Introduction to Object-Oriented Programming COMP2011 Data Structures Stack Queue Prof Andrew Horner Dr Cecia Chan Dr

Lecture 15 h.stack-queue.pdf - Introduction to...

This preview shows page 1 - 8 out of 21 pages.

Introduction to Object-Oriented Programming COMP2011: Data Structures — Stack & Queue Prof. Andrew Horner Dr. Cecia Chan Dr. Cindy Li Department of Computer Science & Engineering The Hong Kong University of Science and Technology Hong Kong SAR, China developed by Prof. Brian Mak { horner, kccecia, lixin } @cse.ust.hk COMP2011 (Spring 2018) 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. An abstract data type (ADT) is the mathematical model of a data structure that is independent of its implementation. It may be used to analyze the efficiency of algorithms. { horner, kccecia, lixin } @cse.ust.hk COMP2011 (Spring 2018) 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. { horner, kccecia, lixin } @cse.ust.hk COMP2011 (Spring 2018) p.3
Image of page 3
Part I Stack { horner, kccecia, lixin } @cse.ust.hk COMP2011 (Spring 2018) 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 { horner, kccecia, lixin } @cse.ust.hk COMP2011 (Spring 2018) 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 int top() const ; // Retrieve the value of the top item // MUTATOR member functions void push( int ); // Add a new item to the top of the stack void pop(); // Remove the top item from the stack }; { horner, kccecia, lixin } @cse.ust.hk COMP2011 (Spring 2018) p.6
Image of page 6
Stack of int Data — Test Program #include "int-stack.h" /* File: int-stack-test.cpp */ void print_stack_info( const int_stack& s) { cout << "No. of data currently on the stack = " << s.size() << "\t" ; if (!s.empty())
Image of page 7
Image of page 8

You've reached the end of your free preview.

Want to read all 21 pages?

  • Spring '17
  • wert
  • Binary numeral system, int data, Prof. Andrew Horner

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