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: << >>

【418】C语言ADT实现Quack(stack+queue)的更多相关文章

  1. STL容器适配器 stack, queue

    stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...

  2. STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map

      list vector deque stack queue priority_queue set [unordered_set] map [unordered_map] multimap [uno ...

  3. Stack&&Queue

    特殊的容器:容器适配器 stack     queue     priority_queue:vector+堆算法---->优先级队列 stack:     1.栈的概念:特殊的线性结构,只允许 ...

  4. 数据结构设计 Stack Queue

    之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...

  5. programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation

    编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...

  6. js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process

    js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process ...

  7. Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector

    Collection List 在Collection的基础上引入了有序的概念,位置精确:允许相同元素.在列表上迭代通常优于索引遍历.特殊的ListIterator迭代器允许元素插入.替换,双向访问, ...

  8. 第11天 Stack Queue

    1.Stack package algs4; import java.util.Iterator; import java.util.NoSuchElementException; public cl ...

  9. 特殊集合 Stack Queue Hashtable

    //Stack    干草堆集合    栈集合      先进后出 Stack st = new Stack(); //实例化 初始化 st.Push(2); //添加元素 st.Push(6); s ...

随机推荐

  1. Thrift使用入门---RPC服务

    https://blog.csdn.net/zkp_java/article/details/81879577 RPC基本原理 大部分的RPC框架都遵循如下三个开发步骤: RPC通信过程如下图所示 通 ...

  2. 【经典/基础BFS+略微复杂的题意】PAT-L3-004. 肿瘤诊断

    L3-004. 肿瘤诊断 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是 ...

  3. Swap Without Extra Variable

    Given two variables, x and y, swap two variables without using a third variable.   Example Given x = ...

  4. easyUI--入门实例

    ui框架 1.需要导入的所有jar包,以及外部的类或文件 1.1导入jar包 1.2导入WebContent外部资源 1.3导入所有需要的辅助类--Util包 2.实例代码 2.1创建TreeNode ...

  5. HTML 文字前面怎么加空格

    HTML  写文字开头需要用空格时  就需要在文字前面     使用时两个 为一个字的距离 使用后

  6. Greenplum 调优--数据倾斜排查(二)

    上次有个朋友咨询我一个GP数据倾斜的问题,他说查看gp_toolkit.gp_skew_coefficients表时花费了20-30分钟左右才出来结果,后来指导他分析原因并给出其他方案来查看数据倾斜. ...

  7. 上传1T文件

    一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不能这样上传了.我这里分享一下我自己开发的一套大文件上传控件 ...

  8. [CERC2015]Juice Junctions(边双连通+字符串hash)

    做法 考虑边数限制的特殊条件,显然答案仅有\(\{0,1,2,3\}\) 0:不联通 1:连通 2:边双连通 3:任意删掉一条边都为边双连通 考虑每次删边后记录各点的边双染色情况来特判\(3\):是否 ...

  9. CSS绘制三角形—border法

    1. 实现一个简单的三角形 使用CSS盒模型中的border(边框)即可实现如下所示的三角形:   CSS实现简单三角形 实现原理: 首先来看在为元素添加border时,border的样子:假设有如下 ...

  10. meshing-三棱锥结构化网格

    原视频下载地址: https://yunpan.cn/cqcq2gE6Iy2P8  访问密码 7d5a ​