【418】C语言ADT实现Quack(stack+queue)
quack.h
1 2 3 4 5 6 7 8 9 10 11 12 13 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | // 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
1 | gcc quack.c separateQuack.c && . /a .out |
output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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: << >> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2016-06-28 【207】WinForm Chart类
2012-06-28 【054】◀▶ JavaScript 语法参考