quack.h

// quack.h: an interface definition for a queue/stack
#include <stdio.h>
#include <stdlib.h> typedef struct node *Quack; Quack createQuack(void); // create and return Quack
void push(int, Quack); // put the given integer onto the top of the quack
void qush(int, Quack); // put the given integer onto the bottom of the quack
int pop(Quack); // pop and return the top element on the quack
int isEmptyQuack(Quack); // return 1 is Quack is empty, else 0
void makeEmptyQuack(Quack);// remove all the elements on Quack
void showQuack(Quack); // print the contents of Quack, from the top down

quackLL.c

// quackLL.c: a linked-list-based implementation of a quack
#include "quack.h"
#include <limits.h> // INT_MAX means largest int
// if using this value, you need to include <limits.h>
#define HEAD_DATA INT_MAX // dummy data struct node {
int data;
struct node *next;
}; // create a null head
Quack createQuack(void) {
// Actually this head is NULL, it doesn't store
// meaningful data, only store the address.
Quack head;
head = malloc(sizeof(struct node));
if (head == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
} head->data = HEAD_DATA;
head->next = NULL; // return the address of head
return head;
} // push a new node between head and 1st node
void push(int data, Quack qs) {
Quack newnode; // determine whether this quack is NULL or not
if (qs == NULL) {
fprintf(stderr, "push: quack not initialised\n");
}
else {
newnode = malloc(sizeof(struct node));
if (newnode == NULL) {
fprintf(stderr, "push: no memory, aborting\n");
exit(1);
} newnode->data = data;
newnode->next = qs->next;
qs->next = newnode;
}
return;
} // pop the 1st node
int pop(Quack qs) {
// store data in variable retval
int retval = 0; if (qs == NULL) {
fprintf(stderr, "pop: quack not initialised\n");
}
else {
// if qs is empty, you can't pop any nodes
if (isEmptyQuack(qs)) {
fprintf(stderr, "pop: quack underflow\n");
}
else {
Quack pop_node = qs->next;
retval = pop_node->data; // link head with 2nd node
qs->next = qs->next->next;
free(pop_node);
pop_node = NULL;
}
}
return retval;
} // pop all nodes in qs
void makeEmptyQuack(Quack qs) {
if (qs == NULL) {
fprintf(stderr, "makeEmptyQuack: quack not initialised\n");
}
else {
while (!isEmptyQuack) {
pop(qs);
}
}
return;
} // only head, head link to NULL
int isEmptyQuack(Quack qs) {
if (qs == NULL) {
fprintf(stderr, "makeEmptyQuack: quack not initialised\n");
}
else {
if (qs->next == NULL) {
return 1;
}
}
return 0;
} // print all nodes
void showQuack(Quack qs) {
if (qs == NULL) {
fprintf(stderr, "makeEmptyQuack: quack not initialised\n");
}
else {
// ??? maybe head is corrupted by opertions
if (qs->data != HEAD_DATA) {
fprintf(stderr, "showQuack: linked list head corrupted\n");
}
else {
printf("Quack: ");
if (qs->next == NULL) {
printf("<< >>\n");
}
else {
printf("<<");
Quack node = qs->next;
while (node->next != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("%d ", node->data);
printf(">>\n");
}
}
}
return;
}

clientQuack.c

#include "quack.h"

int main() {
Quack qs = createQuack();
push(1, qs);
showQuack(qs);
push(2, qs);
showQuack(qs);
push(3, qs);
showQuack(qs);
push(4, qs);
showQuack(qs); pop(qs);
showQuack(qs);
pop(qs);
showQuack(qs);
pop(qs);
showQuack(qs);
pop(qs);
showQuack(qs); }

运行下面命令实现编译

alex@ubuntu:Day01$ gcc quackLL.c clientQuack.c && ./a.out
Quack: <<1 >>
Quack: <<2 1 >>
Quack: <<3 2 1 >>
Quack: <<4 3 2 1 >>
Quack: <<3 2 1 >>
Quack: <<2 1 >>
Quack: <<1 >>
Quack: << >>

【420】链表实现Quack的更多相关文章

  1. linux内核链表的实现

    .\linux-2.6.22.6_vscode\include\linux\list.h #ifndef _LINUX_LIST_H#define _LINUX_LIST_H #ifdef __KER ...

  2. c语言实现链表增、删、改、查及文件读写 && 链表实现程序

    一.链表实现增删改查 1.链表定义 1 #include<stdio.h> 2 #include<string.h> 3 #include<windows.h> 4 ...

  3. 皓远的第二次博客作业(最新pta集,链表练习及期中考试总结)

    前言: 知识点运用:正则表达式,有关图形设计计算的表达式和算法,链表的相关知识,Java类的基础运用,继承.容器与多态. 题量:相较于上次作业,这几周在java方面的练习花了更多的精力和时间,所要完成 ...

  4. Redis链表实现

    链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...

  5. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  6. 排序算法----基数排序(RadixSort(L))单链表智能版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  7. 防御性编程习惯:求出链表中倒数第 m 个结点的值及其思想的总结

    防御性编程习惯 程序员在编写代码的时候,预料有可能出现问题的地方或者点,然后为这些隐患提前制定预防方案或者措施,比如数据库发生异常之后的回滚,打开某些资源之前,判断图片是否存在,网络断开之后的重连次数 ...

  8. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  9. C语言之链表list

    #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h& ...

随机推荐

  1. Win8.1 Anaconda下安装第三方库,以jieba wordcloud为例

    最近在看情感分析的东西,于是在spyder里import jieba,and then就报错了. 百度之后,发现jieba是一个第三方库,它并不存在于anaconda内置的packages中.所以在用 ...

  2. Gym - 102346D Denouncing Mafia 取k叶子节点使叶子到根覆盖节点数最大

    给你一棵树 你可以取K条链 一条链为根到叶子的路径 问你K条链最多覆盖树上多少个节点 贪心的做 肯定是每次取最长链 但是取完最长链 一颗树就会变为若干个森林 我们要维护当前所有森林里的最长链 ans数 ...

  3. 利用random模块做一个抢红包功能

    我们都知道random模块是一个生成随机数的模块,用它来做抢红包的功能很合适. 抢红包,抢到的金额是随机的,但怎么让每个人抢到的随机金额公平合理呢 比如:我想发一个100元的红包让10个人抢,我可以把 ...

  4. Session&Cookie&localStorage浅谈

    Session&Cookie&localStorage 领导让我开发一个有两张信息表单需要提交页面的网站,我作为一名开发人员,这个需求太简单了,和领导说直接存session sessi ...

  5. 0020SpringBoot使用SpringCloud中的eureka实现远程调用

    要实现远程调用,主要需要三个module:一个注册中心.一个服务提供者.一个服务消费者,然后进行各自的配置和编码,详细内容如下: 1.建一个空的project,创建3各module a.注册中心模块 ...

  6. 2.3 vue配置(上)

    rm,在打包之前把上一次打包之后的东西删掉,然后webpack重新打包 通过DefinePlugin形成一个环境变量 HTML打包插件

  7. TinyMCE 工具栏配置

    plugins: { type: [String, Array], default: 'lists image media wordcount advlist bbcode code charmap ...

  8. LightOJ - 1116-Ekka Dokka(思维)

    链接: https://vjudge.net/problem/LightOJ-1116 题意: Ekka and his friend Dokka decided to buy a cake. The ...

  9. SublimeCodeIntel 所有代码提示和补全插件 All Autocomplete 插件搜索所有打开的文件来寻找匹配的提示词

    SublimeCodeIntelSublimeCodeIntel 作为一个代码提示和补全插件,支持 JavaScript.Mason.XBL.XUL.RHTML.SCSS.Python.HTML.Ru ...

  10. HTML 003 元素

    HTML 元素 HTML 文档由 HTML 元素定义. HTML 元素 开始标签 * 元素内容 结束标签 * <p> 这是一个段落 </p> <a href=" ...