NEFU-数据结构 实验2-栈和队列

目录

1. 利用栈的运算,实现一个表达式(以#结束)中括号的匹配算法:括号只含有{},(),[ ]。匹配输出1,否则输出0。(程序题, 20分)

2. 试编写算法,采用顺序存储实现栈的初始化、入栈、出栈操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 5分)

3. 编程,采用顺序存储实现循环队列的初始化、入队、出队操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 10分)

4.试编写算法,利用栈实现表达式求值算法。要求输入数字为个位数,表达式以#结束。 (程序题, 20分)

5. 编程,利用栈实现数制转换(将一个十进制数转换成d进制数,d为整数),例如 1024 2,输出为1024(10)=10000000000(2)。(程序题, 15分)

6.试编写算法,采用链式存储实现栈的初始化、入栈、出栈操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 10分)

7. 利用队列打印杨辉三角:编写程序,根据输入的行数,屏幕显示杨辉三角。输入整形数据,输出数字图形。输出格式:(%d两个空格)(程序题, 10分)

8. 编程,采用链式存储实现队列的初始化、入队、出队操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 10分)


1. 利用栈的运算,实现一个表达式(以#结束)中括号的匹配算法:括号只含有{},(),[ ]。匹配输出1,否则输出0。(程序题, 20分)

输入格式:

1+(2+3)*[3*6]/{4+5}+{[8*7]}#

输出:

1

#include <bits/stdc++.h>
#define MAXSIZE 100
typedef int Status;
typedef char SElemType;

using namespace std;

typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
    S.base=new SElemType[MAXSIZE];
    if(!S.base) exit(-2);
    S.top=S.base;
    S.stacksize=MAXSIZE;
    return 1;
}

Status Push(SqStack &S,SElemType e)
{
    if(S.top-S.base==S.stacksize) return 0;
    *S.top++=e;
    return 1;
}

Status Pop(SqStack &S,SElemType &e)
{
    if(S.top==S.base) return 0;
    e=*--S.top;
    return 1;
}

SElemType GetTop(SqStack S)
{
    if(S.top!=S.base)
    {
        return *(S.top-1);
    }
    return 1;
}

int main()
{
    SqStack S;
    InitStack(S);
    char x,rub;
    int flag;
    cin >> x;
    while(x!='#')
    {
        if(x=='{'||x=='('||x=='[')
        {
            Push(S,x);
        }
        if(x=='}'||x==')'||x==']')
        {
            if((GetTop(S)=='{'&&x=='}')||(GetTop(S)=='('&&x==')')||(GetTop(S)=='['&&x==']'))
            {
                flag=1;
                Pop(S,rub);
            }
            else
            {
                flag=0;
                break;
            }
        }
        cin >> x;
    }
    if(S.top!=S.base) flag=0;
    cout << flag << endl;
    return 0;
}

2. 试编写算法,采用顺序存储实现栈的初始化、入栈、出栈操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 5分)

输入:1 2 3 4 5 6 7 8 9 0
输出:9 8 7 6 5 4 3 2 1

#include <bits/stdc++.h>
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;

using namespace std;

typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S)
{
    S.base=new SElemType[MAXSIZE];
    if(!S.base) exit(-2);
    S.top=S.base;
    S.stacksize=MAXSIZE;
    return 1;
}

Status Push(SqStack &S,SElemType e)
{
    if(S.top-S.base==S.stacksize) return 0;
    *S.top++=e;
    return 1;
}

Status Pop(SqStack &S,SElemType &e)
{
    if(S.top==S.base) return 0;
    e=*--S.top;
    return 1;
}

SElemType GetTop(SqStack S)
{
    if(S.top!=S.base)
    {
        return  *(S.top-1);
    }
    return 1;
}

int main()
{
    SqStack S;
    InitStack(S);
    int num;
    cin >> num;
    while(num!=0)
    {
        Push(S,num);
        cin >> num;
    }
    while(S.top!=S.base)
    {
        Pop(S,num);
        cout << num << " ";
    }
    return 0;
}

3. 编程,采用顺序存储实现循环队列的初始化、入队、出队操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 10分)

输入:
1 2 3 4 5 6 7 8 9 0
输出:
1 2 3 4 5 6 7 8 9

#include <bits/stdc++.h>
#define MAXSIZE 100
typedef int QElemType ;
typedef int Status ;

using namespace std;

typedef struct
{
    QElemType *base;
    int front;
    int rear;
}SqQueue;

Status InitQueue(SqQueue &Q)
{
    Q.base= new QElemType[MAXSIZE];
    if(!Q.base) exit(-2);
    Q.front = Q.rear = 0;
    return 1;
}

Status EnQueue(SqQueue &Q ,QElemType e)
{
    if((Q.rear+1)%MAXSIZE==Q.front) return -1;
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXSIZE;
    return 1;
}

Status DeQueue(SqQueue &Q,QElemType &e)
{
    if(Q.front == Q.rear) return -2;
    e=Q.base[Q.front];
    Q.front = (Q.front +1 )%MAXSIZE;
    return 1;
}

int main()
{
    int num;
    int x;
    SqQueue q;
    InitQueue(q);
    cin >> num;
    while(num!=0)
    {
        EnQueue(q,num);
        cin >> num;
    }
    while(q.rear!=q.front)
    {
        DeQueue(q,x);
        cout << x << " ";
    }
    //cout << "Hello world!" << endl;
    return 0;
}

4.试编写算法,利用栈实现表达式求值算法。要求输入数字为个位数,表达式以#结束。 (程序题, 20分)

第1组测试数据输入:6-(9-3)/2-(7-1)/3#              输出:1
第2组测试数据输入:9-3*2#                           输出:3
第3组测试数据:9-3*(9-7)/2-4*3#                    输出:-6

#include <bits/stdc++.h>
using namespace std;

#define MAXSIZE 100
typedef int Status;
typedef char ElemType;

typedef struct StackNode
{
    ElemType data;
    struct StackNode *next;
}StackNode,*LinkStack;

Status InitStack(LinkStack &S)
{
    S=NULL;
    return 1;
}

Status Push(LinkStack &S,ElemType e)
{
    LinkStack p;
    p=new StackNode;
    p->data=e;
    p->next=S;
    S=p;
    return 1;
}

Status Pop(LinkStack &S,ElemType &e)
{
    if(S==NULL) return -1;
    LinkStack p;
    p=S;
    e=p->data;
    S=p->next;
    delete p;
    return 1;
}

char GetHead(LinkStack S)
{
    if(S!=NULL) return S->data;
    return 1;
}

int In(char ch)
{
    if(ch>47&&ch<59) return 0;
    else return 1;
}

int Operate(int a,char theta,int b)
{
        //cout << a << theta << b;
        int x;
        if(theta=='+') x= a+b;
        if(theta=='-') x= a-b;
        if(theta=='*') x= a*b;
        if(theta=='/') x= a/b;
        //cout << x;
        return x;
}

char Precede(char optr,char ch)
{
    if(optr=='#'&&ch!='#') return '<';
    if(ch=='#'&&optr!='#') return '>';
    if(ch=='#'&&optr=='#') return '=';
    if(ch=='(') return '<';
    if(ch==')'&&optr!='(') return '>';
    if(ch==')'&&optr=='(') return '=';
    if(optr=='('&&ch!=')') return '<';
    if(optr==')') return '>';
    if(ch!='#'&&ch!='('&&ch!=')'&&ch==optr) return '>';
    if((optr!='#'&&optr!='('&&optr!=')')&&(ch=='+'||ch=='-')) return '>';
    if(ch=='*'||ch=='/')
    {
        if(optr=='+'||optr=='-') return '<';
        if(optr=='*'||optr=='/') return '>';
    }
    /*
    if(ch=='+'||ch=='-') return '>';
    if(ch=='*'||ch=='/')
    {
        if(optr=='+'||optr=='-') return '<';
        if(optr=='*'||optr=='/') return '>';
    };
*/
    return 1;
}

int main()
{
    LinkStack OPND;
    LinkStack OPTR;
    InitStack(OPND);
    InitStack(OPTR);
    Push(OPTR,'#');
    char theta,x;
    char a,b;
    char ch;
    cin>>ch;
    //cout <<ch;
    //cout <<char(49);
    while(ch!='#'||GetHead(OPTR)!='#')
    {
        if(!In(ch))
        {
            Push(OPND,ch);
            cin>>ch;
            //cout <<ch;
        }
        else
        {
            //x=GetHead(OPTR);
            //cout << x;
            //cout<<GetHead(OPTR)<<" "<<ch<<endl;
            //cout<<GetHead(OPND) <<endl;
            switch(Precede(GetHead(OPTR),ch))
            {
                //cout<<GetHead(OPTR)<<" "<<ch<<endl;
                //cout <<Precede(GetHead(OPTR),ch);
                case '<':
                {
                    Push(OPTR,ch);
                    cin>>ch;
                    //cout <<ch;
                    break;
                }
                case '>':
                {
                    Pop(OPTR,theta);
                    Pop(OPND,b);
                    Pop(OPND,a);
                    //cout <<Operate(int(a-'0'),theta,int(b-'0'));
                    Push(OPND,char(Operate(int(a-'0'),theta,int(b-'0'))+'0'));
                    break;
                }
                case '=':
                {
                    Pop(OPTR,x);
                    cin >> ch;
                    //cout <<ch;
                    break;
                }
            }
        }
    }
    int x1;
    x=GetHead(OPND);
    //cout << typeid(0-int('0'-x)).name();
    if(x<'0')
    {
        x=0-(int('0'-x));
        x1=x;
        cout<<x1<<endl;
    }
    else cout << x << endl;
    return 0;
}

5. 编程,利用栈实现数制转换(将一个十进制数转换成d进制数,d为整数),例如 1024 2,输出为1024(10)=10000000000(2)。(程序题, 15分)

输入:
117 6
输出:
117(10)=313(6)

#include <bits/stdc++.h>

using namespace std;
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;

typedef struct StackNode
{
    ElemType data;
    struct StackNode *next;
}StackNode,*LinkStack;

Status InitStack(LinkStack &S)
{
    S=NULL;
    return 1;
}

Status Push(LinkStack &S,ElemType e)
{
    LinkStack p;
    p=new StackNode;
    p->data=e;
    p->next=S;
    S=p;
    return 1;
}

Status Pop(LinkStack &S,ElemType &e)
{
    if(S==NULL) return -1;
    LinkStack p;
    p=S;
    e=p->data;
    S=p->next;
    delete p;
    return 1;
}

Status GetHead(LinkStack S)
{
    if(S!=NULL) return S->data;
    return 1;
}

int main()
{
    int num,numy;
    int wei;
    int x;
    LinkStack s;
    InitStack(s);
    cin>>num>>wei;
    numy=num;
    while(num)
    {
        Push(s,num%wei);
        num=num/wei;
    }
    cout<<numy<<'('<<10<<')'<<'=';
    while(s!=NULL)
    {
        Pop(s,x);
        cout<<x;
    }
    cout <<'('<<wei<<')'<<endl;
    //cout << "Hello world!" << endl;
    return 0;
}

6.试编写算法,采用链式存储实现栈的初始化、入栈、出栈操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 10分)

输入:1 2 3 4 5 6 7 8 9 0    输出:9 8 7 6 5 4 3 2 1
或者输入:11 22 33 44 55 66 77 88 99 0  输出:99 88 77 66 55 44 33 22 11

#include <bits/stdc++.h>

using namespace std;
#define MAXSIZE 100
typedef int Status;
typedef int ElemType;

typedef struct StackNode
{
    ElemType data;
    struct StackNode *next;
}StackNode,*LinkStack;

Status InitStack(LinkStack &S)
{
    S=NULL;
    return 1;
}

Status Push(LinkStack &S,ElemType e)
{
    LinkStack p;
    p=new StackNode;
    p->data=e;
    p->next=S;
    S=p;
    return 1;
}

Status Pop(LinkStack &S,ElemType &e)
{
    if(S==NULL) return -1;
    LinkStack p;
    p=S;
    e=p->data;
    S=p->next;
    delete p;
    return 1;
}

Status GetHead(LinkStack S)
{
    if(S!=NULL) return S->data;
    return 1;
}

int main()
{
    int n;
    LinkStack s;
    InitStack(s);
    cin >> n;
    while(n!=0)
    {
        Push(s,n);
        cin >> n;
    }
    while(s!=NULL)
    {
        Pop(s,n);
        cout << n << " ";
    }
    cout << endl;
    //cout << "Hello world!" << endl;
    return 0;
}

7. 利用队列打印杨辉三角:编写程序,根据输入的行数,屏幕显示杨辉三角。输入整形数据,输出数字图形。输出格式:(%d两个空格)(程序题, 10分)

输入:
9
输出:

1  

1  1  

1  2  1  

1  3  3  1  

1  4  6  4  1  

1  5  10  10  5  1  

1  6  15  20  15  6  1  

1  7  21  35  35  21  7  1  

1  8  28  56  70  56  28  8  1    

#include <bits/stdc++.h>

#define MAXSIZE 100
typedef int QElemType ;
typedef int Status ;
using namespace std;

typedef struct
{
    QElemType *base;
    int front;
    int rear;
}SqQueue;

Status InitQueue(SqQueue &Q)
{
    Q.base= new QElemType[MAXSIZE];
    if(!Q.base) exit(-2);
    Q.front = Q.rear = 0;
    return 1;
}

Status EnQueue(SqQueue &Q ,QElemType e)
{
    if((Q.rear+1)%MAXSIZE==Q.front) return -1;
    Q.base[Q.rear]=e;
    Q.rear=(Q.rear+1)%MAXSIZE;
    return 1;
}

Status GetHead(SqQueue Q,QElemType &e)
{
    if(Q.front != Q.rear)
    e=Q.base[Q.front];
    return 1;
}

Status DeQueue(SqQueue &Q,QElemType &e)
{
    if(Q.front == Q.rear) return -2;
    e=Q.base[Q.front];
    Q.front = (Q.front +1 )%MAXSIZE;
    return 1;
}

int main()
{
    int n,k,m,e1,e2,e,i;
    SqQueue q;
    InitQueue(q);
    EnQueue(q,1);
    EnQueue(q,1);
    int num;
    cin >> n;
    if(n==1) printf("%d  \n",1);
    if(n==2)
    {
        EnQueue(q,1);
        printf("%d  \n",1);
        printf("%d  ",1);
        printf("%d  \n",1);
    }
    if(n>3||n==3)
    {
        printf("%d  \n",1);
        printf("%d  ",1);
        printf("%d  \n",1);
        for(k=3;k<=n;k++)
        {
            e1=0;
            e2=0;
            while(e2!=1)
            {
                DeQueue(q,e1);
                GetHead(q,e2);
                e=e1+e2;
                EnQueue(q,e);
            }
            EnQueue(q,e2);
            for(i=1;i<=k;i++)
            {
                DeQueue(q,e);
                printf("%d  ",e);
                EnQueue(q,e);
            }
            cout << endl;
        }
    }
    return 0;
}

8. 编程,采用链式存储实现队列的初始化、入队、出队操作。要求输入整形数据,输入0表示结束(0不存储在栈中)。(程序题, 10分)

输入:1 2 3 4 5 6 7 8 9 0
输出:1 2 3 4 5 6 7 8 9

#include <bits/stdc++.h>

#define MAXSIZE 100
typedef int Status;
typedef int QElemType;
using namespace std;

typedef struct QNode
{
    QElemType data;
    struct QNode *next;
}QNode,*QueuePtr;

typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

Status InitQueue(LinkQueue &Q)
{
    Q.front=Q.rear=new QNode;
    Q.front->next=NULL;
    return 1;
}

Status EnQueue(LinkQueue &Q,QElemType e)
{
    QueuePtr p;
    p=new QNode;
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    return 1;
}

Status DeQueue(LinkQueue &Q,QElemType &e)
{
    QueuePtr p;
    if(Q.front==Q.rear) return -1;
    p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    if(p==Q.rear) Q.front=Q.rear;
    delete p;
    return 1;
}

Status GetHead(LinkQueue &Q,QElemType &e)
{
    if(Q.front!=Q.rear) return Q.front->next->data;
    return 1;
}

int main()
{
    int n;
    LinkQueue q;
    InitQueue(q);
    cin >> n;
    while(n!=0)
    {
        EnQueue(q,n);
        cin >> n;
    }
    while(q.front !=q.rear)
    {
        DeQueue(q,n);
        cout  << n << " " ;
    }
    cout << endl;
    return 0;
}

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值