Error[8]: Undefined offset: 919, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

应用一:栈在括号匹配中的应用
#include 
#include 
#include
#define MaxSize 50

typedef 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]!=')'switch
    {
        ([str]i)//左括号匹配
        {
        case
        '(' :Push
            (&,s'(');break
            ;case
        '[' :Push
            (&,s'[');break
            ;case
        '{' :Push
            (&,s'{');break
            ;//右括号检测栈顶
        case
        ')':Pop
            (&,s&)e;if
            (!=e'(')return ; falsebreak
            ;case
        ']' :Pop
            (&,s&)e;if
            (!=e'[')return; falsebreak
            ;case
        '}' :Pop
            (&,s&)e;if
            (!=e'{')return; falsebreak
            ;default
            :break ;}
        ++
        i;}
    if
    (!EmptyStack(&)s)printf
    {
        ("括号不匹配\n");return
        ; false}
    else
    printf
    {
        ("括号匹配\n");return
        ; true}

    }
int



main ()char
{


    [ d12];char
    * ;p//p为字符数组printf
    ("请输入匹配表达式\n");gets
    ()d;=
    p;dprintf
    ("%d",BracketsCheck()p);return


    0 ;}
实现复杂,就不做实现了


应用二、表达式求值

将中缀表达式,转换为后缀表达式: *** 作栈栈顶元素优先级**大于等于(注意等于情况)**当前 *** 作优先级,则栈顶元素出栈,接着比较直到,栈顶元素优先级小于当前元素优先级,当前元素入 *** 作栈
接着依次如果是数值,则压入栈,如果是op运算符号,则从栈中取出两个元素,运算后在压入栈

for

应用三、栈在递归中的应用

1、创建带序号no的栈, 表示递归层数,也就是n
2、利用栈实现递归到底 如下代码

   (int= i;n2 i>=;-- i)//最后两层不需要赋值 ++
    {
        top;//栈顶的n小,栈底的n大 [
        st]top.=no;i}
    while

3、依次出栈,类比递归中回溯计算当前层的数值,主要利用两个变量 fv1,fv2保存之前的结果

    (0top>=)[
    {
        st]top.=val2**x-fv22*([st]top.-no1)*;fv1//递归表达式 =
        fv1;fv2//更新fv1   Pn-2的值 =
        fv2[st]top.;val//更新fv2  Pn-1的值 --
        top;}
    #

整体代码

include# 
include# 
include#
defineMaxSize 50 typedef

char ; ElemTypedouble

/*

利用栈计算递归表达式

Pn(x)= 1    ,n=0;
     = 2x   ,n=1;
     = 2xPn-1(x)-2(n-1)Pn-2(x) ,n>1  
*/


p (int, ndouble) xstruct
{
    stack int
    {
        ; no//当前n 也就是递归层数double
        ; val}
    [ st]MaxSize;int
    = top-1;for
    (int= i;n2 i>=;-- i)//最后两层不需要赋值 ++
    {
        top;//栈顶的n小,栈底的n大 [
        st]top.=no;i}
    double
    = fv11,=fv22*;x//P0=1,P1=2*xwhile
    (0top>=)[
    {
        st]top.=val2**x-fv22*([st]top.-no1)*;fv1//递归表达式 =
        fv1;fv2//更新fv1   Pn-2的值 =
        fv2[st]top.;val//更新fv2  Pn-1的值 --
        top;}
    if
    (==n0)return
    {
        ; fv1}
    return
    ; fv2}
int


main ()printf
{

    ("%lf",p(3,1));//-4  return



    0 ;}
#


应用三、二叉树层序遍历
include"stdio.h" #  
include"stdlib.h" #    
 
defineERROR 0 typedef
 
struct tree char
{  
    ; datastruct  
    tree * ;lchildstruct  
    tree * ;rchild}  
 *;Ptreetypedef 
 
; Ptree ElementTypestruct
 
Node ;{      
     ElementType Datastruct      
    Node * ;Next}      
;struct      
      
QNode struct{      
    Node * ;rearstruct      
    Node * ;front}      
;typedef  
struct QNode * ;Queue//创建树    
 
createTree
Ptree ();//创建队列  
 
CreateQueue
Queue ();//删除队列头元素    
DeleteQ
ElementType ()Queue PtrQ;//在队尾插入元素     
void
InsertQ (,ElementType item)Queue PtrQ;//判断是否空      
int
IsEmpty ()Queue Q;//利用队列层次遍历  
 
void
LevelOrderTraversal ()Ptree BT;void
 
 
main ();  
{   
   Ptree tprintf 
   ("先序创建二叉树,用空格代表虚结点:\n");=
   tcreateTree();printf 
   ("\n");printf
   ("利用队列的层次遍历:\n");LevelOrderTraversal
   ()t;printf
   ("\n");system
   ("pause");}
//树的建立
 
 
 
createTree
 Ptree ()char   
 {  
     ; ch;  
     Ptree t=  
     chgetchar();//输入二叉树数据  if
     (==ch' ')//判断二叉树是否为空  =
         tNULL;else  
     = 
     {  
         t()Ptreemalloc(sizeof()Ptree);//二叉树的生成  =
         t->data;ch=  
         t->lchildcreateTree();=  
         t->rchildcreateTree();}  
     return  
     ; t}  
 //创建队列    
 
 
CreateQueue
Queue ();{  
    Queue PtrQ=    
    PtrQ()Queuemalloc(sizeof(structQNode ));struct  
    Node * ;rearstruct  
    Node * ;front=  
    rear (*Node)malloc(sizeof(structNode ));=  
    rearNULL;=  
    front (*Node)malloc(sizeof(structNode ));=  
    frontNULL;=  
    PtrQ->front;front=  
    PtrQ->rear;rearreturn  
    ; PtrQ}  
;//删除队列头元素   
 
DeleteQ
ElementType ()Queue PtrQstruct{      
    Node * ;FrontCell;      
    ElementType FrontElemif      
      
    (IsEmpty()PtrQ)printf{      
        ("队列空");return      
        ; ERROR}      
    =    
    FrontCell;PtrQ->frontif    
    (==PtrQ->front)PtrQ->rear=    
       PtrQ->front=PtrQ->rearNULL;else    
    ={    
        PtrQ->front;PtrQ->front->Next}    
    =    
      FrontElem;FrontCell->Datafree    
      ()FrontCell;return    
      ; FrontElem}      
//在队尾插入元素        
 
void
InsertQ (,ElementType item)Queue PtrQstruct{    
    Node * ;FrontCell=    
    FrontCell(*Node)malloc(sizeof(structNode ));=  
    FrontCell->Data;item=  
    FrontCell->NextNULL;if  
  
    (IsEmpty()PtrQ)={  
        PtrQ->front;FrontCell=  
        PtrQ->rear;FrontCell}  
    else  
    ={  
        PtrQ->rear->Next;FrontCell=  
        PtrQ->rear;FrontCell}  
    }  
;//判断是否空     
 
int
IsEmpty ()Queue Qreturn{  
     (==Q->frontNULL);}    
;//利用队列层次遍历 
 
 
 
void
LevelOrderTraversal ()Ptree BT;
{
	Queue Q;
	Ptree Tif
	(!)BTreturn ;=
	QCreateQueue();=
	T;BTInsertQ
	(,T)Q;while
	(!IsEmpty()Q)={
		TDeleteQ()Q;printf
		("%c",)T->data;if
		()T->lchildInsertQ (,T->lchild)Q;if
		()T->rchildInsertQ (,T->rchild)Q;}
	}
;[+++]
)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
栈的应用大全_C_内存溢出

栈的应用大全

栈的应用大全,第1张

应用一:栈在括号匹配中的应用
#include 
#include 
#include
#define MaxSize 50

typedef 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]!=')'switch
    {
        ([str]i)//左括号匹配
        {
        case
        '(' :Push
            (&,s'(');break
            ;case
        '[' :Push
            (&,s'[');break
            ;case
        '{' :Push
            (&,s'{');break
            ;//右括号检测栈顶
        case
        ')':Pop
            (&,s&)e;if
            (!=e'(')return ; falsebreak
            ;case
        ']' :Pop
            (&,s&)e;if
            (!=e'[')return; falsebreak
            ;case
        '}' :Pop
            (&,s&)e;if
            (!=e'{')return; falsebreak
            ;default
            :break ;}
        ++
        i;}
    if
    (!EmptyStack(&)s)printf
    {
        ("括号不匹配\n");return
        ; false}
    else
    printf
    {
        ("括号匹配\n");return
        ; true}

    }
int



main ()char
{


    [ d12];char
    * ;p//p为字符数组printf
    ("请输入匹配表达式\n");gets
    ()d;=
    p;dprintf
    ("%d",BracketsCheck()p);return


    0 ;}
实现复杂,就不做实现了


应用二、表达式求值

将中缀表达式,转换为后缀表达式: *** 作栈栈顶元素优先级**大于等于(注意等于情况)**当前 *** 作优先级,则栈顶元素出栈,接着比较直到,栈顶元素优先级小于当前元素优先级,当前元素入 *** 作栈
接着依次如果是数值,则压入栈,如果是op运算符号,则从栈中取出两个元素,运算后在压入栈

for

应用三、栈在递归中的应用

1、创建带序号no的栈, 表示递归层数,也就是n
2、利用栈实现递归到底 如下代码

   (int= i;n2 i>=;-- i)//最后两层不需要赋值 ++
    {
        top;//栈顶的n小,栈底的n大 [
        st]top.=no;i}
    while

3、依次出栈,类比递归中回溯计算当前层的数值,主要利用两个变量 fv1,fv2保存之前的结果

    (0top>=)[
    {
        st]top.=val2**x-fv22*([st]top.-no1)*;fv1//递归表达式 =
        fv1;fv2//更新fv1   Pn-2的值 =
        fv2[st]top.;val//更新fv2  Pn-1的值 --
        top;}
    #

整体代码

include# 
include# 
include#
defineMaxSize 50 typedef

char ; ElemTypedouble

/*

利用栈计算递归表达式

Pn(x)= 1    ,n=0;
     = 2x   ,n=1;
     = 2xPn-1(x)-2(n-1)Pn-2(x) ,n>1  
*/


p (int, ndouble) xstruct
{
    stack int
    {
        ; no//当前n 也就是递归层数double
        ; val}
    [ st]MaxSize;int
    = top-1;for
    (int= i;n2 i>=;-- i)//最后两层不需要赋值 ++
    {
        top;//栈顶的n小,栈底的n大 [
        st]top.=no;i}
    double
    = fv11,=fv22*;x//P0=1,P1=2*xwhile
    (0top>=)[
    {
        st]top.=val2**x-fv22*([st]top.-no1)*;fv1//递归表达式 =
        fv1;fv2//更新fv1   Pn-2的值 =
        fv2[st]top.;val//更新fv2  Pn-1的值 --
        top;}
    if
    (==n0)return
    {
        ; fv1}
    return
    ; fv2}
int


main ()printf
{

    ("%lf",p(3,1));//-4  return



    0 ;}
#


应用三、二叉树层序遍历
include"stdio.h" #  
include"stdlib.h" #    
 
defineERROR 0 typedef
 
struct tree char
{  
    ; datastruct  
    tree * ;lchildstruct  
    tree * ;rchild}  
 *;Ptreetypedef 
 
; Ptree ElementTypestruct
 
Node ;{      
     ElementType Datastruct      
    Node * ;Next}      
;struct      
      
QNode struct{      
    Node * ;rearstruct      
    Node * ;front}      
;typedef  
struct QNode * ;Queue//创建树    
 
createTree
Ptree ();//创建队列  
 
CreateQueue
Queue ();//删除队列头元素    
DeleteQ
ElementType ()Queue PtrQ;//在队尾插入元素     
void
InsertQ (,ElementType item)Queue PtrQ;//判断是否空      
int
IsEmpty ()Queue Q;//利用队列层次遍历  
 
void
LevelOrderTraversal ()Ptree BT;void
 
 
main ();  
{   
   Ptree tprintf 
   ("先序创建二叉树,用空格代表虚结点:\n");=
   tcreateTree();printf 
   ("\n");printf
   ("利用队列的层次遍历:\n");LevelOrderTraversal
   ()t;printf
   ("\n");system
   ("pause");}
//树的建立
 
 
 
createTree
 Ptree ()char   
 {  
     ; ch;  
     Ptree t=  
     chgetchar();//输入二叉树数据  if
     (==ch' ')//判断二叉树是否为空  =
         tNULL;else  
     = 
     {  
         t()Ptreemalloc(sizeof()Ptree);//二叉树的生成  =
         t->data;ch=  
         t->lchildcreateTree();=  
         t->rchildcreateTree();}  
     return  
     ; t}  
 //创建队列    
 
 
CreateQueue
Queue ();{  
    Queue PtrQ=    
    PtrQ()Queuemalloc(sizeof(structQNode ));struct  
    Node * ;rearstruct  
    Node * ;front=  
    rear (*Node)malloc(sizeof(structNode ));=  
    rearNULL;=  
    front (*Node)malloc(sizeof(structNode ));=  
    frontNULL;=  
    PtrQ->front;front=  
    PtrQ->rear;rearreturn  
    ; PtrQ}  
;//删除队列头元素   
 
DeleteQ
ElementType ()Queue PtrQstruct{      
    Node * ;FrontCell;      
    ElementType FrontElemif      
      
    (IsEmpty()PtrQ)printf{      
        ("队列空");return      
        ; ERROR}      
    =    
    FrontCell;PtrQ->frontif    
    (==PtrQ->front)PtrQ->rear=    
       PtrQ->front=PtrQ->rearNULL;else    
    ={    
        PtrQ->front;PtrQ->front->Next}    
    =    
      FrontElem;FrontCell->Datafree    
      ()FrontCell;return    
      ; FrontElem}      
//在队尾插入元素        
 
void
InsertQ (,ElementType item)Queue PtrQstruct{    
    Node * ;FrontCell=    
    FrontCell(*Node)malloc(sizeof(structNode ));=  
    FrontCell->Data;item=  
    FrontCell->NextNULL;if  
  
    (IsEmpty()PtrQ)={  
        PtrQ->front;FrontCell=  
        PtrQ->rear;FrontCell}  
    else  
    ={  
        PtrQ->rear->Next;FrontCell=  
        PtrQ->rear;FrontCell}  
    }  
;//判断是否空     
 
int
IsEmpty ()Queue Qreturn{  
     (==Q->frontNULL);}    
;//利用队列层次遍历 
 
 
 
void
LevelOrderTraversal ()Ptree BT;
{
	Queue Q;
	Ptree Tif
	(!)BTreturn ;=
	QCreateQueue();=
	T;BTInsertQ
	(,T)Q;while
	(!IsEmpty()Q)={
		TDeleteQ()Q;printf
		("%c",)T->data;if
		()T->lchildInsertQ (,T->lchild)Q;if
		()T->rchildInsertQ (,T->rchild)Q;}
	}
;

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1295189.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存