数据结构::链队列
#include <iostream> #include<bits/stdc++.h> using namespace std; #define MaxSize 10 typedef int dataType; //定义链队列结点结构 typedef struct Node{ struct Node *next; dataType data; }LinkNode; //定义链队列结构 typedef struct { LinkNode *front; //指向头部结点,出队 LinkNode *rear; //指向尾部结点,入队 }LQueue; typedef LQueue *pLQueue; //定义一个指向链队列的指针 //初始化链队列 pLQueue InitLQueue() { pLQueue plq=(LQueue*)malloc(sizeof(LQueue)); plq->front=plq->rear=NULL; return plq; } //入队 void EnLQueue(pLQueue plq,dataType value) { LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode)); p->data=value; p->next=NULL; if (plq->rear==NULL) //处理空队列 { plq->rear=p; plq->front=p; } else{ plq->rear->next=p; plq->rear=p; } } //自动创建一个链队列(不使用头结点) pLQueue CreateLQueue() { pLQueue plq=InitLQueue(); int i=1,num=9; while (i<=9) { cout<<i<<" "; EnLQueue(plq,i); i++; } cout<<endl; return plq; } //出队 void DeLQueue(pLQueue plq) { if (plq->front==NULL) { cout<<"Empty"<<endl; return; } cout<<plq->front->data<<endl; LinkNode *temp=plq->front; plq->front=temp->next; free(temp); } //全部出队 void AllDeLQueue(pLQueue plq) { while (plq->front!=NULL) { DeLQueue(plq); } cout<<"Empty"<<endl; return; } //释放队列 void FreeLQueue(pLQueue plq) { while (plq->front!=NULL) { LinkNode *temp=plq->front->next; plq->front->next=temp->next; free(temp); } free(plq); } int main() { pLQueue plq=CreateLQueue(); AllDeLQueue(plq); FreeLQueue(plq); system("pause"); }
原文链接: https://www.cnblogs.com/a-hua/p/13094334.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/354518
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!