我的创作纪念日

机缘

提示:可以和大家分享最初成为创作者的初心

  1. 日常学习过程中的记录
  2. 记录自己学过的代码
  3. 帮助有需要的人

收获

  1. 获得了正向的反馈,如赞、评论、阅读量

日常

  1. 创作是已经是你生活的一部分了
  2. 有限的精力下,都在工作学习

成就

  1. 没有认为自己写过最好的代码,只有最烂的
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    typedef struct vector{
    	int v;
    	vector *next;
    }vector;
    vector *getn(int val){
    	vector *p=(vector *)malloc(sizeof(vector));
    	p->v=val;
    	p->next=NULL;
    	return p;
    }
    typedef struct linklist{
    	vector head,*tail;
    }linklist;
    linklist *initl(){
    	linklist *l=(linklist *)malloc(sizeof(linklist));
    	l->head.next=NULL;
    	l->tail=&(l->head);
    	return l;
    }
    void clearv(linklist *l){
    	vector *p=l->head.next,*q;
    	while(p){
    		q=p->next;
    		free(p);
    		p=q;
    	}
    	free(l);
    	return ;
    }
    typedef struct queue{
    	linklist *data;
    	int count;
    }queue;
    int emptyl(linklist *l){
    	return l->head.next==NULL;
    }
    int frontlist(linklist *l){
    	if(emptyl(l))return 0;
    	return l->head.next->v;
    }
    int inserttail(linklist *l,int val){
    	vector *node=getn(val);
    	l->tail->next=node;
    	l->tail=node;
    	return 1;
    }
    int eraseHead(linklist *l){
    	if(emptyl(l))return 0;
    	vector *p=l->head.next;
    	l->head.next=l->head.next->next;
    	if(p==l->tail) l->tail=&(l->head);
    	free(p);
    	return 1;
    }
    int empty(queue *q){
    	return q->count==0;
    }
    queue *iniq(){
    	queue *q=(queue *)malloc(sizeof(queue));
    	q->data=initl();
    	q->count=0;
    	return q;
    }
    int front(queue *q){
    	if(empty(q))return 0;
    	return frontlist(q->data);
    }
    int push(queue *q,int val){
    	inserttail(q->data,val);
    	q->count+=1;
    	return 1;
    }
    int pop(queue *q){
    	eraseHead(q->data);
    	q->count-=1;
    	return 1;
    }
    
    void clear(queue *q){
    	if(q==NULL)return;
    	clearv(q->data);
    	free(q);
    	return;
    }
    void outputqueue(queue *q){
    	printf("quueue:");
    	vector *p=q->data->head.next;
    	for(int i=0;i<q->count;i++,p=p->next){
    		printf("%4d",p->v);
    	}
    	printf("\n\n");
    	return;
    }
    int main(){
    		srand(time(0));
    	#define MAX_OP 10
    	queue *q=iniq();
    	for(int i=0;i<MAX_OP;i++){
    		int pos=rand()%5;int val=rand()%100;
    		switch(pos){
    		case 0:
    		case 1:
    			printf("front of queue:%d\n",front(q));
    			pop(q);
    			break;
    		case 2:
    		case 3:
    		case 4:
    			printf("push %d to queue\n",val);
    			push(q,val);
    			break;
    		}
    		outputqueue(q);
    	}
    	clear(q);
    	return 0;
    }

     


憧憬

希望未来的路,能继续负重前行。其作始也简,其将毕也必巨。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值