2.3,2.4数据结构瑞格栈与递归、队列

nefu瑞格栈与递归、队列

–题目来源-nefu瑞格

栈与递归

4227

在这里插入图片描述

#include<stdio.h>

// write your code here
int sum(int a[],int n)
{
    if(n==1)
        return a[0];
    else
        return sum(a,n-1)+a[n-1];
}

int main()
{
	int a[6],i;
	for (i=0;i<6;i++)
		scanf("%d", a+i);
	printf("%d\n", sum(a, 6));
	return 0;
}

4226

在这里插入图片描述

#include<stdio.h>

// write your code here
int max(int a[],int n)
{
    if(n==2)
    {
        if(a[0]>a[1])
            return a[0];
        else
            return a[1];
    }
    else
    {
        if(max(a,n-1)>a[n-1])
            return max(a,n-1);
        else
            return a[n-1];
    }

}
int main()
{
	int a[6],i;
	for (i=0;i<6;i++)
		scanf("%d", a+i);
	printf("%d\n", max(a, 6));
	return 0;
}

4228

在这里插入图片描述

#include <stdio.h>

// write your code here
float average(float a[],int n)
{
    if(n==1)
        return a[0];
    else
        return (average(a,n-1)*(n-1)+a[n-1])/n;
}

int main()
{
	float a[6];
	int i;
	for (i=0;i<6;i++)
		scanf("%f", a+i);
	printf("%.2f\n", average(a, 6));
	return 0;
}

队列

4236

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define TRUE 1
#define FALSE 0

typedef int Status;

typedef struct Node {
        int data;
        struct Node *next;
}Node, *pNode;

pNode p;

void InitQueue()
{
	p = (struct Node *)malloc(sizeof(struct Node));
	p->next = p;
}

void EnQueue(const int x)
{
    // write your code here
    pNode r,t=p;
    r = (struct Node *)malloc(sizeof(struct Node));
    r->data=x;
    while(t->next!=p)
    {
        t=t->next;
    }
    t->next=r;
    r->next=p;

}

const Status isEmpty()
{
    // write your code here
    if(p->next==p)
        return 1;
    else
        return 0;

}

Status DeQueue(int *x)
{
    // write your code here
    pNode r=p,t=p;
    while(r->next!=p)
        r=r->next;
    *x=p->data;
    p=p->next;
    free(t);
    r->next=p;
}

const void Print()
{
	Node *t = p->next;
	while (t->next!=p->next) {
		printf("%d ", t->data);
		t = t->next;
	}
	printf("\n");
}

int main()
{
	InitQueue();
	int x;
	EnQueue(1);
	EnQueue(2);
	EnQueue(3);
	DeQueue(&x);
	EnQueue(4);
	DeQueue(&x);
	DeQueue(&x);
	DeQueue(&x);
	EnQueue(5);
	Print();
	return 0;
}

4915

在这里插入图片描述

#include<iostream>
using namespace std;

struct Node {
    int data;
	Node *next;
};

class LinkList {
private:
	Node *first;
public:
	LinkList();
	void Insert(const int &x);
	Node *First();
	int Length(Node *n);
};

LinkList::LinkList()
{
	first = new Node();
	first->next = NULL;
}

void LinkList::Insert(const int &x)
{
	Node *t = first;
	while (t->next!=NULL)
		t = t->next;
	Node *n = new Node();
	n->data = x;
	n->next = t->next;
	t->next = n;
}

Node *LinkList::First()
{
	return first;
}

//write your code here
int LinkList::Length(Node *n)
{
    if(n==NULL)
        return 0;
    else
        return this->Length(n->next)+1;
}


int main()
{
	LinkList l;
	int x;
	cin >> x;
	while (x!=-1) {
		l.Insert(x);
		cin >> x;
	}
	//write your code here
	Node *t;
	t=l.First();
    printf("%d",l.Length(t->next));
	return 0;
}

4907

在这里插入图片描述
B

4872

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>

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

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


int InitQueue(LinkQueue &Q)
{
	Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
	if( !Q.front) exit(0);
	Q.front->next = NULL;
	return 1;
}

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

int DeQueue(LinkQueue &Q,int &e)
{
    QueuePtr p;
    p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    delete p;
}
int main()
{
	int m,n;
	int i,e;
	LinkQueue Q;
	InitQueue(Q);
	scanf("%d",&n);
	for(i = 0 ; i < n ; ++i)
	{
		scanf("%d",&e);
		EnQueue(Q,e);
	}
	scanf("%d",&m);
	for(i = 0 ; i < m ; ++i)
	{
		DeQueue(Q,e);
		printf("%d ",e);
	}
	printf("\n");
	return 0;
}

4219

在这里插入图片描述
A

4220

在这里插入图片描述
B

4235

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

#define TRUE 1
#define FALSE 0

typedef int Status;

int *q;
int maxSize;
int front;
int rear;
int tag;

const Status isFull()
{
    // write your code here
    if(front==rear&&tag==1)
        return 1;
    else
        return 0;
}

const Status isEmpty()
{
    // write your code here
    if(front==rear&&tag==0)
        return 1;
    else
        return 0;

}

void InitStack(int sz)
{
    maxSize = sz;
	q = (int *)malloc(maxSize*sizeof(int));
	front = 0;
	rear = 0;
	tag = 0;
}

void output()
{
    int i;
	for (i=0;i<maxSize;i++)
		printf("%d ", q[i]);
}

Status EnQueue(const int x)
{
    // write your code here
    if(isFull())
        return 0;
    else
    {
        q[rear]=x;
        rear=(rear+1)%maxSize;
        if(rear==front)
            tag=1;
        return 1;
    }
}

Status DeQueue(int x)
{
    // write your code here
    if(isEmpty())
        return 0;
    else
    {
        x=q[front];
        q[front]=0;
        front=(front+1)%maxSize;
        if(rear==front)
            tag=0;
        return 1;
    }

}

int main()
{
	InitStack(4);
	int m;
	EnQueue(1);
	DeQueue(m);
	DeQueue(m);
	EnQueue(2);
	EnQueue(3);
	EnQueue(4);
	EnQueue(5);
	DeQueue(m);
	DeQueue(m);
	EnQueue(6);
	output();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值