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=(LinkNode*)malloc(sizeof(LinkNode));
    	q.front->next=NULL;
    }
    bool IsEmpty(LinkQueue q)
    {
    	if(q.front==q.rear)
    		return true;
    	else
    		return false;
    }
    void EnQueue(LinkQueue &q,ElemType e)
    {
    	LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
    	s->data=e;
    	s->next=NULL;
    	q.rear->next=s;
    	q.rear=s;
    }
    bool DeQueue(LinkQueue &q,ElemType &e)
    {
    	if(q.rear==q.front)
    		return false;
    	LinkNode *p=q.front->next;
    	e=p->data;
    	q.front->next=p->next;
    	if(q.rear==p)
    		q.front=q.rear;
    	free(p);
    	return true;
    }
    bool HeadQueue(LinkQueue q,ElemType &e)
    {
    	if(q.front==q.rear)
    		return e='?';
    	e=q.front->next->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");
    }
    

      

  • 相关阅读:
    如何构建积木式Web应用
    ASP.NET 2.0 异步页面原理浅析 [1] [原]
    HybridDictionary 类
    datagrid自定义
    认识.NET的集合
    织梦 10060
    java.io.FileNotFoundException: E:\temp (拒绝访问。)
    引用与对象实例化
    C#中为DataGrid添加下拉列表框
    C#中使用指针
  • 原文地址:https://www.cnblogs.com/-slz-2/p/13201671.html
Copyright © 2011-2022 走看看