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. 通过 cross apply 实现函数转换成表并与原表进行关联

    create table tb_cross_apply ( id int identity, multivalue ) ) insert into tb_cross_apply VALUES ('A| ...

  2. 【转】Senior Data Structure · 浅谈线段树(Segment Tree)

    本文章转自洛谷 原作者: _皎月半洒花 一.简介线段树 ps: _此处以询问区间和为例.实际上线段树可以处理很多符合结合律的操作.(比如说加法,a[1]+a[2]+a[3]+a[4]=(a[1]+a[ ...

  3. Python+request 使用pymysql连接数据库mysql的操作《十》

    使用指南.pymysql支持python2.7同时也支持python3.x.当前我用的是python2.7.所以过断选择了pymysql的使用,这里注意几点.一般我们连接数据库为了安全起见,都会要求按 ...

  4. java中byte的范围计算

    概念:java中用补码表示二进制数,补码的最高位是符号位,最高位为“0”表示正数,最高位为“1”表示负数.正数补码为其本身:负数补码为其绝对值各位取反加1:例如:+21,其二进制表示形式是000101 ...

  5. redis-4.0.14 cluster 配置实战

    1.操作系统配置 切换到root用户修改配置sysctl.conf vim /etc/sysctl.conf # 添加配置: vm.max_map_count= vm.overcommit_memor ...

  6. windows下递归删除指定文件和文件夹

    //删除文件del *.后缀 /s//删除文件夹for /r 目录 %a in (文件夹名\) do @if exist "%a" rd /s/q "%a"

  7. 洛谷P1972 HH的项链【树状数组】

    题目:https://www.luogu.org/problemnew/show/P1972 题意:给定一个长度为n的序列,数字表示珠子的种类.m次查询每次询问给定区间内珠子的种类数. 思路:可以说是 ...

  8. sql的九个常用语句是什么

    一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...

  9. Postgresql 物理备份冷备份 与 热备份

    一.冷备份 将数据库停下来,然后把数据库的PGDATA目录拷贝下来就可以了. PostgreSQL把与数据库实例有关的配置文件和数据文件都放在PGDATA目录下,所以做冷备份很简单. 二.热备份 热备 ...

  10. Jquery tabs

    官网 http://api.jqueryui.com/tabs/ 必须通过了后台验证tab1的信息后才允许进入tab2 var passed=false;          $("#tabs ...