zoukankan      html  css  js  c++  java
  • "双队列"学习

    image

    image

    image

    1.输入限制性双队列:

    代码:InLimitDeQueue.c

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct _queue
    {
    	int data;
    	struct _queue *next;
    }QUEUE;
    
    QUEUE * rear = NULL;
    QUEUE * front = NULL;
    
    //输入限制型双队列
    int InQueue(int value)
    {
    	QUEUE * q = (QUEUE *)malloc(sizeof(QUEUE));
    	if(q == NULL) return 0;
    	q->data = value;
    	q->next = NULL;
    	if(front == NULL)
    		front = q;
    	else
    		rear->next = q;
    	rear = q;
    	return 1;
    }
    
    int OutQueueByFront(int *value)
    {
    	QUEUE * p = NULL;
    	if(front == NULL)
    		return 0;
    	p = front;
    	front = front->next;
    	*value = p->data;
    	free(p);
    	return 1;
    }
    
    int OutQueueByRear(int *value)
    {
    	QUEUE *p = NULL;
    	if(rear == NULL)
    		return 0;
    	//只有一个数据
    	if(rear == front)
    	{
    		*value = rear->data;
    		free(rear);
    		rear = NULL;
    		front = NULL;
    	}else{
    		p = front;
    		//使p指向最后一个的前面一个
    		while(p->next != rear)
    			p = p->next;
    		*value = rear->data;
    		free(rear);
    		rear = p;
    		rear->next = NULL;
    	}
    	return 1;
    }
    
    void main()
    {
    	int res,i,arr[5] = {1,2,3,4,5};
    	for(i = 0;i<5;i++)
    		InQueue(arr[i]);
    	while(1)
    	{
    		printf("1.从队头取出;2.从队尾取出;3.退出:");
    		scanf("%d",&res);
    		//清空输入缓冲区
    		fflush(stdin);
    		if(res == 1)
    		{
    			if(OutQueueByFront(&res) == 1)
    				printf("取出的值为:%d\n",res);
    			else
    				printf("队列为空!\n");
    
    		}else if(res == 2)
    		{
    			if(OutQueueByRear(&res) == 1)
    				printf("取出的值为:%d\n",res);
    			else
    				printf("队列为空!\n");
    		}else if(res == 3)
    		{
    			exit(0);
    		}
    	}
    }

    运行效果:

    存入队列的元素为1,2,3,4,5

    image

    2.输出限制性双队列:

    代码:OutLimitDeQueue.c

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct _queue
    {
    	int data;
    	struct _queue *next;
    }QUEUE;
    
    QUEUE * rear = NULL;
    QUEUE * front = NULL;
    
    //输出限制型双队列
    int OutQueue(int *value)
    {
    	QUEUE *p = NULL;
    	if(front == NULL)
    		return 0;
    	p = front;
    	front = front->next;
    	*value = p->data;
    	free(p);
    	return 1;
    }
    
    int InQueueByRear(int value)
    {
    	QUEUE *q = (QUEUE *)malloc(sizeof(QUEUE));
    	if(q == NULL) return 0;
    	q->data = value;
    	q->next = NULL;
    	if(rear == NULL)
    		front = q;
    	else 
    		rear->next = q;
    	rear = q;
    	return 1;
    }
    
    int InQueueByFront(int value)
    {
    	QUEUE *q = (QUEUE *)malloc(sizeof(QUEUE));
    	if(q == NULL)return 0;
    	q->data = value;
    	q->next = front;
    	front = q;
    	if(rear == NULL)
    		rear = q;
    	return 1;
    }
    
    
    void PrintQueue()
    {
    	QUEUE *p = front;
    	while(p)
    	{
    		printf("%5d",p->data);
    		p = p->next;
    	}
    	printf("\n");
    }
    
    void main()
    {
    	int res;
    	while(1)
    	{
    		printf("1.从队头存入;2.从队尾存入;3.退出");
    		scanf("%d",&res);
    		fflush(stdin);
    		if(res == 1)
    		{
    			printf("请输入要存入的值:");
    			scanf("%d",&res);
    			fflush(stdin);
    			if(InQueueByFront(res))
    			{
    				PrintQueue();
    			}else
    				printf("存入失败\n");
    		}else if(res == 2)
    		{
    			printf("请输入要存入的值:");
    			scanf("%d",&res);
    			fflush(stdin);
    			if(InQueueByRear(res))
    			{
    				PrintQueue();
    			}else
    				printf("存入失败\n");
    		}else if(res == 3)
    			break;
    	}
    }

    运行效果:

    image

  • 相关阅读:
    通过gridview隐藏的一列的值,来设置该行的背景颜色。
    修改js文件后,浏览器不会立即发现js文件更新,连接状态是200 (from cache)缓存
    getDate()显示格式
    设置gridview自动分页的页码不显示
    数据库字段类型,如果是bool类型,数据库中用bit
    给dropdownlist手动添加一列"请选择"(不是从数据库中读取的数据)
    数据库中使用关键词时注意
    获取服务器控件的id时,遇到使用到模板列取不到的问题
    页面自动刷新代码
    启用IIS的Gzip压缩功能
  • 原文地址:https://www.cnblogs.com/shenerguang/p/2333996.html
Copyright © 2011-2022 走看看