队列操作【记录】

来源:互联网 发布:淘宝客服奖罚 编辑:程序博客网 时间:2024/04/30 02:08
#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 inttypedef 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 hereLinkQueue 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