队:先进先出;
1,顺序表队:(空出一位来判满)循环顺序表tail进数据front 出数据;
tail插入front删除
头文件
#pragma once
typedef int Elemtype;
typedef struct Queue
{
Elemtype* base;
int front;
int tail;
}Queue,*Pqueue;
//
//初始化
void Init(Queue* plist);
// 入队tail入队
bool Push(Queue*plist,Elemtype val);
//出队 (出对操作成功还要返回队头值)头部标记位置出去
bool Pop(Queue*plist,Elemtype*rval);
//获取队头元素值
bool Top(Queue* plist,Elemtype*rval);
// 获取有效值个数
int Get_length(Queue*plist);
//判空
bool Is_full(Queue*plist);
//判满
bool Is_empty(Queue*plist);
//清空
bool Clear(Queue*plist);
//销毁
bool Destroy(Queue* plist);
//打印
void Show(Queue*plist);
.cpp
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include"queue.h"
#define SIZE 7
//初始化
void Init(Queue* plist)
{
assert(plist != nullptr);
plist->base = (Elemtype*)malloc(sizeof(Elemtype) * SIZE);
if (plist->base == nullptr) printf("初始化失败\n");
plist->front=0;
plist->tail= 0;
printf("初始化成功\n");
}
// 入队
bool Push(Queue* plist, Elemtype val)
{
assert(plist != nullptr);
if (Is_full(plist)) return false;
plist->base[plist->tail] = val;
plist->tail = (plist->tail + 1) % SIZE;//防止tail越界
return true;
}
//出队 (出对操作成功还要返回队头值)
bool Pop(Queue* plist, Elemtype* rval)
{
assert(plist != nullptr);
if (Is_empty(plist)) return false;
*rval = plist->base[plist->front];
plist->front = (plist->front + 1) % SIZE;
return false;
}
//获取队头元素值
bool Top(Queue* plist,Elemtype *rval)
{
assert(plist != nullptr);
if (Is_empty(plist)) return false;
*rval = plist->base[plist->front];
return true;
}
// 获取有效值个数
int Get_length(Queue* plist)
{
assert(plist!=nullptr);
return (plist->tail - plist->front + SIZE) % SIZE;
}
//判空
bool Is_empty(Queue* plist)
{
assert(plist != nullptr);
return plist->front == plist->tail;
}
//判满
bool Is_full(Queue* plist)
{
assert(plist!=nullptr);
return (plist->tail+1)%SIZE==plist->front;
}
//清空
bool Clear(Queue* plist)
{
assert(plist != nullptr);
plist->tail = plist->front;
return true;
}
//销毁
bool Destroy(Queue* plist)
{
assert(plist != nullptr);
free(plist->base);
plist->base = nullptr;
printf("销毁成功\n");
return true;
}
//打印
void Show(Queue* plist)
{
assert(plist != nullptr);
for (int i=plist->front;i!=plist->tail;i=(i+1)%SIZE)
{
printf("%d ",plist->base[i]);
}
printf("\n");
}
2,链式队:单链表链表在头节点加一个直接指向队尾的结点指针
头插尾删的方法;
头文件
```c
#pragma once
typedef int Elemtype;
typedef struct Lqueue
{
Elemtype data;
struct Lqueue* next;
}Lqueue,*Plqueue;
typedef struct Linkqueue
{
Lqueue* head;
Lqueue* tail;
int cursize;
}Linklist;
//初始化
void Init(Linklist* plist);
// 入队
bool Push(Linklist* plist, Elemtype val);
//出队 (出对操作成功还要返回队头值)
bool Pop(Linklist* plist, Elemtype* rval);
//获取队头元素值
bool Top(Linklist* plist, Elemtype* rval);
// 获取有效值个数
int Get_length(Linklist* plist);
//判空
bool Is_empty(Linklist* plist);
//清空
bool Clear(Linklist* plist);
//销毁
bool Destroy(Linklist* plist);
//打印
void Show(Linklist* plist);
函数文件
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Lqueue.h"
//初始化
Lqueue* Buynode()
{
return (Lqueue*)malloc(sizeof(Lqueue));
}
void Init(Linklist* plist)
{
assert(plist!=nullptr);
Lqueue*s= Buynode();
plist->head =plist->tail=s ;
plist->head->next = nullptr;
plist->tail->next = plist->head;
plist->cursize = 0;
}
// 入队
bool Push(Linklist* plist, Elemtype val)
{
assert(plist != nullptr);
Lqueue* s = Buynode();
assert(s != nullptr);
s->data = val;
s->next = nullptr;
plist->tail->next=s;
plist->tail = s;
plist->cursize += 1;
return true;
}
//出队 (出对操作成功还要返回队头值)
bool Pop(Linklist* plist, Elemtype* rval)
{
assert(plist!=nullptr);
if (Is_empty(plist)) return false;
Lqueue* s = plist->head->next;
*rval = s->data;
if (plist->cursize == 1)
{
plist->head->next =nullptr;
plist->tail->next = plist->head;
}
else
{
plist->head->next = s->next;
}
free(s);
s = nullptr;
plist->cursize -= 1;
return false;
}
//获取队头元素值
bool Top(Linklist* plist, Elemtype* rval)
{
assert(plist!=nullptr);
if (Is_empty(plist)) return false;
Lqueue* s = plist->head->next;
*rval = s->data;
return true;
}
// 获取有效值个数
int Get_length(Linklist* plist)
{
assert(plist != nullptr);
return plist->cursize;
}
//判空
bool Is_empty(Linklist* plist)
{
assert(plist != nullptr);
return plist->cursize == 0;
}
//清空
bool Clear(Linklist* plist)
{
assert(plist != nullptr);
while (plist->cursize != 0)
{
int a = 0;
Pop(plist, &a);
}
printf("清空成功\n");
return true;
}
//销毁
bool Destroy1(Linklist* plist)
{
assert(plist!=nullptr);
Clear(plist);
return true;
}
bool Destroy2(Linklist*plist)
{
assert(plist!=nullptr);
Lqueue* p = plist->head->next;
Lqueue* q = nullptr;
plist->head->next = nullptr;
plist->tail->next = plist->head;
plist->cursize = 0;
while (p != nullptr)
{
q = p->next;
free(p);
p = q;
}
printf("销毁成公\n");
return true;
}
//打印
void Show(Linklist* plist)
{
assert(plist!=nullptr);
Lqueue* s = plist->head->next;
for (s; s != nullptr; s = s->next)
{
printf("%d ", s->data);
}
printf("\n");
}
int main()
{
Linklist list;
Init(&list);
for (int i = 0; i < 8; i++)
{
Push(&list, i + 1);
}
Show(&list);
printf("长度是%d\n",Get_length(&list));
int val = 0;
Pop(&list, &val);
printf("%d\n", val);
printf("长度是%d\n", Get_length(&list));
Show(&list);
Top(&list,&val);
printf("%d\n", val);
Clear(&list);
printf("长度是%d\n", Get_length(&list));
Destroy1(&list);
printf("长度是%d\n", Get_length(&list));
Destroy2(&list);
printf("长度是%d\n", Get_length(&list));
return 0;
}