0% found this document useful (0 votes)
2 views2 pages

Maxsize Bool Int Int: Isempty

Uploaded by

azizatemirovaa
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
2 views2 pages

Maxsize Bool Int Int: Isempty

Uploaded by

azizatemirovaa
Copyright
© © All Rights Reserved
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");


printf("pushing: 100, 200, 300, 400, 500 onto second
stack\n\n");
push(1, stack1, &top1);
push(2, stack1, &top1);
push(3, stack1, &top1);
push(4, stack1, &top1);
push(5, stack1, &top1);
push(100, stack2, &top2);
push(200, stack2, &top2);
push(300, stack2, &top2);
push(400, stack2, &top2);
push(500, stack2, &top2);

printf("popping alternating stacks:\n");


printf("1> %d\n",pop(stack1, &top1));
printf("2> %d\n",pop(stack2, &top2));
printf("1> %d\n",pop(stack1, &top1));
printf("2> %d\n",pop(stack2, &top2));
printf("1> %d\n",pop(stack1, &top1));
printf("2> %d\n",pop(stack2, &top2));
printf("1> %d\n",pop(stack1, &top1));
printf("2> %d\n",pop(stack2, &top2));
printf("1> %d\n",pop(stack1, &top1));
printf("2> %d\n",pop(stack2, &top2));
printf("1> %d\n",pop(stack1, &top1));
printf("2> %d\n",pop(stack2, &top2));
return 0;
}

You might also like