博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【418】C语言ADT实现Quack(stack+queue)
阅读量:7114 次
发布时间:2019-06-28

本文共 4126 字,大约阅读时间需要 13 分钟。

 quack.h 

#include 
#include
#include
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 1000struct 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 bottomvoid 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 
#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 2pop from s produces 1pop from q produces 1pop from q produces 2----------------------------------pop from s produces 2pop from s produces 1pop from q produces 1pop from q produces 2----------------------------------stack examplepush: 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 exampleqush: 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: << >>

 

转载于:https://www.cnblogs.com/alex-bn-lee/p/11100626.html

你可能感兴趣的文章
安全狗
查看>>
tomcat安装配置
查看>>
大型网站系统架构演化之路
查看>>
强大的数据库查询工具Database.NET 9.4.5018.42
查看>>
DB2 IBM InfoSphere Data Replication Center - SQL Replication[翻译]
查看>>
CentOS时间同步
查看>>
PHP防XSS 防SQL注入的代码
查看>>
MongoDB权威指南——管理
查看>>
ActiveMQ持久化方式
查看>>
手动(批量)添加nginx虚拟主机和rewrite规则
查看>>
安装JBOSS EAP 6 Standalone模式
查看>>
手动修改IP和MAC地址
查看>>
(20)Powershell中的特殊运算符
查看>>
IIS 7.0 六大新特性
查看>>
32. mac上传下载文件到远程服务器scp
查看>>
阿里云数据库2-3月刊:阿里云峰会云数据库四大发布
查看>>
像Google一样构建机器学习系统 - 利用MPIJob运行ResNet101
查看>>
Django book 笔记---Form表单
查看>>
为什么我不同意建房子
查看>>
使用webpack实现jquery按需加载
查看>>