#include <stdio.h> #include "Stack.h" int main(void) { Stack myStack; // uninitialized Stack int x; // variable to hold inputs printf("Hello\n"); initStack(&myStack); while (scanf("%d",&x) == 1) { if (x < 0) break; if (!pushStack(&myStack, x)) break; showStack(myStack); } printf("Now we pop ...\n"); while (!isEmptyStack(myStack)) { printf("%d\n", popStack(&myStack)); } return 0; }
#include "Stack.h" #include <stdio.h> void initStack(Stack *s) { s->top = 0; // (*s).top = 0; } int pushStack(Stack *s, Item val) { if (s->top == MAX_STACK) return 0; s->items[s->top] = val; s->top++; return 1; } Item popStack(Stack *s) { if (s->top == 0) return -1; s->top--; return s->items[s->top]; } int isEmptyStack(Stack s) { if (s.top == 0) return 1; else return 0; // return (s.top == 0) ? 1 : 0; // return (s.top == 0); } void showStack(Stack s) { printf("Base "); for (int i = 0; i < s.top; i++) { printf("%d ", s.items[i]); } printf("Top\n"); }
// Interface to Stack data type #define MAX_STACK 5 typedef int Item; typedef struct _stack { int top; Item items[MAX_STACK]; } Stack; void initStack(Stack *s); int pushStack(Stack *s, Item val); Item popStack(Stack *s); int isEmptyStack(Stack s); void showStack(Stack s);
CC=gcc CFLAGS=-Wall -Werror -std=c99 main : main.o Stack.o main.o : main.c Stack.h Stack.o : Stack.c Stack.h clean : rm -f main *.o core
Stack client