日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

栈和队列的应用大全

發布時間:2023/12/29 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 栈和队列的应用大全 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

應用一:棧在括號匹配中的應用

#include <stdio.h> #include <stdlib.h> #include<stdbool.h> #define MaxSize 50typedef char ElemType;typedef struct {ElemType data[MaxSize];int top; } SqStack;bool InitStack(SqStack *s) {s->top=-1;//初始化棧 }bool Push(SqStack *s,ElemType e) {if(s->top==MaxSize-1)return false;s->data[++s->top]=e;return true; }bool Pop(SqStack *s,ElemType *e) {if(s->top==-1)return false;*e=s->data[s->top--];return true; }void PrintStack(SqStack *s) {int i=0;for(int i=0; i<=s->top; i++){printf("%d-->",s->data[i]);}printf("\n"); }//判斷棧是否為空 bool EmptyStack(SqStack *s) {if(s->top==-1)return true;return false; }bool BracketsCheck(char *str) {SqStack s;InitStack(&s);int i=0;char e;while(str[i]!='\0'){switch(str[i]){//左括號匹配case '(':Push(&s,'(');break;case '[':Push(&s,'[');break;case '{':Push(&s,'{');break;//右括號檢測棧頂case')':Pop(&s,&e);if(e!='(') return false;break;case ']':Pop(&s,&e);if(e!='[')return false;break;case '}':Pop(&s,&e);if(e!='{')return false;break;default: break;}i++;}if(!EmptyStack(&s)){printf("括號不匹配\n");return false;}else{printf("括號匹配\n");return true;} }int main() {char d[12];char *p;//p為字符數組printf("請輸入匹配表達式\n");gets(d);p=d;printf("%d",BracketsCheck(p));return 0; }

應用二、表達式求值

將中綴表達式,轉換為后綴表達式:操作棧棧頂元素優先級**大于等于(注意等于情況)**當前操作優先級,則棧頂元素出棧,接著比較直到,棧頂元素優先級小于當前元素優先級,當前元素入操作棧
接著依次如果是數值,則壓入棧,如果是op運算符號,則從棧中取出兩個元素,運算后在壓入棧

實現復雜,就不做實現了

應用三、棧在遞歸中的應用

1、創建帶序號no的棧, 表示遞歸層數,也就是n
2、利用棧實現遞歸到底 如下代碼

for(int i=n; i>=2; i--) //最后兩層不需要賦值{top++; //棧頂的n小,棧底的n大st[top].no=i;}

3、依次出棧,類比遞歸中回溯計算當前層的數值,主要利用兩個變量 fv1,fv2保存之前的結果

while(top>=0){st[top].val=2*x*fv2-2*(st[top].no-1)*fv1; //遞歸表達式fv1=fv2; //更新fv1 Pn-2的值fv2=st[top].val; //更新fv2 Pn-1的值top--;}

整體代碼

#include <stdio.h> #include <stdlib.h> #include<stdbool.h> #define MaxSize 50typedef char ElemType;/*利用棧計算遞歸表達式Pn(x)= 1 ,n=0;= 2x ,n=1;= 2xPn-1(x)-2(n-1)Pn-2(x) ,n>1 */double p(int n,double x) {struct stack{int no;//當前n 也就是遞歸層數double val;} st[MaxSize];int top=-1;for(int i=n; i>=2; i--) //最后兩層不需要賦值{top++; //棧頂的n小,棧底的n大st[top].no=i;}double fv1=1,fv2=2*x;//P0=1,P1=2*xwhile(top>=0){st[top].val=2*x*fv2-2*(st[top].no-1)*fv1; //遞歸表達式fv1=fv2; //更新fv1 Pn-2的值fv2=st[top].val; //更新fv2 Pn-1的值top--;}if(n==0){return fv1;}return fv2; }int main() {printf("%lf",p(3,1)); //-4return 0; }

應用三、二叉樹層序遍歷

#include "stdio.h" #include "stdlib.h" #define ERROR 0typedef struct tree { char data; struct tree *lchild; struct tree *rchild; }*Ptree; typedef Ptree ElementType;struct Node{ ElementType Data; struct Node *Next; }; struct QNode{ struct Node *rear; struct Node *front; }; typedef struct QNode *Queue; //創建樹 Ptree createTree();//創建隊列 Queue CreateQueue(); //刪除隊列頭元素 ElementType DeleteQ(Queue PtrQ); //在隊尾插入元素 void InsertQ(ElementType item,Queue PtrQ); //判斷是否空 int IsEmpty(Queue Q); //利用隊列層次遍歷 void LevelOrderTraversal(Ptree BT);void main() { Ptree t; printf("先序創建二叉樹,用空格代表虛結點:\n");t=createTree(); printf("\n");printf("利用隊列的層次遍歷:\n");LevelOrderTraversal(t);printf("\n");system("pause"); }//樹的建立Ptree createTree() { char ch; Ptree t; ch=getchar(); //輸入二叉樹數據if(ch==' ') //判斷二叉樹是否為空t=NULL; else { t=(Ptree)malloc(sizeof(Ptree)); //二叉樹的生成t->data=ch; t->lchild=createTree(); t->rchild=createTree(); } return t; } //創建隊列 Queue CreateQueue(){ Queue PtrQ; PtrQ=(Queue)malloc(sizeof(struct QNode)); struct Node *rear; struct Node *front; rear =(Node*)malloc(sizeof(struct Node)); rear=NULL; front =(Node*)malloc(sizeof(struct Node)); front=NULL; PtrQ->front=front; PtrQ->rear=rear; return PtrQ; }; //刪除隊列頭元素 ElementType DeleteQ(Queue PtrQ){ struct Node *FrontCell; ElementType FrontElem; if(IsEmpty(PtrQ)){ printf("隊列空"); return ERROR; } FrontCell=PtrQ->front; if(PtrQ->front==PtrQ->rear) PtrQ->front=PtrQ->rear=NULL; else{ PtrQ->front=PtrQ->front->Next; } FrontElem=FrontCell->Data; free(FrontCell); return FrontElem; } //在隊尾插入元素 void InsertQ(ElementType item,Queue PtrQ){ struct Node *FrontCell; FrontCell=(Node*)malloc(sizeof(struct Node)); FrontCell->Data=item; FrontCell->Next=NULL; if(IsEmpty(PtrQ)){ PtrQ->front=FrontCell; PtrQ->rear=FrontCell; } else{ PtrQ->rear->Next=FrontCell; PtrQ->rear=FrontCell; } }; //判斷是否空 int IsEmpty(Queue Q){ return(Q->front==NULL); }; //利用隊列層次遍歷 void LevelOrderTraversal(Ptree BT) {Queue Q;Ptree T;if(!BT) return;Q=CreateQueue();T=BT;InsertQ(T,Q);while(!IsEmpty(Q)){T=DeleteQ(Q);printf("%c",T->data);if(T->lchild) InsertQ(T->lchild,Q);if(T->rchild) InsertQ(T->rchild,Q);} };

總結

以上是生活随笔為你收集整理的栈和队列的应用大全的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。