其他分享
首页 > 其他分享> > 21.12.15 学习总结

21.12.15 学习总结

作者:互联网

周一周二把栈,队列,链表看了半天,今天想办法写了两个题目(链表还在学)

 ps:每组数据后有一个空行。(难怪交7次都是格式错误)

借鉴了别人的博客,结果找不到网址了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>
typedef struct asw{
	int a[101];
	int op;
	int ed;
}asd;
void start(asd *q){
	q->op=0;q->ed=0;
}
void add(asd *q,int e){
	q->a[q->ed++]=e;
}
void insallhead(asd *q){
	if(q->op==q->ed)printf("this is empty!\n");
	else printf("%d\n",q->a[q->op++]);
}
void stop(asd *q){
	while(q->op!=q->ed)printf("%d\n",q->a[q->op++]);
}
int main()
{
	asd *q=(asd*)malloc(sizeof(asd));
	int t;
	while(scanf("%d",&t)!=EOF){
		start(q);
		while(t--){
			char x[15];
			int e;
			getchar();
			scanf("%s",x);
			if(strcmp(x, "enqueue") == 0) {
				scanf("%d", &e);
				add(q, e);
			}
			
			if(strcmp(x, "dequeue") == 0) {
				insallhead(q);
			}
			
			if(strcmp(x, "query") == 0) {
				if(q->op == q->ed)
					printf("this is empty!\n");
				else
					stop(q);
			}
		}
		printf("\n");//每组数据后跟一个空行,吐了
	}
    return 0;
}

 ps:只要出现一次空栈后pop就在数据结束后输出error,有且仅输出一次

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
int dl1=0,z1=0;
typedef struct asw{
	int num[1001];
	int op,ed;
}duilie;//队列:dl
typedef struct asd{
	int num[1001];
	int tipid;
}zhan;//栈:z
void opendl(duilie *dl){//初始化
	dl->op=dl->ed=0;
}
void openz(zhan *z){
	z->tipid=0;
}
void adddl(duilie *dl,int e){//push
	dl->num[dl->ed++]=e;
}
void addz(zhan *z,int e){
	z->num[z->tipid++]=e;
}
void zyydl(duilie *dl){//pop
	if(dl->op>=dl->ed)dl1=1;
	else if(dl->op<dl->ed)dl->op++;
}
void zyyz(zhan *z){//最后输出
	if(z->tipid<=0)z1=1;
	else if(z->tipid>0)z->tipid--;
}
void putsdl(duilie *dl){
	if(dl1==1)printf("error\n");
	else{
		for(int i=dl->op;i<dl->ed;i++)printf("%d ",dl->num[i]);
		printf("\n");
	}
}
void putsz(zhan *z){
	if(z1==1)printf("error\n");
	else {
		for(int i=0;i<z->tipid;i++)printf("%d ",z->num[i]);
		printf("\n");
	}
}
int main(){
	int m;
	scanf("%d",&m);
	for(int ii=0;ii<m;ii++){
		int n;
		dl1=0;z1=0;
		scanf("%d",&n);
		duilie *dl=(duilie*)malloc(sizeof(duilie));
		zhan *z=(zhan*)malloc(sizeof(duilie));
		opendl(dl);openz(z);
		for(int i=0;i<n;i++){
			getchar();
			char xx[100];
			scanf("%s",xx);
			if(strcmp(xx,"push")==0){
				int e;
				scanf("%d",&e);
				adddl(dl,e);
				addz(z,e);
			}
			else if(strcmp(xx,"pop")==0){
				zyydl(dl);zyyz(z);
			}
		}
		putsdl(dl);putsz(z);
	}
    return 0;
}

(我怎么用的都是结构体数组?)

标签:总结,dl,15,int,void,printf,21.12,include,op
来源: https://blog.csdn.net/m0_63987562/article/details/121963974