【420】链表实现Quack
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的更多相关文章
- linux内核链表的实现
.\linux-2.6.22.6_vscode\include\linux\list.h #ifndef _LINUX_LIST_H#define _LINUX_LIST_H #ifdef __KER ...
- c语言实现链表增、删、改、查及文件读写 && 链表实现程序
一.链表实现增删改查 1.链表定义 1 #include<stdio.h> 2 #include<string.h> 3 #include<windows.h> 4 ...
- 皓远的第二次博客作业(最新pta集,链表练习及期中考试总结)
前言: 知识点运用:正则表达式,有关图形设计计算的表达式和算法,链表的相关知识,Java类的基础运用,继承.容器与多态. 题量:相较于上次作业,这几周在java方面的练习花了更多的精力和时间,所要完成 ...
- Redis链表实现
链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- 排序算法----基数排序(RadixSort(L))单链表智能版本
转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...
- 防御性编程习惯:求出链表中倒数第 m 个结点的值及其思想的总结
防御性编程习惯 程序员在编写代码的时候,预料有可能出现问题的地方或者点,然后为这些隐患提前制定预防方案或者措施,比如数据库发生异常之后的回滚,打开某些资源之前,判断图片是否存在,网络断开之后的重连次数 ...
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- C语言之链表list
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h& ...
随机推荐
- G1垃圾收集器官方文档透彻解读【官方解读】
在前几次中已经对G1的理论进行了一个比较详细的了解了,对于G1垃圾收集器最权威的解读肯定得上官网,当咱们将官网的理解透了,那基本上网上对于G1的说明其实最终都是来自于官网,所以接下来会详细来解读Ora ...
- python学习之flask接口开发,环境变量扩展,网络编程requests
python基础 flask之mock接口 所谓mock接口,其实就是我们在正式接口还没联调或者是测试接口没有正式使用时,自己创建一个模拟接口,来供项目暂时打通功能或者测试流程梳理的桥梁,而我们这儿使 ...
- canvas圆形进度条(逆时针)
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- win10 水晶报表安装包
windows 10 64 VS2013安装 CR For VS 13_0_18 安装过程没有报错 安装成功http://downloads.businessobjects.com/akdlm/cr4 ...
- JS正则表达式完整教程
JS正则表达式完整教程(略长) 引言 亲爱的读者朋友,如果你点开了这篇文章,说明你对正则很感兴趣. 想必你也了解正则的重要性,在我看来正则表达式是衡量程序员水平的一个侧面标准. 关于正则表达式的教程, ...
- Sql Server (MSSQLSERVER) 服务无法启动
北京的冬天特别干燥,大清早的一同事就和服务器擦出了爱的火花,结果没想到竟导致服务器无法开机了,这可尴尬了,代码可都在服务器上托管着呢,一会还等着上线呢,必须得修啊.他们说是主板坏了,就另外找了一台电脑 ...
- pip 安装包 不行 自己下载whl 包自己安装
https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
- [GraphQL] Query Lists of Multiple Types using a Union in GraphQL
Unions are used when we want a GraphQL field or list to handle multiple types of data. With a Union ...
- bootstrap富文本编辑
先把设定富文本框架 <div class="form-group"> <label class="col-sm-2 control-label" ...
- 二十六. 集群及LVS简介 LVS-NAT集群 LVS-DR集群
方案:安装ipvsadm软件包,关于ipvsadm的用法可以参考man ipvsadm资料. 常用ipvsadm命令语法格式如表-1及表-2所示. 1.ipvsadm命令用法(proxy) 1.1 创 ...