队列操作【记录】

#include <iostream>
#include<malloc.h>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define Status int
#define OVERFLOW 0
#define ElemType int
typedef struct Node{
	int data;
	Node* next;
	}Node;
typedef struct Node* QueuePtr;
typedef struct LinkQueue {
	
	QueuePtr front;
	QueuePtr rear;
};
//队列初始化
Status InitQueue(LinkQueue &Q)//引用 给变量取一个别名
{

	Q.front=Q.rear=(QueuePtr)malloc(sizeof(Node));//malloc reurn void ptr so we need transform it!!
//	Q->rear=(QueuePtr)malloc(sizeof(Node));//malloc reurn void ptr so we need transform it!!
    if(!(Q.front)) cout<<0;
    
	Q.front->next=NULL;//指针不能悬空
	return OK;
	
}
//队列销毁操作
Status DestroyQueue(LinkQueue &Q)
{
	while(Q.front)
	{
		Q.rear=Q.front->next;
		free(Q.front);
		Q.front=Q.rear;
		
	}
	return OK;
	
}
//队列插入操作
Status InsertQueue(LinkQueue &Q,ElemType e)
{
	QueuePtr p;
	p=(QueuePtr)malloc(sizeof(Node));
	if(!p) exit(OVERFLOW);
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	
	return OK;
	
}
//队列的删除头元素操作
Status DeQueue(LinkQueue &Q,ElemType &e)
{
	
  if(Q.front==Q.rear) return ERROR;//如果只剩头尾节点 再进行删除操作出错!!
	QueuePtr p;
	p=Q.front->next;
	Q.front->next=p->next;
	e=p->data;//得到删除点的数值
	if(Q.rear==p) Q.rear=Q.front;//如果只剩一个节点 删除后尾巴要重定位!!
	free(p);//释放内存节点
	return OK;
}

int main() {
	// your code goes here
	LinkQueue Q;
	ElemType e;
   // Q->front=NULL;
  //  Q->rear=NULL;
	
     InitQueue(Q);//引用的用法
    
     InsertQueue(Q,12);
     DeQueue(Q,e);
	cout<<(Q.front)->data;
	return 0;
}

 

/*******************************************

int *a = &i;//这里a是一个指针,它指向变量i

int &b = i;//这里b是一个引用,它是变量i的引用,引用是什么?它的本质是什么?下面会具体讲述

int * &c = a;//这里c是一个引用,它是指针a的引用

int & *d;//这里d是一个指针,它指向引用,但引用不是实体,所以这是错误的

不能简单的说引用=指针,虽然两者有相同的地方,但是也有不同的地方!比如 引用必须要初始化一次,之后不能改变,引用不能为空等.

和指针不同,一旦引用和对象绑定,它无法再被重新指向其他对象。引用本身不是一个对象(它没有标识;当试图获得引用的地址时,你将的到它的指示物的地址;记住:引用就是它的指示物 )。从某种意义上来说,引用类似 int* const p 这样的const指针.

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值