We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
#include <stdio.
h> // stardard input/output library
#include <stdbool.h> // standard boolean library: bool, true, false
#define MAXSIZE 100
bool isEmpty (int* s, int t) {
// returns true if t = -1 // INSERT YOUR CODE HERE return (t == -1); }
bool isFull (int* s, int t) {
// returns true if no more room in the stack // INSERT YOUR CODE HERE return (t == MAXSIZE -1); }
void push(int v, int* s, int* tp) {
// put v onto the top of the stack s unless it is already full // INSERT YOUR CODE HERE if (!isFull(s, *tp)) { *tp = *tp + 1; // Increment the top index s[*tp] = v; // Place the value v on top of the stack } else { // If the stack is full, print an error message printf("Stack is full, cannot push %d\n", v); } }
int pop (int* s, int* tp) {
// return the top entry in the stack unless stack is empty // update s and *tp -- requires top to be passed by reference! // INSERT YOUR CODE HERE if (!isEmpty(s, *tp)) { // If the stack is not empty, return the top value and update the top index int temp = s[*tp]; // Store the top value in a temporary variable *tp = *tp - 1; // Decrement the top index return temp; // Return the popped value } else { // If the stack is empty, print an error message and return -1 printf("Stack is empty, cannot pop\n"); return -1; // Return -1 to indicate an error } }
int main () {
int stack1[MAXSIZE]; // array in which stack will live
int top1 = -1; // top valid location in stack, -1 == empty int stack2[MAXSIZE]; // array in which stack will live int top2 = -1; // top valid location in stack, -1 == empty
printf("pushing: 1, 2, 3, 4, 5 onto first stack\n");