ReviewForJob(3)表、栈和队列

【0】表ADT
1)intro:我们把 形如 A1, A2, A3, ..., An 的结构称为表;
2)表的实现: 数组(循环数组) 或 链表 或 双链表 或 循环链表实现;
3)表的插入,删除操作可以在任意位置上进行;

【1】栈(基于表)
1)intro:栈是限制插入和删除只能在一个位置上进行的的表;
2)栈的实现: 数组实现 或 链表实现;
3)栈的应用
应用荔枝1)检验代码的平衡符号:每一个 括号(圆括号,中括号,花括号) 都要一一对应;
Attention)stack.h如下:
#include <stdio.h>
#include <malloc.h>#define ElementType char
#define ERROR(str) printf(str)struct Stack;
typedef struct Stack *Stack;struct Stack
{int size;ElementType* head;int top;
};int isFull(Stack s);
int isEmpty(Stack s);
Stack initStack(int size);
void push(Stack s, ElementType c);
void pop(Stack s, ElementType *e);int isFull(Stack s)
{return s->size == s->top ? 1 : 0;
}int isEmpty(Stack s)
{return 0 == s->top ? 1 : 0;
}Stack initStack(int size)
{Stack s = (Stack)malloc(sizeof(struct Stack));if(s==NULL){ERROR("error: failed initStack() for there is no spare space.\n");}else {s->size = size;s->top = 0;s->head = (ElementType*)malloc(sizeof(ElementType) * size);if(s->head==NULL){ERROR("error: failed initStack() for there is no spare space.\n");return NULL;}}return s;
}void push(Stack s, ElementType c)
{if(!isFull(s)){s->head[s->top++] = c;} else{printf("%s", "failed pushing for stack is full.");}
}void pop(Stack s, ElementType *e)
{if(!isEmpty(s)){*e = s->head[--s->top];}else{*e = ' ';printf("%s", "failed poping for stack is empty.");}	
}
</pre><pre code_snippet_id="1797122" snippet_file_name="blog_20160731_3_2765700" name="code" class="cpp">#include "stack.h"// check whether the grammar defined in given file is correct or not.
int checkFile(Stack s)
{FILE *fp;ElementType c;ElementType popc;fp = fopen("D:\\classical_books\\datastructure_alg\\source_code\\chapter3\\review_for_job\\p52_check_balanced_char\\temp.txt", "r");// only test for round bracket '()', bracket '[]', brace'{}'.// do you know the meanings of open brace '{' and close brace '}'.while((c=getc(fp)) != EOF){if(c == '(' || c == '[' || c == '{'){push(s, c);}else if(c == ')' || c == ']' || c == '}'){pop(s, &popc);if(c==')' && popc!= '('){return 0;}else if(c==']' && popc!= '['){return 0;}else if(c=='}' && popc!= '{'){return 0;}}}return 1;
}int main()
{Stack s;int result;	s = initStack(1000);if(s==NULL){return -1;}result = checkFile(s);printf("%d \n", result);return 0;
}
应用荔枝2)计算后缀表达式的值:step1)将中缀表达式转为 后缀表达式;step2)然后求 后缀表达式的值;
Attention)infix2postfix.h 见应用荔枝3;
#include "infix2postfix.h" void computeExpr(Stack s, ElementType *postfix, int length)
{int i = 0;char c;ElementType num1, num2, result;for(;i<length;i++){c = postfix[i];if(isOperator(c) == -1)// c is an operand.{push(s, c-48);}else if(isOperator(c) != -1) // c is an operator.{pop(s, &num1);pop(s, &num2);			switch(c){case '+': result = num1+num2;break;case '-': result = num1-num2;break;case '*': result = num1*num2;break;case '/': result = num1/num2;break;}push(s, result);}}pop(s, &result);	printf("final computing result is %d \n", result);
}int main()
{Stack s;	ElementType output[255];int length;s = initStack(1000);if(s==NULL){return -1;}length = infix2postfix(s, output); //switch infix into postfix.printChatArray(output, length);// compute postfix expr.free(s);s=initStack(1000);computeExpr(s, output, length);return 0;
}


应用荔枝3)中缀表达式: 中缀转后缀表达式;
stack.h 同上 && infix2postfix.h 如下:
#include "stack.h"
#include "math.h"void printChatArray(ElementType* array, int size);
int isOperator(ElementType c);
int checkPriority(int p1, int p2);
int infix2postfix(Stack s, ElementType *output);// just print array.
void printChatArray(ElementType* array, int size)
{int i=0;for(;i<size; i++){putchar(array[i]);}printf("\n\n");
}// check whether the char is operator or not.
// if true returns priority with array index ,otherwise return -1.
int isOperator(ElementType c)
{ int i = 0;char priorities[] = {'(', ' ', '+','-',' ','*','/'};int size = 7;for(; i<size; i++){if(c==priorities[i]){return i;}}return -1;
} // 0 means p1.priority == p2.priority
// 1                    >
// -1                   <
int checkPriority(int p1, int p2)
{if(p1-p2==1 || p1-p2==-1 || p1-p2==0){return 0;}else if(p1-p2>1){return 1;}else if(p1-p2 < -1){return -1;}
}// transmit infix into postfix.
// attention for operands not being pushed into stack.
int infix2postfix(Stack s, ElementType *output)
{	ElementType c, popc, topc;	int i = 0;int p1, p2;	printf("%s", "input the expr: ");while((c=getchar()) != '\n'){if(c=='(') // when the char is ( or ){push(s, c);}else if(c==')'){while(!isEmpty(s) && (topc=getTop(s))!='('){pop(s, &popc);output[i++] = popc;	}if(topc=='('){pop(s,&popc);}} // when the char is ( or ) over.else if(isOperator(c) == -1) // c is an operand.{output[i++] = c;}else if((p1=isOperator(c)) != -1) // c is an operator.{if(isEmpty(s)) // if the stack is empty.{push(s, c);}else  // if the stack is not empty.{								while(!isEmpty(s)){topc = getTop(s);// after that, check priority between c and topc.p2 = isOperator(topc);if(checkPriority(p1,p2) != 1) // p1.priority <= p2.priority, then pop operand under p2 into output array.{pop(s, &popc);output[i++] = popc;}else{						break;}}	push(s, c);}			}}while(!isEmpty(s)) // pop surplus elements into output array.{pop(s, &popc);output[i++] = popc;	}	return i;
}
#include "infix2postfix.h" int main()
{Stack s;	ElementType output[255];int length;s = initStack(1000);if(s==NULL){return -1;}length = infix2postfix(s, output); //switch infix into postfix.printChatArray(output, length);return 0;
}


【2】队列(基于表)
1)intro:队列是插入在一端,而删除在另一端的表;
2)队列的实现:数组实现 或  循环数组(队列)实现;
3)(循环队列)代码如下:
#include <stdio.h>
#include <malloc.h>#define ElementType int
#define Error(str) printf("\nerror: %s", str)struct Queue;
typedef struct Queue* Queue;// 循环队列的数据结构.
struct Queue
{int capacity;int front;int rear;int size;ElementType* array;
};Queue initQueue(int capacity);
int isFull(Queue queue);
void enQueue(Queue queue, ElementType e);
int isEmpty(Queue queue);
ElementType deQueue(Queue queue);
void printQueue(Queue queue);// init queue wit capacity.
Queue initQueue(int capacity)
{// allocate memory for queue.Queue queue = (Queue)malloc(sizeof(struct Queue));	if(queue==NULL){Error("failed initQueue() for out of space.");return NULL;			}		queue->capacity = capacity;queue->front = 0;queue->rear = 0;queue->size = 0;// allocate memory for queue->array.queue->array = (ElementType*)malloc(capacity * sizeof(ElementType));if(queue->array == NULL){Error("failed initQueue() for out of space.");return NULL;}	return queue;
}// judge whether the queue is full or not.
int isFull(Queue queue)
{return queue->size == queue->capacity ? 1 : 0;
}// 进队列,满时不进.
void enQueue(Queue queue, ElementType e)
{	if(isFull(queue)){Error("failed enQueue() for the queue is full.");return ;}	queue->array[queue->rear++ % queue->capacity] = e;queue->size++;
}// judge whether the queue is empty or not.
int isEmpty(Queue queue)
{return queue->size == 0 ? 1 : 0;
}// 出队列,空时不出.
ElementType deQueue(Queue queue)
{int temp;if(isEmpty(queue)){Error("failed deQueue() for the queue is empty.");return -1;}temp = queue->array[queue->front++ % queue->capacity];queue->size--;return temp;
}// 打印队列
void printQueue(Queue queue)
{int i, index;ElementType* array = queue->array;printf("\n");for(i=0; i<queue->size; i++){index = (queue->front + i) % queue->capacity;printf("%d ", array[index]);}printf("\n");
}// 打印队列所在数组
void printArray(Queue queue)
{int i;printf("\nelements in queue->array from index 0 are as follows: ");for(i=0; i<queue->size; i++){printf("%d ", queue->array[i]);}printf("\n");
}
#include "queue.h"void main()
{ElementType array[] = {3, 4, 6, 1, 2, 0, 10, 8, 9};Queue queue;int capacity=6;int i;queue=initQueue(capacity); // 初始化队列if(queue == NULL){return ;}printf("\nlet {3, 4, 6, 1, 2, 0, 10, 8, 9} enter queue.\n");for(i=0; i<9; i++){enQueue(queue, array[i]); // 让元素进队列}printf("\n\nthe elements in queue are as follows: ");printQueue(queue);// 让元素出队列deQueue(queue);deQueue(queue);deQueue(queue);enQueue(queue, array[6]);enQueue(queue, array[7]);enQueue(queue, array[8]);printf("\n\nafter 3 dequeue operations and enQueue({10,8,9}) ,the elements in queue are as follows: ");printQueue(queue);printArray(queue);
}


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/330986.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2017一季度JAVA面试题锦集

转载自 2017一季度JAVA面试题锦集 1、如何实现分布式事务&#xff0c;你们公司是怎么解决的&#xff1f; 2、HashMap数据结构及实现原理&#xff0c;其链表是用来解决什么问题的 3、可以自定义java.lang.String类吗&#xff0c;说明为什么 4、redis 1&#xff09;有哪几种类型的…

JDK并发包

JDK提供了大量实用的API和框架&#xff0c;来支持JDK内部功能&#xff1a; 介绍更多多线程控制方法&#xff0c;比如之前的synchronized&#xff1b;介绍JDK中对线程池的支持&#xff0c;提高线程调度性能&#xff1b;向大家介绍JDK的一些并发容器。 1 多线程的团队协作&…

ReviewForJob——希尔排序(缩小增量排序)之塞奇威克增量序列

【0】README 0&#xff09;希尔排序是基于插入排序的。将插入排序算法 内for循环中的所有 1 改为增量就可以。。bingo。。 插入排序源码 1&#xff09;本文旨在给出 希尔排序&#xff08;缩小增量排序&#xff09;之塞奇威克增量序列 的源码实现&#xff1b; 2&#xff09;为…

稍微有点难度的10道java面试题,你会几道?

转载自 稍微有点难度的10道java面试题&#xff0c;你会几道&#xff1f; 1、jvm对频繁调用的方法做了哪些优化&#xff1f; 2、常见的攻击手段有哪些&#xff1f;如何防范&#xff1f; 3、restful api有哪些设计原则&#xff1f; 4、hessian是做什么用的&#xff1f;它的…

重新学习Spring一--Spring在web项目中的启动过程

1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系&#xff0c;实现高内聚、低耦合。&#xff08;高内聚&#xff1a;相关性很强的代码组成&#xff0c;既单一责任原则&#xff1b;低耦合&#xff1a;耦合指块间联系&…

ReviewForJob——堆排序

【0】README1&#xff09;本文旨在给出 推排序的源码实现&#xff1b;堆排序是基于二叉树的数组实现的&#xff1b;【1】堆排序步骤step1&#xff09;对排序数据建堆&#xff0c;执行 n 次 insert 操作&#xff08;基于上滤操作&#xff09;&#xff1b;每次 insert 包括 将 新…

重新学习Spring2——IOC和AOP原理彻底搞懂

一、AOP 1 Spring AOP 的实现原理 是对OOP编程方式的一种补充。翻译过来为“面向切面编程”。 1 AspectJ是静态代理的增强&#xff1a;所谓静态代理就是AOP框架会在便一阶段生成AOP代理类&#xff0c;也叫编译器增强。 2 使用Spring AOP 与AspectJ 的静态代理不同&#xff0c…

厉害了,关于String的10道经典面试题

转载自 厉害了&#xff0c;关于String的10道经典面试题 1、String是基本数据类型吗&#xff1f; 2、String是可变的话&#xff1f; 3、怎么比较两个字符串的值一样&#xff0c;怎么比较两个字符串是否同一对象&#xff1f; 4、switch中可以使用String吗&#xff1f; 5、String …

ReviewForJob——快速排序(基于插入排序)+快速选择(快速排序变体)

【0】README 0&#xff09;本文旨在给出 快速排序 的 源码实现和源码分析&#xff08;分析它的坑&#xff09;&#xff1b; 2&#xff09;要知道 在 元素个数小于10的时候&#xff0c;快速排序不如插入排序&#xff1b;注意快速排序选取枢纽元 时 所使用的方法是 三数中值分割…

Spring boot web(2):web综合开发

1 web开发 Spring boot web 开发非常简单&#xff0c;其中包括常用的 json输出、filters、property、log等 1.1 json接口开发 在以前的Spring 开发我么提供json 的做法&#xff1a; 添加jackjson 等相关jar包配置Spring controller扫描对接的方法添加ResponseBody 而在Spri…

10道腾讯的Java面试题

转载自 10道腾讯的Java面试题 下面总结10道面试腾讯的Java面试题。 1、说几种常见的攻击方式及预防手段。 2、http1.x和http2.x的区别。 3、mysql查询语句怎么做性能分析。 4、你知道哪几种排序算法&#xff1f; 5、HashMap和HashTable的区别&#xff0c;并说明其底层实现数据…

ReviewForJob——桶式排序+基数排序(==多次桶式排序)

【0】README 1&#xff09;本文旨在 给出 ReviewForJob——桶式排序基数排序&#xff08;多次桶式排序&#xff09; 的 代码实现和代码分析&#xff1b; 2&#xff09;桶式排序基础参见 http://blog.csdn.net/pacosonswjtu/article/details/49685749&#xff0c; 基数排序基…

Spring boot(3):Spring boot中Redis 的使用

Spring boot除了常用的数据库支持外&#xff0c;对nosql数据库也进行了封装自动化。 1 Redis介绍 Redis 是目前业界使用最广泛的内存数据存储。相比memcached&#xff0c; &#xff08;1&#xff09;Redis支持更丰富的数据结构&#xff0c;例如hashes&#xff0c;lists&#x…

Java List面试题汇总

转载自 Java List面试题汇总 1、你知道的List都有哪些&#xff1f; 2、List和Vector有什么区别&#xff1f; 3、List是有序的吗&#xff1f; 4、ArrayList和LinkedList的区别&#xff1f;分别用在什么场景&#xff1f; 5、ArrayList和LinkedList的底层数据结构是什么&#…

ReviewForJob——拓扑排序+最短路径算法(有权+无权)

【0】README 1&#xff09;本文旨在给出 拓扑排序最短路径算法&#xff08;有权无权&#xff09; 的源码实现 和 分析&#xff0c;内容涉及到 邻接表&#xff0c; 拓扑排序&#xff0c; 循环队列&#xff0c;无权最短路径&#xff08;广度优先搜索&#xff09;&#xff0c;有权…

Spring boot (5):Spring data jpa 的使用

总结&#xff1a; jpa是什么&#xff0c;spring data jpa是什么&#xff1f; jpa是一套规范&#xff0c;不是一套产品。jpa是一套规范&#xff0c;不是一套产品。 spring data jpa是spring基于ORM框架、JPA规范的基础上封装的一套JPA应用框架&#xff0c;提供了包括增删改等在…

Java Map集合面试题汇总

转载自 Java Map集合面试题汇总1、 你都知道哪些常用的Map集合?2、Collection集合接口和Map接口有什么关系&#xff1f;3、HashMap是线程安全的吗&#xff1f;线程安全的Map都有哪些&#xff1f;性能最好的是哪个&#xff1f;4、使用HashMap有什么性能问题吗&#xff1f;5、Ha…

ReviewForJob——二叉堆优先队列的实现(三种堆节点类型——int + struct HeapNode + struct HeapNode*)

【0】README 1&#xff09;本文旨在给出 二叉堆优先队列的实现 的代码实现和分析&#xff0c; 而堆节点类型 不外乎三种&#xff1a; 一&#xff0c; 基本类型如int&#xff1b; 二&#xff0c;结构体类型 struct HeapNode&#xff1b; 三&#xff0c;结构体指针类型 struct H…

Spring boot(六):如何优雅的使用mybatis

总结 hibernate 和 mybatis 的区别 hibernate的特点是所有的sql都用java代码生成&#xff0c;不用跳出程序去&#xff08;看&#xff09;sql&#xff0c;发展到最顶端就是Spring data jpa了。 mybatis初期使用比较麻烦&#xff0c;需要各种配置文件、实体类、dao层映射关联、还…

Java中创建String的两道面试题及详解

转载自 Java中创建String的两道面试题及详解 我们知道创建一个String类型的变量一般有以下两种方法&#xff1a; String str1 "abcd";String str2 new String("abcd"); 那么为什么会存在这两种创建方式呢&#xff0c;它们在内存中的表现形式各有什么区别…