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. Eclipse安装zookeeper监控插件

    1.在 Eclipse 菜单打开Help -> Install New Software…2.添加 url http://www.massedynamic.org/eclipse/updates ...

  2. 关于zsh-autosuggestions插件导致粘贴内容很慢的问题

    zsh开启autosuggestions 插件的时候,在终端中粘贴大量的内容的时候,会粘贴的很慢,基本上是一个字符一个字符的粘贴的. 解决方案 在.zshrc文件中配置以下内容: # This spe ...

  3. IntentService使用

    说实话,对于这个类在我实际工作中并没有用到过,通常也只是用了它的父类Service,通过官方文档可以看出类的层次结构: 而在今年的一次面试当中,有个面试官提起了它,所以虽说目前还没有真实在项目中用它, ...

  4. python中set(集合),深浅拷贝以及一些补充知识点

    1.set集合 特点:无序,不重复,元素必须可哈希(不可变) 作用:去重复 本身是可变的数据类型.有增删改查操作.{集合的增删改查操作应用较少,这里不做详细介绍了(这里的增有一个方法update注意这 ...

  5. unable to access 'https://github.com/shixianqing/img.git/': SSL connect error 解决办法

    解决在linux环境下,git clone 项目,走https协议时,报SSL connect error 错误 升级nss yum update -y nss curl libcurl

  6. python算法与数据结构-常用查找算法一(37)

    一.什么是查找 查找(Searching)就是根据给定的某个值,在查找表中确定一个其关键字等于给定值的数据元素(或记录). 查找表(Search Table):由同一类型的数据元素(或记录)构成的集合 ...

  7. feign.RetryableException: Read timed out executing xxx

    feign.RetryableException: Read timed out executing GET http://common-item/service/item/selectTbItemA ...

  8. javascript权威指南第14章 表单脚本示例代码

    HTML部分 <!DOCTYPE html> <html> <head> <title></title> </head> < ...

  9. 将Eclipse,MyEclipse等编辑器的项目管理框颜色改为护眼豆沙绿的方法

    转载链接:https://blog.csdn.net/caibaoH/article/details/77005977

  10. 2019 ICPC Asia Xuzhou Regional

    目录 Contest Info Solutions A. Cat B. Cats line up C. <3 numbers E. Multiply F. The Answer to the U ...