[NEFU锐格 数据结构]实验二 栈和队列有关的操作

本文详细介绍了NEFU大学二年级课程中关于链式队列(Queue)和栈(Stack)的基本操作,包括队列的入队、出队、判断空队列等,以及如何利用栈实现杨辉三角和进制转换。通过实例演示了链式队列和栈在算法中的实用技巧。
摘要由CSDN通过智能技术生成

[NEFU锐格 数据结构]实验二 栈和队列有关的操作

推荐阅读
[数据结构]NEFU 大二上 锐格实验参考 目录

知识点

题目知识点
8797链式队列计算杨辉三角
8563顺序存储栈基本操作
8562链式栈基本操作
8566利用栈实现进制转化
8569利用栈进行表达式计算
8564链式队列基本操作
8565循环队列基本操作

题目

8797

5-6班的版本(没啥格式要求)

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>

typedef struct Node{
    int data;
    struct Node *next;
}LQNode,*LinkQNode;
typedef struct{
    LQNode *front,*rear;
}LQueue,*LinkQueue;

int QueueInit(LinkQueue &Q){
    LinkQNode p;
    Q=(LinkQueue)malloc(sizeof(LQueue));//申请头尾指针节点
    p=(LinkQNode)malloc(sizeof(LQNode));//申请链队头节点
    p->next=NULL;Q->front=Q->rear=p;
    return 1;
}
void QueuePush(LinkQueue &Q,int x){
    LinkQNode p=(LinkQNode)malloc(sizeof(LQNode));
    p->data=x;
    p->next=NULL;
    Q->rear->next=p;
    Q->rear=p;
}
bool QueueEmpty(LinkQueue Q){
    if(Q->front==Q->rear)return true;
    return false;
}
bool QueuePop(LinkQueue &Q,int &x){
    if(QueueEmpty(Q)){
        puts("Queue Empty!");
        return 0;
    }
    LinkQNode p=Q->front->next;
    Q->front->next=p->next;
    x=p->data;
    if(p==Q->rear)Q->rear=Q->front;//只有一个元素的话,出队后为空
    delete p;
    return 1;
}
bool GetHead(LinkQueue Q,int &head){
    if(QueueEmpty(Q))return false;
    head=Q->front->next->data;
    return true;
}
int main(){
    int n,out,head;scanf("%d",&n);
    LinkQueue Q;
    QueueInit(Q);QueuePush(Q,1);
    for(int i=2;i<=n;i++){
        QueuePush(Q,1);
        for(int j=1;j<=i-2;j++){
            QueuePop(Q,out);
            printf("%d ",out);
            GetHead(Q,head);
            out+=head;
            QueuePush(Q,out);
        }
        QueuePop(Q,out);
        printf("%d ",out);
        QueuePush(Q,1);
        puts("");
    }
    while(!QueueEmpty(Q)){
        QueuePop(Q,out);
        printf("%d ",out);
    }
    return 0;
}

7-8班的版本(有格式要求)
我觉得吧,这种题目拿格式来搞你就没意思了,而且锐格他不显示空格就好烦,我拿尺子看有没有对齐的。考试的时候吧,我建议手写个队列走个流程,然后直接printf答案。

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>

typedef struct Node{
    int data;
    struct Node *next;
}LQNode,*LinkQNode;
typedef struct{
    LQNode *front,*rear;
}LQueue,*LinkQueue;

int QueueInit(LinkQueue &Q){
    LinkQNode p;
    Q=(LinkQueue)malloc(sizeof(LQueue));//申请头尾指针节点
    p=(LinkQNode)malloc(sizeof(LQNode));//申请链队头节点
    p->next=NULL;Q->front=Q->rear=p;
    return 1;
}
void QueuePush(LinkQueue &Q,int x){
    LinkQNode p=(LinkQNode)malloc(sizeof(LQNode));
    p->data=x;
    p->next=NULL;
    Q->rear->next=p;
    Q->rear=p;
}
bool QueueEmpty(LinkQueue Q){
    if(Q->front==Q->rear)return true;
    return false;
}
bool QueuePop(LinkQueue &Q,int &x){
    if(QueueEmpty(Q)){
        puts("Queue Empty!");
        return 0;
    }
    LinkQNode p=Q->front->next;
    Q->front->next=p->next;
    x=p->data;
    if(p==Q->rear)Q->rear=Q->front;//只有一个元素的话,出队后为空
    delete p;
    return 1;
}
bool GetHead(LinkQueue Q,int &head){
    if(QueueEmpty(Q))return false;
    head=Q->front->next->data;
    return true;
}
int main(){
    int n,out,head;scanf("%d",&n);
    LinkQueue Q;
    QueueInit(Q);QueuePush(Q,1);
    for(int i=2;i<=n;i++){
        for(int j=2*(n-i)+1;j>=0;j--)printf(" ");
        QueuePush(Q,1);
        for(int j=1;j<=i-2;j++){
            QueuePop(Q,out);
            if(out<10)printf(" ");
            printf("%d  ",out);
            
            GetHead(Q,head);
            out+=head;
            QueuePush(Q,out);
        }
        QueuePop(Q,out);
        printf(" %d",out);
        QueuePush(Q,1);
        puts("");
    }
    if(!QueueEmpty(Q)){
        QueuePop(Q,out);
        printf(" 1  ");
    
        while(!QueueEmpty(Q)){
        QueuePop(Q,out);
        if(out<10)printf(" ");
        printf("%d  ",out);
    }
    }
    //printf("\n2");
    return 0;
}

8563

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#define MAXSIZE 1024
typedef struct {
    int data[MAXSIZE];
    int top;
}SeqStack;

bool StackInit(SeqStack &S){
    S.top=-1;return 1;
}
bool StackEmpty(SeqStack S){
    if(S.top==-1)return 1;
    return 0;
}
bool StackPush(SeqStack &S,int x){
    if(S.top==MAXSIZE-1){
        puts("栈满");
        return 0;
    }
    S.top++;
    S.data[S.top]=x;
    return 1;
}
bool StackPop(SeqStack &S,int &x){
    if(S.top==-1){
        puts("栈空");
        return 0;
    }
    x=S.data[S.top];
    S.top--;
    return 0;
}
bool StackGetTop(SeqStack S,int &x){
    if(S.top==-1){
        puts("栈空");
        return 0;
    }
    x=S.data[S.top];
    return 1;
}
int main(){
    int x;
    SeqStack Stk;
    StackInit(Stk);
    while(~scanf("%d",&x)){
        if(x==0)break;
        StackPush(Stk,x);
    }
    while(!StackEmpty(Stk)){
        StackPop(Stk,x);
        printf("%d ",x);
    }
    return 0;
}

8562

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>

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

bool StackInit(LinkStack &top){
    top=NULL;return 1;
}
bool StackEmpty(LinkStack top){
    if(top==NULL)return 1;
    return 0;
}
void StackPush(LinkStack &top,int x){
    StackNode *s;
    s=(StackNode*)malloc(sizeof(StackNode));
    s->data=x;
    s->next=top;
    top=s;
}
bool StackPop(LinkStack &top,int &x){
    if(top==NULL){
        puts("Stack Empty!");return 0;
    }
    x=top->data;
    StackNode *p=top;
    top=top->next;
    free(p);
    return 1;
}

int main(){
    int x;
    LinkStack Stk;
    StackInit(Stk);
    while(~scanf("%d",&x)){
        if(x==0)break;
        StackPush(Stk,x);
    }
    while(!StackEmpty(Stk)){
        StackPop(Stk,x);
        printf("%d ",x);
    }
    return 0;
}

8566

如果不是直接pop输出的话注意数据可能爆long long

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>
#define MAXSIZE 1024
typedef struct {
    int data[MAXSIZE];
    int top;
}SeqStack;

bool StackInit(SeqStack &S){
    S.top=-1;return 1;
}
bool StackEmpty(SeqStack S){
    if(S.top==-1)return 1;
    return 0;
}
bool StackPush(SeqStack &S,int x){
    if(S.top==MAXSIZE-1){
        puts("栈满");
        return 0;
    }
    S.top++;
    S.data[S.top]=x;
    return 1;
}
bool StackPop(SeqStack &S,int &x){
    if(S.top==-1){
        puts("栈空");return 0;
    }
    x=S.data[S.top];
    S.top--;
    return 0;
}
bool StackGetTop(SeqStack S,int &x){
    if(S.top==-1){
        puts("栈空");return 0;
    }
    x=S.data[S.top];
    return 1;
}

long long convert(int x,int r){
    SeqStack Stk;
    StackInit(Stk);
    while(x){
        StackPush(Stk,x%r);
        x/=r;
    }
    long long res=0;
    int e;
    while(!StackEmpty(Stk)){
        StackPop(Stk,e);
        res=res*10+e;
    }
    return res;
}
int main(){
    int a,r;scanf("%d%d",&a,&r);
    long long b=convert(a,r);
    printf("%d(10)=%lld(%d)",a,b,r);
    return 0;
}

8569

上课上到了再补不用STL的写法

在这里插入代码片

可扩展运算符的c++代码

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

const int N=1e5+10;
stack<int> num;
stack<char> op;

void eval(){
    auto b=num.top();num.pop();
    auto a=num.top();num.pop();
    auto c=op.top();op.pop();
    int x;
    if(c=='+')x=a+b;
    else if(c=='-')x=a-b;
    else if(c=='*')x=a*b;
    else x=a/b;
    num.push(x);
}
int main()
{
    unordered_map<char,int>pr{{'+',1},{'-',1},{'*',2},{'/',2}};//运算符优先级
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        auto c=s[i];
        if(isdigit(c))//转数字
        {
            int x=0,j=i;
            while(j<s.size()&&isdigit(s[j]))
                x=x*10+s[j++]-'0';
            i=j-1;
            num.push(x);
        }
        else if(c=='(')op.push(c);
        else if(c==')')//括号直接算
        {
            while(op.top()!='(')eval();
            op.pop();
        }
        else
        {
            while(op.size()&&pr[op.top()]>=pr[c])eval();//如果当前优先级比前面的低就算
            op.push(c);//操作符入栈
        }
    }
    while(op.size())eval();//剩余计算
    printf("%d",num.top());
    return 0;
}

8564

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>

typedef struct Node{
    int data;
    struct Node *next;
}LQNode,*LinkQNode;
typedef struct{
    LQNode *front,*rear;
}LQueue,*LinkQueue;

int QueueInit(LinkQueue &Q){
    LinkQNode p;
    Q=(LinkQueue)malloc(sizeof(LQueue));//申请头尾指针节点
    p=(LinkQNode)malloc(sizeof(LQNode));//申请链队头节点
    p->next=NULL;Q->front=Q->rear=p;
    return 1;
}
void QueuePush(LinkQueue &Q,int x){
    LinkQNode p=(LinkQNode)malloc(sizeof(LQNode));
    p->data=x;
    p->next=NULL;
    Q->rear->next=p;
    Q->rear=p;
}
bool QueueEmpty(LinkQueue Q){
    if(Q->front==Q->rear)return true;
    return false;
}
int QueuePop(LinkQueue &Q,int &x){
    if(QueueEmpty(Q)){
        puts("Queue Empty!");
        return 0;
    }
    LinkQNode p=Q->front->next;
    Q->front->next=p->next;
    x=p->data;
    if(p==Q->rear)Q->rear=Q->front;//只有一个元素的话,出队后为空
    delete p;
    return 1;
}
bool GetHead(LinkQueue Q,int &head){
    if(QueueEmpty(Q))return false;
    head=Q->front->next->data;
    return true;
}
int main(){
    LinkQueue Q;
    QueueInit(Q);
    int x;
    while(~scanf("%d",&x)){
        if(x==0)break;
        QueuePush(Q,x);
    }
    while(!QueueEmpty(Q)){
        QueuePop(Q,x);
        printf("%d ",x);
    }
    return 0;
}

8565

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdbool.h>

#define MAXSIZE 1024
typedef struct{
    int data[MAXSIZE];
    int front,rear;
}SeQueue;

bool QueueInit(SeQueue &Q){
    Q.front=0;Q.rear=0;
    return 1;
}
bool QueuePush(SeQueue &Q,int x){
    if((Q.rear+1)%MAXSIZE==Q.front){
        puts("队列满");return 0;
    }
    Q.data[Q.rear]=x;
    Q.rear=(Q.rear+1)%MAXSIZE;
    return 1;
}
bool GetHead(SeQueue Q,int &x){
    if(Q.front==Q.rear){
        puts("队列空");return 0;
    }
    x=Q.data[Q.front];
    return 1;
}
bool QueueEmpty(SeQueue Q){
    if(Q.front==Q.rear)return 1;
    return 0;
}
bool QueuePop(SeQueue &Q,int &x){
    if(Q.front==Q.rear){
        puts("队列空");return 0;
    }
    x=Q.data[Q.front];
    Q.front=(Q.front+1)%MAXSIZE;
}

int main(){
    SeQueue Q;
    QueueInit(Q);
    int x;
    while(~scanf("%d",&x)){
        if(x==0)break;
        QueuePush(Q,x);
    }
    while(!QueueEmpty(Q)){
        QueuePop(Q,x);
        printf("%d ",x);
    }
    return 0;
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值