【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);
}

ReviewForJob(3)表、栈和队列相关推荐

  1. c语言用两个栈构造队列伪码,数据结构习题线性表栈队列.doc

    数据结构习题线性表栈队列 线性表(58) 1. 在单链表.双链表和单循环链表中,若仅知道指针p指向某结点,不知道头指针,能否将结点*p从相应的链表中删去?若可以,其时间复杂度各为多少? 2.设线性表的 ...

  2. Python__数据结构与算法——表、栈、队列

    目录 一.表 二.栈(后进先出) 三.队列(先进先出) 数据结构使用来描述一种或多种数据元素之间的特定关系,算法是程序设计中对数据操作的描述,数据结构和算法组成了程序.对于简单的任务,只要使用编程语言 ...

  3. 数据结构 思维导图【绪论、线性表、栈、队列和数组、树与二叉树、图、查找、排序】

    目录 01.绪论 02.线性表 03.栈.队列和数组 04.树与二叉树 05.图 06.查找 07.排序 算法 数据结构 排序算法 01.绪论 02.线性表 03.栈.队列和数组 04.树与二叉树 0 ...

  4. 数据结构(二):线性表包括顺序存储结构(顺序表、顺序队列和顺序栈)和链式存储结构(链表、链队列和链栈)...

    还记得数据结构这个经典的分类图吧: 今天主要关注一下线性表. 什么是线性表 线性表的划分是从数据的逻辑结构上进行的.线性指的是在数据的逻辑结构上是线性的.即在数据元素的非空有限集中 (1) 存在唯一的 ...

  5. 数据结构-线性表(栈与队列的特殊性)

    通过前面的介绍我们知道,数据结构按照对应关系可以分为一对一的线性表结构.一 对多的树形结构,多对多的图形结构,以及同属一个集合的集合结构. 在此我们分析一下线性表结构: 对于线性表我们知道有数组.链表 ...

  6. 左神算法课笔记(二):链表、栈和队列、递归Master公式、哈希表、有序表

    单向链表 双向链表 单链表.双链表最简单的面试题 1.单链表和双链表如何反转 package class02;import java.util.ArrayList;public class Code0 ...

  7. 有十五个数按由大到小顺序存放在一个数组中_数据结构基础 (代码效率优化, 线性表, 栈, 队列, 数组,字符串,树和二叉树,哈希表)...

    作者:张人大 代码效率优化 复杂度 -- 一个关于输入数据量n的函数 时间复杂度 -- 昂贵 与代码的结构设计有着紧密关系 一个顺序结构的代码,时间复杂度是O(1), 即任务与算例个数 n 无关 空间 ...

  8. b+树时间复杂度_数据结构:线性表,栈,队列,数组,字符串,树和二叉树,哈希表...

    作者:张人大 代码效率优化 复杂度 -- 一个关于输入数据量n的函数 时间复杂度 -- 昂贵 与代码的结构设计有着紧密关系 一个顺序结构的代码,时间复杂度是O(1), 即任务与算例个数 n 无关 空间 ...

  9. 数据结构-线性表之用队列实现栈用栈实现队列

    文章目录 **********用队列实现栈 一:思路 二:实现 (1)结构体定义 (2)初始化和销毁 (3)进"栈" (4)出"栈" 三:代码 ******** ...

  10. 表、栈和队列(JAVA实现)

    文章目录 1 概述 2 表 ADT 2.1 预先知识 2.1.1 Collection 接口 2.1.2 Iterator接口 2.2 List 表 的数组实现(ArrayList) 2.3 List ...

最新文章

  1. 孙甘露:小说是他的女人,写作是他爱女人的过程
  2. word List 09
  3. 剑指 Offer 15. 二进制中1的个数 and leetcode 1905. 统计子岛屿
  4. mac 终端登陆linux,Mac终端自动登录服务器
  5. python入门之装饰器
  6. java 字符串查找程序_java-如何在JAR文件中搜索字符串
  7. Socket通信学习(二):序列化与反序列化
  8. Unity3D基础4:空物体与预制体
  9. python生成器的作用是什么_看完这篇,你就知道Python生成器是什么
  10. js 获取 当前年月日以及农历日期和星期几
  11. 电脑是linux下安装win7,Linux下安装win7
  12. 【linux内核分析与应用-陈莉君】内核同步措施
  13. CA认证完整实现步骤
  14. zoj 1104 Leaps Tall Buildings(超人不会飞- -。。)
  15. 2021全球程序员收入报告!字节高级码农年薪274万元排第5
  16. 【100 种语言速成】第 4 节:Lua
  17. Tableau-可视化操作(五)
  18. android eclipse加密,Elliptic Curve Cryptography:在eclipse android中使用NFC发送加密消息
  19. WPF 获取主程序(主窗口)对象
  20. Linux 系统vim练习简单的 shell script

热门文章

  1. 【NOI2019】 机器人 【区间dp】【插值】【下降幂多项式】【分段函数】
  2. Gym - 100917F Find the Length-用最小路径树求最小环
  3. CF1497E2 Square-free division (hard version)
  4. P4159 [SCOI2009] 迷路
  5. F - Heron and His Triangle UVALive - 8206
  6. 三角形周长和【牛客网】牛客网练习赛60
  7. [2021.4.7多校省选模拟33]A,B,C
  8. 剪纸游戏(博弈论)(SG函数)
  9. P3527-[POI2011]MET-Meteors【整体二分,树状数组】
  10. 欢乐纪中某A组赛【2019.7.9】