数据结构第四章程序题3

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define DataType int


struct SeqQueue 
{
	int MAXNUM;  
	int f, r;
	DataType *q;
};
typedef  struct SeqQueue *PSeqQueue;

PSeqQueue createEmptyQueue_seq(int m)
{
	PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
	paqu->f = 0;
	paqu->r = 0;
	if (paqu != NULL) 
	{
		paqu->q = (DataType*)malloc(sizeof(DataType) * m);
		if (paqu->q) 
		{
			paqu->MAXNUM = m;
			return (paqu);
		}
		else free(paqu);
	}
	printf("Out of space!!\n");    	
	return NULL;
}

void enQueue_seq(PSeqQueue paqu, DataType x) 
{
	if ((paqu->r + 1) % paqu->MAXNUM == paqu->f)  
		printf("Full queue.\n");
	else 
	{
		paqu->q[paqu->r] = x;
		paqu->r = (paqu->r + 1) % paqu->MAXNUM;
	}
}

void printf_seq(PSeqQueue paqu)
{
	for (int i = paqu->f;i < paqu->r;i++)
	{
		printf("%d ", paqu->q[i]);
	}
}

int main()
{
	PSeqQueue paqu;
	int i;
	scanf("%d", &i);
	paqu = createEmptyQueue_seq(i+1);
	while (i--)
	{
		int x;
		scanf("%d", &x);
		enQueue_seq(paqu, x);
	}
	printf_seq(paqu);
/*输入3 1 2 3
输出为 1 2 3
*/
}

第一个题不多说了,水题,只要看懂了队列就行,懂的都懂。

以下是第二题

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define DataType int

struct SeqQueue
{
	int MAXNUM;
	int f, r;
	DataType* q;
};
typedef  struct SeqQueue* PSeqQueue;
struct SeqStack
{
	int MAXNUM, t;
	DataType* s;
};
typedef struct SeqStack* PSeqStack;

PSeqQueue createEmptyQueue_seq(int m)
{
	PSeqQueue paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
	paqu->f = 0;
	paqu->r = 0;
	if (paqu != NULL)
	{
		paqu->q = (DataType*)malloc(sizeof(DataType) * m);
		if (paqu->q)
		{
			paqu->MAXNUM = m;
			return (paqu);
		}
		else free(paqu);
	}
	printf("Out of space!!\n");
	return NULL;
}

void enQueue_seq(PSeqQueue paqu, DataType x)
{
	if ((paqu->r + 1) % paqu->MAXNUM == paqu->f)
		printf("Full queue.\n");
	else
	{
		paqu->q[paqu->r] = x;
		paqu->r = (paqu->r + 1) % paqu->MAXNUM;
	}
}

void printf_seq(PSeqQueue paqu)
{
	for (int i = paqu->f;i < paqu->r;i++)
	{
		printf("%d ", paqu->q[i]);
	}
}


PSeqStack createEmptyStack_seq(int m)
{
	PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
	if (pastack != NULL)
	{
		pastack->s = (DataType*)malloc(sizeof(DataType) * m);
		if (pastack->s)
		{
			pastack->MAXNUM = m;
			pastack->t = -1;
			return pastack;
		}
		else
			free(pastack);
	}
	else
	{
		printf("Out of space!\n");
		return NULL;
	}
}

void push_seq(PSeqStack pastack, DataType x)
{
	if (pastack->t >= pastack->MAXNUM - 1)
		printf("Overflow! \n");
	else
	{
		pastack->t = pastack->t + 1;
		pastack->s[pastack->t] = x;
	}
}
void pop_seq(PSeqStack pastack)
{
	if (pastack->t == -1)
		printf("Underflow!\n");
	else
		pastack->t = pastack->t - 1;
}
DataType top_seq(PSeqStack pastack)
{
	if (pastack->t == -1)
		printf("It is empty!\n");
	else
		return (pastack->s[pastack->t]);
}


void stackToQueue(PSeqStack pastack, PSeqQueue paqu)
{
	while (pastack->t != -1)
	{
		enQueue_seq(paqu, top_seq(pastack));
		pop_seq(pastack);
	}
}
int main()
{
	PSeqStack pastack;
	PSeqQueue paqu;
	int i;
	scanf("%d", &i);
	pastack = createEmptyStack_seq(i + 1);
	paqu = createEmptyQueue_seq(i + 1);
	while (i--)
	{
		int x;
		scanf("%d", &x);
		push_seq(pastack, x);
	}
	stackToQueue(pastack, paqu);
	printf_seq(paqu);
	/*输入 3 1 2 3
	输出 3 2 1
	*/
}


途中遇到了error C4335: 检测到 Mac 文件格式: 请将源文件转换为 DOS 格式或 UNIX 格式,只要复制到别的word文件里,记住一定要把原来的代码删除,再复制回去,不然会没有效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值