【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 ...
随机推荐
- linux网络编程之socket编程(八)
学习socket编程继续,今天要学习的内容如下: 先来简单介绍一下这五种模型分别是哪些,偏理论,有个大致的印象就成,做个对比,因为最终只会研究一个I/O模型,也是经常会用到的, 阻塞I/O: 先用一个 ...
- machine learning(8) -- classification
分类预测不能使用linear regression, linear regression算法对于分类预测效果很差,应使用logistic regression算法 Logistic regressti ...
- Pthon操作Gitlab API----批量删除,创建,取消保护
1.需求:大批量的应用上线后合并到Master,其他的分支develop/test/uat等需要同步最新代码的操作. 2.操作:可以通过传参 ,列表 的方式把每个项目的id值填入,才能对相关项目进行批 ...
- 2G以上的大文件如何上传
无法上传大文件是因为php.ini配置有限制了,这样限制了用户默认最大为2MB了,超过了就不能上传了,如果你确实要上传我们可以按下面方法来处理一下. 打开php.ini, 参数 设置 说明 fil ...
- luogu 1144
最短路计数 #include <bits/stdc++.h> using namespace std; , M = 2e6 + ; << ); #define gc getch ...
- bzoj 3498
统计三元环 很多代码在bzoj都T诶 #include <iostream> #include <cstdio> #include <algorithm> #inc ...
- python 元组 【基本使用功能】
元组是括号,列表是方括号,都可以通用的有好多,比如判断一个元素是否存在可以直接用 in ,复制或者合并可以直接用乘或者加. 下面是在菜鸟教程截得的: 示例: #!/usr/bin/python # - ...
- 比Excel还简单的SQL语句查询
大家好,我是jacky朱元禄,很高兴继续跟大家分享<MySQL数据分析实战>系列课程,前面的课程jacky分享了数据层面增删改查中的增删改,下面的课程我们要说增删改查的这个查,jacky说 ...
- vue+elementui 开发的网站IE浏览器加载白屏(不兼容)解决办法
1.需要检查一下 export default { name: 'aa',-------vue的name是不可以重复的-----这个是决定性原因 data() { return {} } 2.变量声明 ...
- SDN与IXP
IXP 互联网交换中心(IXP)在互联网生态系统中发挥着关键作用.在全球范围内,100多个国家/地区有超过400个IXP,其中最大的IXP具有接近10 Tbps的峰值数据速率并连接数百个网络.IXP提 ...