Lecture 4
Lecture 4
#include <stdio.h>
#include <stdlib.h> // Check if the stack is empty
#define MAX 10 int isempty(st *s) {
int count = 0; if (s->top == -1)
// Creating a stack return 1;
struct stack { else
int items[MAX]; return 0;
int top; }
};
typedef struct stack st; // Add elements into stack
void createEmptyStack(st *s) { void push(st *s, int newitem) {
s->top = -1; if (isfull(s)) {
} printf("STACK FULL");
// Check if the stack is full } else {
int isfull(st *s) { s->top++;
if (s->top == MAX - 1) s->items[s->top] = newitem;
return 1; }
else count++;
return 0; }
}
Example
⚫ To reverse a word - Put all the letters in a stack and pop them
out. Because of the LIFO order of stack, you will get the letters in
reverse order.