编程实现链式队列

来源:互联网 发布:医疗器械销售数据库 编辑:程序博客网 时间:2024/05/06 04:21
#include<iostream>using namespace std;typedef struct Node{char a;struct Node *next;}Lnode;typedef struct Queue{Lnode *front;Lnode *rear;}LQueue;//初始化,实参传入a的地址void Initiate(LQueue *a){a->front=NULL;    a->rear=NULL;}//添加元素void AppendQueue(LQueue *a,char b){Lnode * p;p=(Lnode*)malloc(sizeof(Lnode));p->a=b;p->next=NULL;if(a->front==NULL)  a->front=a->rear=p;else{a->rear->next=p;a->rear=p;}}int QueueNotEmpty(LQueue *Q){if(Q->front==NULL )return 0;else return 1;}//取得队列第一个元素,并删除char QueueDelete(LQueue *Q){if(Q->front==NULL) {cout<<"the Queue havent elements"<<endl;    return NULL;}Lnode *p;char data;p=Q->front;data=p->a;Q->front=Q->front->next;if(Q->front==NULL)  Q->rear=NULL;free(p);return data;}//取得队列第一个元素char QueueGet(LQueue *Q){if(Q->front==NULL) {cout<<"the Queue is empty"<<endl;return NULL;}else{return Q->front->a;}}//销毁队列void Destroy(LQueue *Q){Lnode *p;if(Q->front==NULL)  return ;while(Q->front!=NULL){p=Q->front;Q->front=Q->front->next;free(p);}}void main(){LQueue Q;char a;Initiate(&Q);//初始化AppendQueue(&Q,'p');AppendQueue(&Q,'h');AppendQueue(&Q,'a');AppendQueue(&Q,'j');a=QueueDelete(&Q);//取队头元素并删除cout<<a<<endl;cout<<QueueGet(&Q)<<endl;//取得队友元素Destroy(&Q);//销毁队列if(QueueNotEmpty) cout<<"destroy sucessfuly "<<endl;else cout<<"destroy unsucessfully"<<endl;system("pause");}

0 0
原创粉丝点击