数据结构线性顺序队列
线性顺序队列简介
使用顺序存储结构的线性存储结构的队列为线性顺序队列,线性存储结构是元素逻辑结构一对一,顺序存储结构是元素物理结构连续,队列是只能在队尾插入元素,队头删除元素(先进先出).
C语言实现代码
#include<stdio.h>//包含标准输入输出库文件
#include<stdlib.h>//包含标准库库文件
typedef struct//类型定义结构
{
int*Array,Length;//整数指针数组,整数长度
}Sequential_Queue;//顺序队列
Sequential_Queue Sequential_Queue_Create(void)//顺序队列顺序队列创造,空
{
return(Sequential_Queue){malloc(sizeof(int))};//返回顺序队列成员数组赋值分配字节整数
}
void Sequential_Queue_Destroy(Sequential_Queue*sequential_queue)//空顺序队列销毁,顺序队列指针顺序队列
{
free(sequential_queue->Array);//释放顺序队列成员数组
}
void Sequential_Queue_Insert(Sequential_Queue*sequential_queue,int Insert_Data)//空顺序队列插入,顺序队列指针顺序队列,整数插入数据
{
sequential_queue->Array=realloc(sequential_queue->Array,++sequential_queue->Length*sizeof(int));//顺序队列成员数组赋值顺序队列成员数组重新分配加加顺序队列成员长度乘字节整数
sequential_queue->Array[sequential_queue->Length-1]=Insert_Data;//顺序队列成员数组顺序队列成员长度减1下标赋值插入数据
}
void Sequential_Queue_Delete(Sequential_Queue*sequential_queue)//空顺序队列删除,顺序队列指针顺序队列
{
--sequential_queue->Length;//减减顺序队列成员长度
for(int Index=0;Index<sequential_queue->Length;++Index)//循环,整数索引赋值0,索引小于顺序队列成员长度,加加索引
sequential_queue->Array[Index]=sequential_queue->Array[Index+1];//顺序队列成员数组索引下标赋值顺序队列成员数组索引加1下标
}
int Sequential_Queue_Obtain(Sequential_Queue sequential_queue)//整数顺序队列获取,顺序队列顺序队列
{
return sequential_queue.Array[0];//返回顺序队列成员数组0下标
}
int Sequential_Queue_Size(Sequential_Queue sequential_queue)//整数顺序队列尺寸,顺序队列顺序队列
{
return sequential_queue.Length;//返回顺序队列成员长度
}
int main(void)//整数主,空
{
Sequential_Queue sequential_queue=Sequential_Queue_Create();//顺序队列顺序队列赋值顺序队列创造
for(int Select=5,Data;Select;scanf("%i",&Select))//循环,整数选择赋值5,整数数据,选择不等于0,格式扫描选择
{
if(Select==1)//如果,选择等于1
{
scanf("%i",&Data);//格式扫描数据
Sequential_Queue_Insert(&sequential_queue,Data);//顺序队列插入顺序队列最后数据
}
else if(Select==2)//否则如果,选择等于2
Sequential_Queue_Delete(&sequential_queue);//顺序队列插入顺序队列零数据
else if(Select==3)//否则如果,选择等于3
printf("%i",Sequential_Queue_Obtain(sequential_queue));//格式打印顺序队列获取顺序队列零数据
else if(Select==4)//否则如果,选择等于4
printf("%i",Sequential_Queue_Size(sequential_queue));//格式打印顺序队列尺寸顺序队列
}
Sequential_Queue_Destroy(&sequential_queue);//顺序队列销毁顺序队列
}
C++语言实现代码
#include<iostream>//包含输入输出流库文件
struct Sequential_Queue//结构顺序队列
{
int*Array=new int,Length=0;//整数指针数组赋值新整数,整数长度赋值0
~Sequential_Queue(void)//顺序队列析构,空
{
delete Array;//删除数组
}
void Insert(int Insert_Data)//空插入,整数插入数据
{
int*Temporary_Array=Array;//整数指针临时数组赋值数组
Array=new int[++Length];//数组赋值新加加长度乘整数
for(int Index=0;Index<Length;++Index)//循环,整数索引赋值0,索引小于长度,加加索引
Array[Index]=Temporary_Array[Index];//数组索引下标赋值临时数组索引下标
delete Temporary_Array;//删除临时数组
Array[Length-1]=Insert_Data;//数组长度减1下标赋值插入数据
}
void Delete(void)//空删除,空
{
--Length;//减减长度
for(int Index=0;Index<Length;++Index)//循环,整数索引赋值0,索引小于长度,加加索引
Array[Index]=Array[Index+1];//数组索引下标赋值数组索引加1下标
}
int Obtain(void)//整数获取,空
{
return Array[0];//返回数组0下标
}
int Size(void)//整数尺寸,空
{
return Length;//返回长度
}
}sequential_queue;//顺序队列
int main(void)//整数主,空
{
for(int Select=5,Data;Select;std::cin>>Select)//循环,整数选择赋值5,整数数据,选择不等于0,标准输入选择
{
if(Select==1)//如果,选择等于1
{
std::cin>>Data;//标准输入数据
sequential_queue.Insert(Data);//顺序队列成员插入最后数据
}
else if(Select==2)//否则如果,选择等于2
sequential_queue.Delete();//顺序队列成员删除零数据
else if(Select==3)//否则如果,选择等于3
std::cout<<sequential_queue.Obtain();//标准输出顺序队列成员获取零数据
else if(Select==4)//否则如果,选择等于4
std::cout<<sequential_queue.Size();//标准输出顺序队列成员尺寸
}
}