数据结构(三)队列
队列
队列是一种受限制的线性表,只允许表的一端插入,在表的另一端删除
队列(顺序存储)
// linear_Queue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define maxsize 50
#define elemtype int
typedef struct
{
elemtype data[maxsize];
int front, rear; //队头指针和队尾指针
}SqQueue;
void InitQueue(SqQueue &Q)
{
Q.rear = Q.front = 0; //初始化队首、队尾指针
}
bool QueueEmpty(SqQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
return false;
}
bool EnQueue(SqQueue& Q, elemtype x)
{
if (Q.rear == maxsize)
{
return false;
}
Q.data[Q.rear++] = x;//添加队列
return true;
}
elemtype DeQueue(SqQueue& Q)
{
bool ret=QueueEmpty(Q);
if (ret)
{
printf("队列为空!\n");
exit(1);
}
return Q.data[Q.front++];//添加队列
}
bool GetHead(SqQueue Q, elemtype& x)
{
if (QueueEmpty(Q))
{
printf("队列为空!\n");
exit(1);
}
x = Q.data[Q.front];
return true;
}
int main()
{
SqQueue Q;
InitQueue(Q);
EnQueue(Q, 5);
EnQueue(Q, 8);
EnQueue(Q, 10);
DeQueue(Q);
for (int i = Q.front; i < Q.rear; i++)
{
printf("%d\n",Q.data[i]);
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
循环队列(顺序存储)
// linear_Queue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define maxsize 50
#define elemtype int
typedef struct
{
elemtype data[maxsize];
int front, rear; //队头指针和队尾指针
}SqQueue;
void InitQueue(SqQueue &Q)
{
Q.rear = Q.front = 0; //初始化队首、队尾指针
}
bool QueueEmpty(SqQueue Q)
{
if (Q.front == Q.rear)
{
return true;
}
return false;
}
bool EnQueue(SqQueue& Q, elemtype x)
{
if (Q.rear == maxsize)
{
return false;
}
Q.data[Q.rear++] = x;//添加队列
return true;
}
elemtype DeQueue(SqQueue& Q)
{
bool ret=QueueEmpty(Q);
if (ret)
{
printf("队列为空!\n");
exit(1);
}
return Q.data[Q.front++];//添加队列
}
bool GetHead(SqQueue Q, elemtype& x)
{
if (QueueEmpty(Q))
{
printf("队列为空!\n");
exit(1);
}
x = Q.data[Q.front];
return true;
}
int main()
{
SqQueue Q;
InitQueue(Q);
EnQueue(Q, 5);
EnQueue(Q, 8);
EnQueue(Q, 10);
DeQueue(Q);
for (int i = Q.front; i < Q.rear; i++)
{
printf("%d\n",Q.data[i]);
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
队列(链式存储)
带头结点
// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType int
using namespace std;
typedef struct linknode //链式节点
{
ElemType data;
struct linknode* next;
}LinkNode;
//链式队列
typedef struct
{
LinkNode* front, *rear;
}LinkQueue;
void InitQueue(LinkQueue &Q)
{
//带头节点的队列初始化
Q.rear=Q.front=(LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
bool IsEmpty(LinkQueue& Q)
{
if (Q.rear == Q.front)
{
return true;
}
return false;
}
void EnQueue(LinkQueue& Q, ElemType x)
{
LinkNode* s= (LinkNode*)malloc(sizeof(LinkNode));
s->data = x;
s->next = Q.rear->next;
Q.rear->next = s;
Q.rear = s;
}
bool DeQueue(LinkQueue& Q, ElemType &x)
{
if (IsEmpty(Q))
{
//队列为空
return false;
}
LinkNode* p = Q.front->next;
x = p->data;
Q.front->next = p->next;
if (p == Q.rear) //要删除的为尾队列
{
Q.rear = Q.front;
}
free(p);
return true;
}
int main()
{
int x;
LinkQueue Q;
InitQueue(Q);
EnQueue(Q, 5);
EnQueue(Q, 7);
EnQueue(Q, 9);
DeQueue(Q, x);
LinkNode* p = Q.front->next;
while (p != NULL)
{
printf("%d\n",p->data);
p = p->next;
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
不带头结点
// linear_listqueue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define ElemType int
using namespace std;
typedef struct linknode //链式节点
{
ElemType data;
struct linknode* next;
}LinkNode;
//链式队列
typedef struct
{
LinkNode* front, * rear;
}LinkQueue;
void InitQueue(LinkQueue& Q)
{
//不带头节点的队列初始化
Q.front =Q.rear= NULL;
}
bool IsEmpty(LinkQueue& Q)
{
if (Q.rear == Q.front)
{
return true;
}
return false;
}
void EnQueue(LinkQueue& Q, ElemType x)
{
LinkNode* s = (LinkNode*)malloc(sizeof(LinkNode));
if(Q.front==NULL)
{
s->data = x;
s->next = NULL;
Q.front = Q.rear = s;
return;
}
s->next = NULL;
s->data = x;
Q.rear->next = s;
Q.rear = s;
}
bool DeQueue(LinkQueue& Q, ElemType& x)
{
if (IsEmpty(Q))
{
printf("Queue is empty!\n");
//队列为空
return false;
}
LinkNode* p = Q.front->next;
free(Q.front);
Q.front = p;
}
int main()
{
int x;
LinkQueue Q;
InitQueue(Q);
EnQueue(Q, 5);
EnQueue(Q, 7);
EnQueue(Q, 9);
DeQueue(Q, x);
LinkNode* p = Q.front;
while (p != NULL)
{
printf("%d\n", p->data);
p = p->next;
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file