quack.h
#include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct node *Quack; Quack createQuack(void); void push(int data, Quack qs); void qush(int data, Quack qs); int pop(Quack qs); void makeEmptyQuack(Quack qs); int isEmptyQuack(Quack qs); void showQuack(Quack qs);
quack.c
#include "quack.h" #define HEIGHT 1000 struct node{ int array[HEIGHT]; int top; }; Quack createQuack(void){ Quack qs; qs = malloc(sizeof(struct node)); if (qs == NULL){ fprintf(stderr, "Out of memory~ "); exit(EXIT_FAILURE); } qs->top = -1; return qs; } void push(int data, Quack qs){ if (qs == NULL){ fprintf(stderr, "push: quack not initialised "); } else { if (qs->top >= HEIGHT - 1){ fprintf(stderr, "push: quack overflow "); } else { ++qs->top; qs->array[qs->top] = data; } } return; } //used as queue, push element from bottom void qush(int data, Quack qs){ if (qs == NULL){ fprintf(stderr, "qush: quack not initialised "); } else { if (qs->top >= HEIGHT - 1) { fprintf(stderr, "qush: quack overflow "); } else { for (int i = qs->top + 1; i > 0; i--) { qs->array[i] = qs->array[i-1]; } qs->array[0] = data; qs->top++; } } return; } int pop(Quack qs){ int retval = 0; if (qs == NULL){ fprintf(stderr, "pop: quack not initialised "); } else { if (isEmptyQuack(qs)){ fprintf(stderr, "pop: quack underflow "); } else { retval = qs->array[qs->top]; --qs->top; } } return retval; } void makeEmptyQuack(Quack qs){ if (qs == NULL){ fprintf(stderr, "makeEmptyQuack: quack not initialised "); } else { while (!isEmptyQuack(qs)) { pop(qs); } } return; } int isEmptyQuack(Quack qs) { // 0 means not empty int empty = 0; if (qs == NULL){ fprintf(stderr, "isEmptyQuack: quack not initialised "); } else { empty = qs->top < 0; } return empty; } void showQuack(Quack qs) { if (qs == NULL){ fprintf(stderr, "showQuack: quack not initialised "); } else { printf("Quack: "); if (qs->top < 0) { printf("<< >> "); } else { int i; printf("<<"); for (i = qs->top; i > 0; i--){ printf("%d, ", qs->array[i]); } printf("%d>> ", qs->array[0]); } } return; }
separateQuack.c
// separateQuack.c: have both a stack and a queue in the same program #include <stdio.h> #include "quack.h" int main(void) { Quack s = NULL; Quack q = NULL; s = createQuack(); q = createQuack(); push(1, s); push(2, s); printf("pop from s produces %d ", pop(s)); printf("pop from s produces %d ", pop(s)); qush(1, q); qush(2, q); printf("pop from q produces %d ", pop(q)); printf("pop from q produces %d ", pop(q)); // printf(" ---------------------------------- "); push(1, s); push(2, s); printf("pop from s produces %d ", pop(s)); printf("pop from s produces %d ", pop(s)); qush(1, q); qush(2, q); printf("pop from q produces %d ", pop(q)); printf("pop from q produces %d ", pop(q)); // printf(" ---------------------------------- "); printf(" stack example "); for (int i = 0; i < 4; i++) { printf("push: %d -- ", i+1); push(i+1, s); showQuack(s); } for (int i = 0; i < 4; i++) { printf("pop: %d --- ", pop(s)); showQuack(s); } printf(" queue example "); for (int i = 0; i < 4; i++) { printf("qush: %d -- ", i+1); qush(i+1, s); showQuack(s); } for (int i = 0; i < 4; i++) { printf("pop: %d --- ", pop(s)); showQuack(s); } return EXIT_SUCCESS; }
Run in terminal
gcc quack.c separateQuack.c && ./a.out
output:
pop from s produces 2 pop from s produces 1 pop from q produces 1 pop from q produces 2 ---------------------------------- pop from s produces 2 pop from s produces 1 pop from q produces 1 pop from q produces 2 ---------------------------------- stack example push: 1 -- Quack: <<1>> push: 2 -- Quack: <<2, 1>> push: 3 -- Quack: <<3, 2, 1>> push: 4 -- Quack: <<4, 3, 2, 1>> pop: 4 --- Quack: <<3, 2, 1>> pop: 3 --- Quack: <<2, 1>> pop: 2 --- Quack: <<1>> pop: 1 --- Quack: << >> queue example qush: 1 -- Quack: <<1>> qush: 2 -- Quack: <<1, 2>> qush: 3 -- Quack: <<1, 2, 3>> qush: 4 -- Quack: <<1, 2, 3, 4>> pop: 1 --- Quack: <<2, 3, 4>> pop: 2 --- Quack: <<3, 4>> pop: 3 --- Quack: <<4>> pop: 4 --- Quack: << >>