数据结构线性顺序队列 线性顺序队列简介
使用顺序存储结构的线性存储结构的队列为线性顺序队列,线性存储结构是元素逻辑结构一对一,顺序存储结构是元素物理结构连续,线性顺序队列只能在队尾插入元素,只能在队头删除元素(先进先出).
C语言实现代码#include//包含标准输入输出文件 #include //包含标准库文件 typedef struct//类型定义结构体 { int*Array,Length;//定义整数指针变量数组,定义整数变量长度 }Sequential_Queue;//定义顺序队列 Sequential_Queue Sequential_Queue_Create(void)//顺序队列创造 { return(Sequential_Queue){calloc(0,sizeof(int)),0};//顺序队列数组初始化为分配0个整数字节动态内存返回值,顺序队列长度初始化为0,返回顺序队列并且退出函数 } void Sequential_Queue_Destroy(Sequential_Queue*sequential_queue)//顺序队列销毁 { free(sequential_queue->Array);//释放顺序队列数组 } void Sequential_Queue_Clear(Sequential_Queue*sequential_queue)//顺序队列清空 { free(sequential_queue->Array);//释放顺序队列数组 sequential_queue->Array=calloc(sequential_queue->Length=0,sizeof(int));//顺序队列数组赋值为分配顺序队列长度赋值为0个整数字节动态内存返回值 } void Sequential_Queue_Insert(Sequential_Queue*sequential_queue,int Insert_Element)//顺序队列插入 { int*temporary_Array=calloc(++sequential_queue->Length,sizeof(int));//定义整数指针变量临时数组初始化为分配顺序队列长度累加1个整数字节动态内存返回值 for(int Index=1;Index Length;++Index)//定义整数变量索引初始化为1,索引小于顺序队列长度,索引累加1 temporary_Array[Index]=sequential_queue->Array[Index-1];//临时数组第索引个元素赋值为顺序队列数组第索引减1个元素 temporary_Array[sequential_queue->Length-1]=Insert_Element;//临时数组第顺序队列长度减1个元素赋值为插入元素 free(sequential_queue->Array);//释放顺序队列数组 sequential_queue->Array=temporary_Array;//顺序队列数组赋值为临时数组 } void Sequential_Queue_Delete(Sequential_Queue*sequential_queue)//顺序队列删除 { int*temporary_Array=calloc(--sequential_queue->Length,sizeof(int));//定义整数指针变量临时数组初始化为分配顺序队列长度累减1个整数字节动态内存返回值 for(int Index=0;Index Length;++Index)//定义整数变量索引初始化为0,索引小于顺序队列长度,索引累加1 temporary_Array[Index]=sequential_queue->Array[Index+1];//临时数组第索引个元素赋值为顺序队列数组第索引加1个元素 free(sequential_queue->Array);//释放顺序队列数组 sequential_queue->Array=temporary_Array;//顺序队列数组赋值为临时数组 } void Sequential_Queue_Modify(Sequential_Queue sequential_queue,int Modify_Element)//顺序队列修改 { sequential_queue.Array[0]=Modify_Element;//顺序队列数组第0个元素赋值为修改元素 } int Sequential_Queue_Obtain(Sequential_Queue sequential_queue)//顺序队列获取 { return sequential_queue.Array[0];//返回顺序队列数组第0个元素并且退出函数 } void Sequential_Queue_Output(Sequential_Queue sequential_queue)//顺序队列输出 { for(int Index=0;Index C++语言实现代码 #include//包含输入输出流文件 template struct Sequential_Queue//模板结构体定义顺序队列 { Type*Array{new Type[0]{}};//定义类型指针变量数组初始化为分配0个类型字节动态内存返回值 int Length{};//定义整数变量长度初始化为0 ~Sequential_Queue(void)//顺序队列析构 { delete[]Array;//释放数组 } void Clear(void)//清空 { delete[]Array;//释放数组 Array=new Type[Length=0]{};//数组赋值为分配长度赋值为0个类型字节动态内存返回值 } void Insert(Type Insert_Element)//插入 { Type*temporary_Array{new Type[++Length]{}};//定义类型指针变量临时数组初始化为分配长度累加1个类型字节动态内存返回值 for(int Index{};Index >Select;//输入选择 if(Select==1)//选择等于1 sequential_queue.Clear();//顺序队列清空 else if(Select==2)//选择等于2 { std::cout<<"输入顺序队列插入的元素:";//输出字符数组 std::cin>>Element;//输入元素 sequential_queue.Insert(Element);//顺序队列插入第一个元素为元素 } else if(Select==3)//选择等于3 sequential_queue.Delete();//顺序队列删除第一个元素 else if(Select==4)//选择等于4 { std::cout<<"输入顺序队列修改的元素:";//输出字符数组 std::cin>>Element;//输入元素 sequential_queue.Modify(Element);//顺序队列修改第一个元素为元素 } else if(Select==5)//选择等于5 std::cout< 编程交流群群号(QQ):387618421



