Stacks and Queues by David Guichard

Stacks and Queues by David Guichard - 3 Stacks and Queues...

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

View Full Document Right Arrow Icon
3 Stacks and Queues Queues and stacks are two special list types of particular importance. They can be imple- mented using any list implementation, but arrays are a more practical solution for these structures than for general purpose lists. A stack is a list with the operations insert and remove restricted to one end of the list, called the top; insert and remove are usually called push and pop respectively. The natural analogy is to a stack of books: it is easy to add or retrieve a book from the top of a pile, but more difficult in the middle of the pile. Another good model is the spring loaded stack of dishes found in many dining halls and cafeterias; ‘push’ and ‘pop’ seem particularly appropriate in this context. The properties of a stack are often referred to as LIFO (Last In First Out), which is sometimes also used as a noun, that is, a stack is a lifo. Stacks are often useful in systems programming and organization. A simple example of a use for a stack is as a text buffer in a line editor. As a line of text is typed in, it is stored in a stack. When a mistake is noticed and the delete key is pressed the editor merely pops the stack, thus eliminating the most recently typed character. Stacks are usually implemented as arrays. In any given application there will typically be only a few stacks in use, so the problem of wasted space is not important. If stacks are expected to be highly variable in size, it may be appropriate to use a pointer based approach. 27
Image of page 1

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

View Full Document Right Arrow Icon
28 Chapter 3 Stacks and Queues For the array implementation we can borrow directly the naive array based imple- mentation of lists. In the record declaration the lastentry field would usually be called top , and the push and pop routines would of course not allow for a parameter indicating position in the stack. Typically the pop function will not only remove the top item from the stack but return the value of the item. Sometimes this is the only way to examine a value, and there is no separate retrieve function. On the other hand, in some mod- ified implementations, it may be possible to examine any item on the stack, but only to insert and remove at the top. In this case, the get_top function might be declared as get_top(int&,int) , and the call get_top(m,-2) would retrieve the value stored in the second item below the top of the stack. class int_stack { public: int_stack(); // initialize a stack with default capacity int_stack(int); // initialize a stack with capacity at least n // The next 4 functions return true on success, false on failure bool push(int n); // push n on the stack bool pop(int& n); // put the top value in n and pop the stack bool get_top(int& n); // put the top value in n bool clear(); // reinitialize the stack so it is empty bool empty(); // return true if the stack is empty, false otherwise bool full(); // return true if the stack is full, false otherwise int size(); // return number of items currently on the stack void dump(); // dump the stack to cerr private: int top,capacity; int* stk; } Figure 3.1 Array based stack 3.1.1 Arithmetic evaluation A good illustration of the use of stacks is in the evaluation of arithmetic expressions. We
Image of page 2
Image of page 3
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