栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

王道数据结构代码3.1.5(栈)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

王道数据结构代码3.1.5(栈)

第三题(入栈出栈序列的合法性)
#include 
#include 
#include
#define MaxSize 50  //定义栈中元素最大个数

typedef int ElemType;

bool Judge(char A[])
{
    int i=0;
    int j=0,k=0;
    while(A[i]!='')
    {
        switch(A[i]){
           case 'I':j++;break;
           case 'O':k++;break;
        }
        if(k>j)return false;//出栈次数大于入栈次数违法
        i++;
    }
    if(j!=k)return false;//非法
    else return true;
}


int main()
{

    char *A[50];
    gets(A);
    // IOIIOIOO(OK)  IIIOIOIO (NO)  IOOIOIIO  (NO)
    printf("当前输入是否合法? %d",Judge(A));




    return 0;
}


[另解]

#include 
#include 
#include
#define MaxSize 50  //定义栈中元素最大个数

typedef int ElemType;

bool Judge(char A[])
{
    int i=0;
    int k=0;
    while(A[i]!='')
    {
        switch(A[i]){
           case 'I':k++;break;
           case 'O':k--;break;
        }
        if(k<0)return false;//出栈次数大于入栈次数违法
        i++;
    }
    if(k!=0)return false;
    return true;
}


int main()
{

    char *A[50];
    gets(A);
    // IOIIOIOO(OK)  IIIOIOIO (NO)  IOOIOIIO  (NO)
    printf("当前输入是否合法? %d",Judge(A));


    return 0;
}


第四题(利用栈判断链表是否中心对称)
//字符串读入费劲,就不实现了
bool dc(LinkList L,int n)
{
    int i;
    char s[n/2];//字符栈
    LNode *p=L->next;
    for(i=0;i
        s[i]=p->data;
        p=p->next;
    }
    i--;//由于多++一次用于判断inext;//中间有中心结点
    
    while(p!=NULL&&s[i]==p->data)
    {
        i--;//出栈
        p=p->next;
    }
    if(i==-1)return true;//关于中心对称
    return false;
}

第五题(共享栈基本操作)

#include 
#include 
#include
#define MaxSize 50  //定义栈中元素最大个数

typedef int ElemType;

typedef struct
{
    ElemType data[MaxSize];//存放栈中元素
    int top[2];//栈顶指针开两个

} SqStack;

//栈的初始化
void  InitStack(SqStack *S)
{
    S->top[0]=-1;//0号栈初始为空
    S->top[1]=MaxSize;//1号栈初始为空

}

//判断栈是否为空
bool StackEmpty(SqStack *S)
{
    if(S->top[1]-S->top[0]==1)return true;//相差一个元素时栈满
    return false;
}

//入栈
bool Push(SqStack *S,ElemType x,int flag)
{
    if(S->top[1]-S->top[0]==1)return false;//栈满
    if(flag==0)
    {
        S->data[++S->top[0]]=x;//从左向右添加元素,数组下标++
    }
    else
    {
        S->data[--S->top[1]]=x;//从右向左添加元素,数组下标--
    }
    return true;
}

//出栈
bool Pop(SqStack *S,ElemType *x,int flag)
{
    if((flag==0&&S->top[0]==-1)||(flag==1&&S->top[1]==MaxSize))return false;//栈为空

    if(flag==0)
    {
        *x=S->data[S->top[0]--];//top0向左移动,出栈
    }
    else
    {
        *x=S->data[S->top[1]++];//top1向右移动,出栈
    }
    return true;
}

//获取栈顶元素
bool GetTop(SqStack *S,ElemType *x,int flag)
{
    if((flag==0&&S->top[0]==-1)||(flag==1&&S->top[1]==MaxSize))return false;//栈为空
    *x=S->data[S->top[flag]];
    return true;
}

//打印栈
void PrintStack(SqStack *S,int flag)
{
    int i;
    if(flag==0)
    {
        i=0;
        printf("打印%d号栈n",flag);
        while(i<=S->top[0])
        {
            printf("%d ",S->data[i++]);
        }
    }
    else
    {
        i=S->top[1];
        printf("打印%d号栈n",flag);
        while(i<=MaxSize-1)
        {
            printf("%d ",S->data[i++]);
        }
    }
    printf("n");
}

int main()
{

    SqStack s;
    InitStack(&s);//初始化

    for(int i=1; i<=10; i++)
    {
        Push(&s,i,0);//将1~~10放入0号栈中
    }

    for(int i=40; i<=50; i++)
    {
        Push(&s,i,1);//将40~~50放入1号栈中
    }

    printf("将1~10入0号栈,40~50入1号栈n");
    PrintStack(&s,0);
    PrintStack(&s,1);
    int *t0;
    int *t1;
    printf("1号栈pop出一个元素n");
    Pop(&s,t1,1);//出1号栈一个元素
    PrintStack(&s,1);






    return 0;
}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/886956.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号