zoukankan      html  css  js  c++  java
  • 不带头节点的链式存储队列基本操作

    #include<stdio.h>
    #include<malloc.h>
    typedef char ElemType;
    typedef struct LinkNode
    {
    	ElemType data;
    	struct LinkNode *next;
    }LinkNode;
    typedef struct
    {
    	struct LinkNode *front,*rear;
    }LinkQueue;
    void InitQueue(LinkQueue &q)
    {
    	q.rear=q.front=NULL;		//初始化队列
    }
    bool IsEmpty(LinkQueue q)
    {
    	if(q.front==NULL)			//判空条件:头指针是否为空
    		return true;
    	else
    		return false;
    }
    void EnQueue(LinkQueue &q,ElemType e)
    {
    	LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
    	s->data=e;
    	s->next=NULL;
    	if(IsEmpty(q))		//如果队列为空,则将头指针尾指针同时指向新插入的节点
    	{
    		q.front=s;
    		q.rear=s;
    		return ;
    	}
    	q.rear->next=s;
    	q.rear=s;
    }
    bool DeQueue(LinkQueue &q,ElemType &e)
    {
    	if(q.front==NULL)
    		return false;
    	LinkNode *p=q.front;	//p指向头指针
    	e=p->data;
    	q.front=p->next;		//队列的头指针指向p指针的下一节点
    	if(q.rear==p)			//此次是最后一个节点,队列置空
    		q.rear=q.front=NULL;
    	free(p);
    	return true;
    }
    bool HeadQueue(LinkQueue q,ElemType &e)
    {
    	if(q.front==NULL)
    		return e='?';
    	e=q.front->data;
    	return true;
    }
    void main()
    {
    	LinkQueue q;
    	InitQueue(q);
    	printf("The Queue is %s
    ",IsEmpty(q)?"Empty":"UnEmpty");
    	ElemType e;
    	HeadQueue(q,e);
    	printf("Head_Queue is %c
    ",e);
    	EnQueue(q,'a');
    	EnQueue(q,'b');
    	EnQueue(q,'c');
    	HeadQueue(q,e);
    	printf("Head_Queue is %c
    ",e);
    	DeQueue(q,e);
    	HeadQueue(q,e);
    	printf("Head_Queue is %c
    ",e);
    	DeQueue(q,e);
    	HeadQueue(q,e);
    	printf("Head_Queue is %c
    ",e);
    	DeQueue(q,e);
    	HeadQueue(q,e);
    	printf("Head_Queue is %c
    ",e);
    	printf("The Queue is %s
    ",IsEmpty(q)?"Empty":"UnEmpty");
    }
    

      

  • 相关阅读:
    Mysql知识总结
    Unity3D UGUI 自动调节大小
    关于 Rijndael 加密
    配置java环境
    二叉查找树
    序列化和反序列化
    关于文件保存/关闭时报错:文件正由另一进程使用,因此该进程无法访问此文件。
    关于Unity中NGUI图片精灵响应鼠标的方法
    用人类的话来描述 里氏转换
    C#中string的相关方法
  • 原文地址:https://www.cnblogs.com/-slz-2/p/13205065.html
Copyright © 2011-2022 走看看