【418】C语言ADT实现Quack(stack+queue)
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~\n"); exit(EXIT_FAILURE); } qs->top = -1; return qs; } void push(int data, Quack qs){ if (qs == NULL){ fprintf(stderr, "push: quack not initialised\n"); } else { if (qs->top >= HEIGHT - 1){ fprintf(stderr, "push: quack overflow\n"); } 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\n"); } else { if (qs->top >= HEIGHT - 1) { fprintf(stderr, "qush: quack overflow\n"); } 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\n"); } else { if (isEmptyQuack(qs)){ fprintf(stderr, "pop: quack underflow\n"); } else { retval = qs->array[qs->top]; --qs->top; } } return retval; } void makeEmptyQuack(Quack qs){ if (qs == NULL){ fprintf(stderr, "makeEmptyQuack: quack not initialised\n"); } 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\n"); } else { empty = qs->top < 0; } return empty; } void showQuack(Quack qs) { if (qs == NULL){ fprintf(stderr, "showQuack: quack not initialised\n"); } else { printf("Quack: "); if (qs->top < 0) { printf("<< >>\n"); } else { int i; printf("<<"); for (i = qs->top; i > 0; i--){ printf("%d, ", qs->array[i]); } printf("%d>>\n", 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\n", pop(s)); printf("pop from s produces %d\n", pop(s)); qush(1, q); qush(2, q); printf("pop from q produces %d\n", pop(q)); printf("pop from q produces %d\n", pop(q)); // printf("\n----------------------------------\n\n"); push(1, s); push(2, s); printf("pop from s produces %d\n", pop(s)); printf("pop from s produces %d\n", pop(s)); qush(1, q); qush(2, q); printf("pop from q produces %d\n", pop(q)); printf("pop from q produces %d\n", pop(q)); // printf("\n----------------------------------\n"); printf("\nstack example\n\n"); 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("\nqueue example\n\n"); 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: << >>