【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 ...
随机推荐
- Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)
http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...
- 301、404、200、304等HTTP状态
在网站建设的实际应用中,容易出现很多小小的失误,就像mysql当初优化不到位,影响整体网站的浏览效果一样,其实,网站的常规http状态码的表现也是一样,Google无法验证网站几种解决办法,提及到由于 ...
- 云计算(3)-what is new in today's Cloud
What is new in today's Cloud Four features new in today's Clouds 如果一个problem有以上4个features里面的一个或者多个,则 ...
- XSLT格式
XSL 指扩展样式表语言(EXtensible Stylesheet Language). XSL - 不仅仅是样式表语言,包括三部分: XSLT :一种用于转换 XML 文档的语言. XPath : ...
- tomcat配置虛擬路徑
1.server.xml设置 打开Tomcat安装目录,在server.xml中<Host>标签中,增加<Context docBase="硬盘目录" path= ...
- Scrapy框架的八个扩展
一.proxies代理 首先需要在环境变量中设置 from scrapy.contrib.downloadermiddleware.httpproxy import HttpProxyMiddlewa ...
- 判断字符串是否是IP地址
#include <stdio.h>#include <string.h> bool isIP(const char* str); int main(){ char str[] ...
- myeclipse2018大括号之前会自动加空格
- 25 | MySQL是怎么保证高可用的?
在上一篇文章中,我和你介绍了binlog的基本内容,在一个主备关系中,每个备库接收主库的binlog并执行. 正常情况下,只要主库执行更新生成的所有binlog,都可以传到备库并被正确地执行,备库就能 ...
- 使用Ajax和一般处理程序实现文件上传与下载
1.使用HTML的input标签 <input type="file" multiple="multiple" id="file_load&qu ...