數據結構(三)隊列
- 隊列
- 隊列(順序存儲)
- 循環隊列(順序存儲)
- 隊列(鏈式存儲)
隊列
隊列是一種受限制的線性表,只允許表的一端插入,在表的另一端刪除
隊列(順序存儲)
// 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