#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct info {
int id;
int age;
char name[20];
}info_s;
typedef struct AArray {
void **Array;
int size;
int capacity;
}Line_Aarrays;
Line_Aarrays* Line_Array_Init(int capacity_in);
void Insert_Array(Line_Aarrays* p_node, void *value);
void Insert_Pos_Array(Line_Aarrays* p_node, int pos,void *value);
void Foreach_Line_Array(Line_Aarrays *p_node, void(*my_printf)(void *));
void my_printf1(void * node_in);
void Remove_Pos_Array(Line_Aarrays* p_node, int pos);
void Remove_value_Array(Line_Aarrays* p_node, void *value, int(*my_compare)(void*, void*));
int my_compare1(void*, void*);
void Clear_Line_Array(Line_Aarrays *p_node);
void Destroy_Line_Array(Line_Aarrays *p_node);
int Len_Line_Array(Line_Aarrays *p_node);
Line_Aarrays* Line_Array_Init(int capacity_in) {
Line_Aarrays *Line_Array = (Line_Aarrays*)malloc(sizeof(Line_Aarrays));
Line_Array -> Array = (void**)malloc(sizeof(void*) * capacity_in);
Line_Array -> size = 0;
Line_Array -> capacity = capacity_in;
return Line_Array;
}
void Insert_Array(Line_Aarrays* p_node, void *value_in) {
if(p_node == NULL)
return;
if(p_node -> size >= p_node -> capacity) {
void **New_Array = (void **)malloc(sizeof(void*) * p_node -> capacity * 2);
memcpy(New_Array, p_node -> Array, p_node -> capacity);
free(p_node -> Array);
p_node -> Array = New_Array;
p_node -> capacity = p_node -> capacity * 2;
}
p_node -> Array[p_node -> size] = value_in;
p_node -> size ++;
}
void Insert_Pos_Array(Line_Aarrays* p_node, int pos,void *value_in) {
if(p_node == NULL)
return;
if(p_node -> size >= p_node -> capacity) {
void **New_Array = (void **)malloc(sizeof(void*) * p_node -> capacity * 2);
memcpy(New_Array, p_node -> Array, p_node -> capacity);
free(p_node -> Array);
p_node -> Array = New_Array;
p_node -> capacity = p_node -> capacity * 2;
}
if(pos < 0 || pos >= p_node -> size)
p_node -> Array[p_node -> size] = value_in;
else {
for(int i = p_node -> size; i > pos; i--)
p_node -> Array[i] = p_node -> Array[i - 1];
p_node -> Array[pos] = value_in;
}
p_node -> size ++;
}
void Foreach_Line_Array(Line_Aarrays *p_node, void(*my_printf)(void *)) {
if(p_node == NULL)
return;
for(int i = 0; i < p_node -> size; i++)
my_printf(p_node -> Array[i]);
}
void my_printf1(void * node_in) {
info_s* p1 = (info_s*) node_in;
printf("id : %d, 年龄 :%d, 姓名:%s\n", p1 ->id, p1 -> age, p1 -> name);
}
void Remove_Pos_Array(Line_Aarrays* p_node, int pos) {
if(p_node == NULL)
return;
if(pos < 0 || pos >= p_node -> size)
return;
else {
for(int i = pos; i < p_node -> size - 1; i++)
p_node -> Array[i] = p_node -> Array[i + 1];
}
p_node -> size --;
}
void Remove_value_Array(Line_Aarrays* p_node, void *value_in, int(*my_compare)(void*, void*)){
if(p_node == NULL)
return;
for(int i = 0; i < p_node -> size; i++) {
if(my_compare(value_in, p_node -> Array[i])) {
p_node -> Array[i] = NULL;
for(int ii = i; ii < p_node -> size - 1; ii++)
p_node -> Array[ii] = p_node -> Array[ii + 1];
break;
}
}
p_node -> size --;
}
int my_compare1(void* value_in, void* node_in) {
int *p1 = (int*)value_in;
info_s *p2 = (info_s*)node_in;
if(*p1 == p2 -> id)
return 1;
else
return 0;
}
void Clear_Line_Array(Line_Aarrays *p_node) {
if(p_node == NULL)
return;
free(p_node -> Array);
p_node -> Array = NULL;
p_node -> size = 0;
}
void Destroy_Line_Array(Line_Aarrays *p_node) {
if(p_node == NULL)
return;
Clear_Line_Array(p_node);
free(p_node);
p_node = NULL;
}
int Len_Line_Array(Line_Aarrays *p_node) {
if(p_node == NULL)
return -1;
return p_node -> size;
}
#include "LineAarray.h"
typedef void* SeqQueue;
SeqQueue SeqQueue_Init();
void SeqQueue_push(SeqQueue p_node, SeqQueue value_in);
void SeqQueue_pop(SeqQueue p_node, SeqQueue value_in);
void SeqQueue_back(SeqQueue p_node, void(*my_printf)(void *));
void SeqQueue_head(SeqQueue p_node, void(*my_printf)(void *));
void Len_SeqQueue(SeqQueue p_node);
void Destory_SeqQueue(SeqQueue p_node);
SeqQueue SeqQueue_Init() {
Line_Aarrays *My_Queue = Line_Array_Init(10);
if (My_Queue == NULL) {
return NULL;
}
return My_Queue;
}
void SeqQueue_push(SeqQueue p_node, SeqQueue value_in) {
if(p_node == NULL)
return;
Line_Aarrays* My_Queue = (Line_Aarrays*)p_node;
Insert_Pos_Array(My_Queue, 0, value_in);
}
void SeqQueue_pop(SeqQueue p_node) {
if(p_node == NULL)
return;
Line_Aarrays* My_Queue = (Line_Aarrays*)p_node;
Remove_Pos_Array(My_Queue, My_Queue -> size - 1);
}
void SeqQueue_back(SeqQueue p_node, void(*my_printf)(void *)) {
if(p_node == NULL)
return;
Line_Aarrays* My_Queue = (Line_Aarrays*)p_node;
if (My_Queue -> size == 0)
return;
my_printf(My_Queue -> Array[0]);
}
void SeqQueue_head(SeqQueue p_node, void(*my_printf)(void *)) {
if(p_node == NULL)
return;
Line_Aarrays* My_Queue = (Line_Aarrays*)p_node;
if (My_Queue -> size == 0)
return;
my_printf(My_Queue -> Array[My_Queue -> size - 1]);
}
void Len_SeqQueue(SeqQueue p_node) {
if(p_node == NULL)
return;
Line_Aarrays* My_Queue = (Line_Aarrays*)p_node;
printf("队列的长度为:%d\n", Len_Line_Array(My_Queue));
}
void Destory_SeqQueue(SeqQueue p_node) {
if(p_node == NULL)
return;
Line_Aarrays* My_Queue = (Line_Aarrays*)p_node;
Destroy_Line_Array(My_Queue);
}
int main() {
printf("---开始创建队列---\n");
SeqQueue Queue_head = SeqQueue_Init();
printf("---队列创建完毕---\n");
info_s n1 = {1, 21, "花木兰"};
info_s n2 = {2, 21, "凯皇"};
info_s n3 = {3, 21, "百里守约"};
info_s n4 = {4, 19, "百里玄策"};
info_s n5 = {5, 22, "苏烈"};
info_s n6 = {6, 20, "李信"};
printf("---开始入队列--\n");
SeqQueue_push(Queue_head, &n1);
SeqQueue_back(Queue_head, my_printf1);
SeqQueue_push(Queue_head, &n2);
SeqQueue_back(Queue_head, my_printf1);
SeqQueue_push(Queue_head, &n3);
SeqQueue_back(Queue_head, my_printf1);
SeqQueue_push(Queue_head, &n4);
SeqQueue_back(Queue_head, my_printf1);
SeqQueue_push(Queue_head, &n5);
SeqQueue_back(Queue_head, my_printf1);
SeqQueue_push(Queue_head, &n6);
SeqQueue_back(Queue_head, my_printf1);
printf("---入队列完毕---\n");
Len_SeqQueue(Queue_head);
printf("---开始出队列--\n");
SeqQueue_head(Queue_head, my_printf1);
SeqQueue_pop(Queue_head);
SeqQueue_head(Queue_head, my_printf1);
SeqQueue_pop(Queue_head);
SeqQueue_head(Queue_head, my_printf1);
SeqQueue_pop(Queue_head);
SeqQueue_head(Queue_head, my_printf1);
SeqQueue_pop(Queue_head);
SeqQueue_head(Queue_head, my_printf1);
SeqQueue_pop(Queue_head);
SeqQueue_head(Queue_head, my_printf1);
SeqQueue_pop(Queue_head);
printf("---出队列完毕---\n");
Len_SeqQueue(Queue_head);
printf("---开始销毁队列---\n");
Destory_SeqQueue(Queue_head);
Queue_head = NULL;
printf("---销毁队列完毕---\n");
system("pause");
return 0;
}