CP114-Stacks lectures - Stacks A stack is a linear collection whose elements are added and removed from one end A stack is LIFO last in first out The

CP114-Stacks lectures - Stacks A stack is a linear...

This preview shows page 1 - 9 out of 29 pages.

Stacks A stack is a linear collection whose elements are added and removed from one end A stack is LIFO – last in, first out The last element to be put on the stack is the first element to be removed A stack is usually depicted vertically, with additions and deletions occurring at the top of the stack
Image of page 1

Subscribe to view the full document.

A conceptual view of a stack
Image of page 2
figure 6.2 The operations on a stack
Image of page 3

Subscribe to view the full document.

Using Stacks Stacks are particularly helpful when solving certain types of problems Consider the undo operation in an application keeps track of the most recent operations in reverse order
Image of page 4
Array Implementation of Stacks Top index 0 1 2 : array (upside-down) int top; float entry[MAXSTACK];
Image of page 5

Subscribe to view the full document.

#define MAXSTACK 20 struct StackRec { int top; float entry[MAXSTACK]; }; typedef struct StackRec Stack; Stack: . . . entry: top:
Image of page 6
#ifndef STACKH #define STACKH #include <stdbool.h> #define MAXSTACK 20 struct StackRec { int top; float entry[MAXSTACK]; }; typedef struct StackRec Stack; void intializeStack(Stack* stackPtr); bool stackEmpty(const Stack* stackPtr); bool stackFull(const Stack* stackPtr); void push(Stack* stackPtr, float item); float pop(Stack* stackPtr); #endif
Image of page 7

Subscribe to view the full document.

#include <stdio.h> #include <stdlib.h> #include “stack.h”
Image of page 8
Image of page 9
  • Fall '13
  • sta
  • Forth

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes