当前位置:网站首页>Implementation of queue (sequential storage, chain storage)
Implementation of queue (sequential storage, chain storage)
2022-07-27 08:41:00 【Madness makes freedom】
/**
Basic operations of queues :
Queue CreateQueue (int Maxsize): Initialize a new empty queue ;
bool IsFull(Queue Q): Judge the queue Q Whether is full ;
bool AddQ(Queue Q,ElementType X): Put the element X Push into the queue Q;
bool IsEmpty(Queue Q): Determines if the queue is empty ;
ElementType DelectQ(Queue Q): Delete and return the queue header element ;
*/
/**
1) Circular queue , When Front==Rear when , Indicates that the queue is empty ;
When (Rear+1)%Maxsize==Front when , The queue is full ;( That is, when the circular queue is full, there is a position that is empty )
*/
/**
Basic operations of queues :
Queue CreateQueue (int Maxsize): Initialize a new empty queue ;
bool IsFull(Queue Q): Judge the queue Q Whether is full ;
bool AddQ(Queue Q,ElementType X): Put the element X Push into the queue Q;
bool IsEmpty(Queue Q): Determines if the queue is empty ;
ElementType DelectQ(Queue Q): Delete and return the queue header element ;
*/
/**
1) Circular queue , When Front==Rear when , Indicates that the queue is empty ;
When (Rear+1)%Maxsize==Front when , The queue is full ;( That is, when the circular queue is full, there is a position that is empty )
*/
/**
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define error 1e9
const int Maxsize = 100;
typedef int ElementType;
typedef int Position;
typedef struct QNode * PtrToQNode;
struct QNode
{
ElementType *Data;
Position Front,Rear;
int Maxsize;
};
typedef PtrToQNode Queue;
Queue CreateQueue (int Maxsize);
bool IsFull(Queue Q);
bool AddQ(Queue Q,ElementType X);
bool IsEmpty(Queue Q);
ElementType DelectQ(Queue Q);
int main()
{
Queue Q=CreateQueue(Maxsize);
cout << "DElectQ:\n";
auto tem=DelectQ(Q);
if(tem!=error)
printf("%d ",tem);
for(int i=1;i<15;++i)
AddQ(Q,i);
cout << "\nDElectQ After AddQ:\n";
for(int i=0;i<10;++i)
{
auto tem=DelectQ(Q);
if(tem!=error)
printf("%d ",tem);
}
cout << "\nDElectQ:\n";
for(int i=0;i<10;++i)
{
auto tem=DelectQ(Q);
if(tem!=error)
printf("%d ",tem);
}
return 0;
}
Queue CreateQueue (int Maxsize)
{
Queue Q=(Queue) malloc(sizeof(QNode));
Q->Data=(ElementType *) malloc(Maxsize*sizeof(ElementType));
Q->Front=Q->Rear=0;
Q->Maxsize=Maxsize;
return Q;
}
bool IsFull(Queue Q)
{
return ((Q->Rear+1)%Q->Maxsize==Q->Front);
}
bool AddQ(Queue Q,ElementType X)
{
if(IsFull(Q))
{
printf(" The queue is full \n");
return false;
}
else
{
Q->Rear=(Q->Rear+1)%Q->Maxsize;
Q->Data[Q->Rear]=X;
return true;
}
}
bool IsEmpty(Queue Q)
{
return (Q->Rear==Q->Front);
}
ElementType DelectQ(Queue Q)
{
if(IsEmpty(Q))
{
printf(" The queue is empty \n");
return error;
}
else
{
Q->Front=(Q->Front+1)%Q->Maxsize;
return Q->Data[Q->Front];
}
}
*/ 2) The chain storage implementation of the queue ;
Linked list without leading node :
/**
2) The chain storage implementation of the queue ;
Linked list without leading node :
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define error 1e9
const int Maxsize = 100;
typedef int ElementType;
typedef struct Node * PtrToNode;
struct Node
{
ElementType data;
PtrToNode next;
}; // The linked list node implements the queue
typedef PtrToNode Position;
typedef struct QNode *PtrToQNode;
struct QNode
{
Position Front,Rear; // Pointer to the beginning and end of the queue
int Maxsize; // The maximum capacity of the queue
int sum; // Number of people who have joined the team
}; // Queue node
typedef PtrToQNode Queue;
Queue CreateQueue ();
bool IsFull(Queue Q);
bool AddQ(Queue Q,ElementType X);
bool IsEmpty(Queue Q);
ElementType DelectQ(Queue Q);
int main()
{
Queue Q=CreateQueue();
cout << "size create a empty queue: " << Q->sum << endl;
cout << "DElectQ after create a empty queue:\n";
auto tem=DelectQ(Q);
if(tem!=error)
printf("%d ",tem);
for(int i=1;i<15;++i)
AddQ(Q,i);
cout << "\nsize after add elements: " << Q->sum << endl;
cout << "\nDElectQ After AddQ:\n";
for(int i=0;i<10;++i)
{
auto tem=DelectQ(Q);
if(tem!=error)
printf("%d ",tem);
}
cout << "\nsize after delect elements: " << Q->sum << endl;
cout << "\nDElectQ:\n";
for(int i=0;i<10;++i)
{
auto tem=DelectQ(Q);
if(tem!=error)
printf("%d ",tem);
}
return 0;
}
Queue CreateQueue ()
{
Queue Q=(Queue) malloc(sizeof(QNode));
Q->Front=Q->Rear=NULL;
Q->Maxsize=Maxsize;
Q->sum=0;
return Q;
}
bool IsFull(Queue Q)
{
return (Q->sum==Q->Maxsize);
}
// There are two situations such as joining the team , One is that the queue is empty , Then the head and tail pointers should point to this new node , Otherwise, the tail pointer points to this new node , The head pointer doesn't move ,
// Then move the pointer back one position
bool AddQ(Queue Q,ElementType X)
{
if(IsFull(Q))
{
printf(" The queue is full \n");
return false;
}
else
{
Position RearCell=(PtrToNode) malloc(sizeof(Node));
RearCell->data=X;
RearCell->next=NULL;
if(Q->Front==NULL)
Q->Front=Q->Rear=RearCell;
else
{
Q->Rear->next=RearCell;
Q->Rear=RearCell;
}
Q->sum+=1;
return true;
}
}
bool IsEmpty(Queue Q)
{
return (Q->Front==NULL);
}
ElementType DelectQ(Queue Q)
{
if(IsEmpty(Q))
{
printf(" The queue is empty \n");
return error;
}
else
{
Position FrontCell=Q->Front;
ElementType FrontElem=FrontCell->data;
if(Q->Front==Q->Rear)
Q->Front=Q->Rear=NULL;
else
Q->Front=Q->Front->next;
free(FrontCell);
Q->sum-=1;
return FrontElem;
}
}边栏推荐
- redis 网络IO
- openGauss之TryMe初体验
- Software test interview questions (key points)
- Chapter 2 foreground data display
- Function realization of order system
- Element display mode: block level, inline, inline block, nesting specification, display mode conversion
- 如何在qsim查看软件对象的实例?
- NIO示例
- Breadth first search
- On Valentine's day, I drew an object with characters!
猜你喜欢

Breadth first search

First experience of tryme in opengauss

MCDF top level verification scheme

Explain cache consistency and memory barrier

Apache SSI remote command execution vulnerability

说透缓存一致性与内存屏障

Eval and assert execute one sentence Trojan horse

Alibaba cloud international receipt message introduction and configuration process

Forced login, seven cattle cloud upload pictures

Functions and arrow functions
随机推荐
Background coupon management
Create a project to realize login and registration, generate JWT, and send verification code
百人参与,openGauss开源社区这群人都在讨论什么?
借生态力量,openGauss突破性能瓶颈
One book 1201 Fibonacci sequence
Day6 --- Sqlalchemy advanced
Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
It's better to be full than delicious; It's better to be drunk than drunk
4275. Dijkstra sequence
openGauss之TryMe初体验
开怀一笑
JWT authentication and login function implementation, exit login
[BJDCTF2020]EasySearch 1
while Loop
Have a good laugh
如何卸载--奇安信安全终端管理系统
Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum
Day5 - Flame restful request response and Sqlalchemy Foundation
The shelf life you filled in has been less than 10 days until now, and it is not allowed to publish. If the actual shelf life is more than 10 days, please truthfully fill in the production date and pu
面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?