【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: << >>
【418】C语言ADT实现Quack(stack+queue)的更多相关文章
- STL容器适配器 stack, queue
stack是一种后进先出(last in first out)的数据结构.它只有一个出口,如图所示.stack允许新增元素,删除元素,取得最顶端元素.但除了最顶端外,没有其他任何地方可以存储stack ...
- 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 ...
- Stack&&Queue
特殊的容器:容器适配器 stack queue priority_queue:vector+堆算法---->优先级队列 stack: 1.栈的概念:特殊的线性结构,只允许 ...
- 数据结构设计 Stack Queue
之前在简书上初步总结过几个有关栈和队列的数据结构设计的题目.http://www.jianshu.com/p/d43f93661631 1.线性数据结构 Array Stack Queue Hash ...
- programming review (c++): (1)vector, linked list, stack, queue, map, string, bit manipulation
编程题常用知识点的review. most important: 想好(1)详尽步骤(2)边界特例,再开始写代码. I.vector #include <iostream> //0.头文件 ...
- 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 ...
- Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector
Collection List 在Collection的基础上引入了有序的概念,位置精确:允许相同元素.在列表上迭代通常优于索引遍历.特殊的ListIterator迭代器允许元素插入.替换,双向访问, ...
- 第11天 Stack Queue
1.Stack package algs4; import java.util.Iterator; import java.util.NoSuchElementException; public cl ...
- 特殊集合 Stack Queue Hashtable
//Stack 干草堆集合 栈集合 先进后出 Stack st = new Stack(); //实例化 初始化 st.Push(2); //添加元素 st.Push(6); s ...
随机推荐
- mysql查询某一列的数据最大字节
语法:select 列名, length(列名) from 表名where length(列名) = ( select max(length(列名)) from 表名); 实例:select proj ...
- JDK源码那些事儿之LinkedBlockingQueue
今天继续讲解阻塞队列,涉及到了常用线程池的其中一个队列LinkedBlockingQueue,从类命名部分我们就可以看出其用意,队列中很多方法名是通用的,只是每个队列内部实现不同,毕竟实现的都是同一个 ...
- Robot Framework--连接Mysql数据库
1.安装Database-Library 输入命令:pip install robotframework_databaselibrary 2.添加Database的Library 3.实例 *** T ...
- AJAX学习笔记——同源策略
同源策略 同源策略,所有浏览器都实行这个政策 最初,它的含义是指,A 网页设置的 Cookie,B 网页不能打开,除非这两个网页"同源".所谓"同源"指的是&q ...
- The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners
链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word ...
- 关于 sublime 的插件 AdvancedNewFile 新建文件/文件夹 插件
新建文件的插件: 快捷键:Ctrl + N 路径:当前目录下进行创建:js/index.js 表示在当前js目录下面创建index.js box 表示直接在当前目录下面创建一个bo ...
- icpc 银川 H. Delivery Route SPFA优化
Problem Description Pony is the boss of a courier company. The company needs to deliver packages to ...
- MongoDB 4.0 事务实现解析
MongoDB 4.0 引入的事务功能,支持多文档ACID特性,例如使用 mongo shell 进行事务操作 > s = db.getMongo().startSession() sessio ...
- mongodb 开发规范
一.命名规则 1.数据库命名规则 数据库名可以是满足以下条件的任意UTF-8字符串: (1)不能是空字符串(”") : (2)不能含有”(空格)...$./..和(空字符): (3)应全部小 ...
- am335x system upgrade rootfs custom service using systemd script(十七)
1 Scope of Document systemd 是一个 Linux 系统基础组件的集合,提供了一个系统和服务管理器,运行为 PID 1 并负责启动其它程序.功能包括:支持并行化任务: ...