• 数据结构之环形队列


    数据结构之环形队列

    #include <stdio.h>
    #include <stdlib.h>
    typedef char ElemType;
    typedef struct qnode
    {	
    	ElemType data;
    	struct qnode *next;
    } DataNode;	
    typedef struct		
    {	
    	DataNode *front;
    	DataNode *rear;
    } LinkQuNode;			
    void InitQueue(LinkQuNode *&q)
    {	
    	q=(LinkQuNode *)malloc(sizeof(LinkQuNode));
    	q->front=q->rear=NULL;
    }
    void DestroyQueue(LinkQuNode *&q)
    {
    	DataNode *pre=q->front,*p;//pre指向队首结点 
    	if (p!=NULL)		//p指向结点pre的后继结点 
    	{	p=pre->next;
    		while (p!=NULL)//p不空循环 
    		{	free(pre);//释放pre结点 
    			pre=p;p=p->next;//pre,p同步后移 
    		}
    	}
    	free(pre);//释放最后一个结点 
    	free(q);	//释放	lian'dui		
    } 
    bool QueueEmpty(LinkQuNode *q)
    {
    	return(q->rear==NULL);
    }
    void enQueue(LinkQuNode *&q,ElemType e)
    {	DataNode *p;
    	p=(DataNode *)malloc(sizeof(DataNode));//创建新结点 
    	p->data=e;
    	p->next=NULL;
    	if (q->rear==NULL)		//若链队为空,则新结点是队首结点又是队尾结点
    		q->front=q->rear=p;
    	else
    	{	q->rear->next=p;	//将p结点链到队尾,并将rear指向它
    		q->rear=p;
    	}
    }
    bool deQueue(LinkQuNode *&q/*,ElemType &e*/)
    {	DataNode *t;
    	if (q->rear==NULL)		//队列为空
    		return false;
    	t=q->front;				//t指向第一个数据结点
    	if (q->front==q->rear)  //队列中只有一个结点时
    		q->front=q->rear=NULL;
    	else					//队列中有多个结点时
    		q->front=q->front->next;
    	printf("%c
    ",t->data); 
    	//e=t->data;
    	free(t);
    	return true;
    }
    int main()
    {
    	LinkQuNode *q;
    
        InitQueue(q);
        printf("初始化完毕
    ");
        if(QueueEmpty(q))
        {
        	printf("链队为空
    "); 
    	}
    	else
    	{
    		printf("链队不为空
    "); 
    	}
    	enQueue(q,'a');
    	enQueue(q,'b');
    	enQueue(q,'c');
    	printf("进队完毕
    "); 
    	deQueue(q);
    	printf("a已经出队
    "); 
    	enQueue(q,'d');
    	enQueue(q,'e');
    	enQueue(q,'f');
    	deQueue(q);
    	deQueue(q);
    	deQueue(q);
    	deQueue(q);
    	deQueue(q);
    	DestroyQueue(q);
    	return 0;   
     } 
    
  • 相关阅读:
    条形码校验码生成
    js 模仿块级作用域(私有作用域)、私有变量
    js 闭包
    js 继承
    javascript 创建对象
    jQuery.noConflict() 函数
    C#对话框-打开和保存对话框(转)
    String.format()的用法
    转:WPF中ListBox的创建和多种绑定用法
    在wpf或winform关闭子窗口或对子窗口进行某个操作后刷新父窗口
  • 原文地址:https://www.cnblogs.com/AmosAlbert/p/12832343.html
Copyright © 2020-2023  润新知