9---------栈和队列的应用
来源:互联网 发布:纽约跑跑美国代购 知乎 编辑:程序博客网 时间:2024/06/03 23:38
- 第一部分:顺序栈的结构体定义,初始化,push,pop,判断空,判断满
- 第二部分:链栈的结构体定义,初始化,push,pop,判断空,没有栈满的情况就不用判断了
- 第三部分:顺序队列(循环队列)的结构体定义,初始化,push,pop,判断空,判断满
- 第四不对:链队的结构体定义,初始化,push,pop,判断空,没有队满的情况就不用判断了
- 括号匹配算法
- 十进制转16进制以下的个各进制算法
- 回文字符串的判断
- 单链表中存放的字符串是否中心对称算法
#include <stdio.h>#include <stdlib.h>/*** 第一部分:顺序栈的结构体定义,初始化,push,pop,判断空,判断满* 第二部分:链栈的结构体定义,初始化,push,pop,判断空,没有栈满的情况就不用判断了* 第三部分:顺序队列(循环队列)的结构体定义,初始化,push,pop,判断空,判断满* 第四不对:链队的结构体定义,初始化,push,pop,判断空,没有队满的情况就不用判断了* 括号匹配算法* 十进制转16进制以下的个各进制算法* 回文字符串的判断* 单链表中存放的字符串是否中心对称算法**/#define maxsize 100//第一部分//顺序栈的结构体定义typedef struct{ int data[maxsize]; int top;}Stack;//顺序栈的初始化void initStack(Stack ** stack){ (*stack) = (Stack *)malloc(sizeof(Stack)); (*stack) -> top = -1;}//判断栈满int isFull(Stack * stack){ if(stack->top == maxsize - 1){ return 1; } return 0;}//判断栈空int isEmpty(Stack * stack){ if(stack->top == -1){ return 1; } return 0;}//入栈算法int push(Stack * stack,int e){ if(!isFull(stack)){ stack->data[++(stack->top)] = e; return 1; } return 0;}//出栈算法int pop(Stack * stack,int * e){ if(!isEmpty(stack)){ (*e) = stack->data[(stack->top)--]; return 1; } return 0;}//第二部分//链栈结构体定义,事实上就是一个单链表typedef struct Node{ int data; struct Node * next;}LStack;//链栈初始化void initLStack(LStack ** stack){ (*stack) = (LStack *)malloc(sizeof(LStack)); (*stack) -> next = NULL; //注意要将头结点指针的next指正空置}//判断空int isEmptyLStack(LStack * stack){ if(stack->next == NULL){ return 1; } return 0;}//入栈算法void pushLStack(LStack * stack,int e){ struct Node * s = (struct Node *)malloc(sizeof(struct Node)); s -> next = stack->next; s -> data = e; stack ->next = s;}//出栈算法int popLStack(LStack * stack,int * e){ struct Node * s; if(!isEmptyLStack(stack)){ s = stack->next; (*e) = s->data; stack->next = s->next; free(s); return 1; } return 0;}//第三部分:顺序队列//顺序队列结构体定义typedef struct { int data[maxsize]; int front,rear;}Queue;//顺序队的初始化void initQueue(Queue ** que){ (*que) = (Queue *)malloc(sizeof(Queue)); (*que) -> front = 0; (*que) -> rear = 0;}//判断队满int isFullQue(Queue * que){ if((que->rear + 1)%maxsize == que->front){ return 1; } return 0;}//判断队空int isEmptyQue(Queue * que){ if(que->rear == que->front){ return 1; } return 0;}//入队int inQueue(Queue * que , int e){ if(!isFullQue(que)){ que->rear = (que->rear + 1)%maxsize; que->data[que->rear] = e; } return 0;}//出队算法int deQueue(Queue * que, int * e){ if(isEmptyQue(que)){ que->front = (que->front + 1)%maxsize; (*e) = que->data[que->front]; return 1; } return 0;}//第四部分//链队结构体的定义typedef struct{ //node直接使用上面定义好的即可 struct Node * front; struct Node * rear;}LQueue;//链队的初始化void initLQueue(LQueue ** que){ (*que) = (LQueue *)malloc(sizeof(LQueue)); (*que)->front = NULL; (*que)->rear = NULL;}//判断对空的算法int isEmptyLQueue(LQueue * que){ if(que->rear == NULL || que->front == NULL){ return 1; } return 0;}//链队的入队算法,不存在对满的情况void inLQueue(LQueue * que,int e){ struct Node * node = (struct Node *)malloc(sizeof(struct Node)); node -> data = e; node -> next = NULL; que->rear->next = node;}//链队的出队算法int deLQueue(LQueue * que,int * e){ struct Node * s = que->front; if(isEmptyLQueue(que)){ return 0; } que->front = s->next; (*e) = s->data; free(s); return 1;}//算法的应用部分,用以上数据结构和算法实现下面几个算法//一个算术表达式存放在一个数组中,我们判断该算数表达式的括号是否合法,用栈可以实现,下面用到顺序栈实现该算法int justify(int arr[],int n){//括号以ascii码存放在数组内 Stack * stack; initStack(&stack); int i; char c; int e; for(i=0;i<n;i++){ c = arr[i]; if(c == '('){ push(stack,arr[i]); }else if(c == ')'){ pop(stack,&e); } } if(isEmpty(stack)){ return 1; //返回1表示合法 } return 0;//返回0表示不合法}//将10进制转换成16进制以下其余各进制,并打印int change(int e,int size){ int new = 0; int mod = 0; int w; LStack * stack; initLStack(&stack); while(e != 0){ mod = e%size; e = e/size; pushLStack(stack,mod); } while(!isEmptyLStack(stack)){ popLStack(stack,&w); char c; switch(w){ case 10: c = 'A'; break; case 11: c = 'B'; break; case 12: c = 'C'; break; case 13: c = 'D'; break; case 14: c = 'E'; break; case 15: c = 'F'; break; } if(w>=10){ printf("%c",c); }else{ printf("%d",w); } } return new;}//回文字符串的判断,分别存入到链队中和顺序栈中实现int isReverseString(char arr[],int n){ int e,v,i; Stack * stack; LQueue * que; initLQueue(&que); initStack(&stack); for(i=0;i<n;i++){ push(stack,arr[i]); inLQueue(que,arr[i]); } for(i=0;i<n;i++){ pop(stack,&e); deLQueue(que,&v); if(e != v){ return 0; } } return 1;}//单链表中存放一个字符串,判断是否中心对称,将其先存放到一个栈中,然后通过循环判断即可int isCentrosymmetry(char arr[],int n){ LStack * stack; initLStack(&stack); int i,w; for(i=0;i<n;i++){ pushLStack(stack,arr[i]); } for(i=0;i<n;i++){ popLStack(stack,&w); if(w != arr[i]){ return 0; } } return 1;}int main(){ return 0;}
阅读全文
0 0
- 9---------栈和队列的应用
- 栈和队列的应用
- 栈和队列应用
- 栈和队列的应用-魔王语言
- 数据结构----栈和队列的综合应用
- 栈和队列的思想应用
- 数据结构 栈和队列的一些应用
- 实验二 栈和队列的应用
- 表达式求值-栈和队列的应用
- 栈和队列的实际应用
- c#栈队列的实现和应用
- 队列和栈的应用 ------ 停车场
- 双向队列 和 栈的应用
- 栈和队列的基本应用
- 栈和队列的简单应用
- 数据结构----栈和队列的综合应用
- 栈和队列的简单应用
- 栈和队列的应用:迷宫问题
- mybatis详解-(20)缓存原理上
- AngularJs 商品订单信息管理
- Tomcat Jenkins
- [dialog system] 阅读笔记
- (十)java锁机制深度化
- 9---------栈和队列的应用
- C++11之多线程初探
- 利用myeclipse自带的功能实现webservice
- OCR图像识别技术的JAVA实现(二)之Tess4J的使用
- Android开发
- android getApplicationContext()强制转换为自定义的application时崩溃的原因
- 现在!立刻!马上!
- AOP简介
- Android外接midi设备的录音