队列基本概念
- 队列是一种先进先出(FIFO)特殊的线性表,仅在线性表的两端操作,不允许在中间操作。
- 对头(front):取出元素的一端
- 队尾(rear):插入元素的一端
线性队列例子:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
typedef struct Node
{
Node* next;
}node;
typedef struct Common_list
{
node head;
int length;
}common_list;
common_list* creat_list()
{
common_list*temp_creat;
temp_creat = (common_list*)malloc(sizeof(common_list));
if (temp_creat == nullptr)
{
printf("空间开辟错误!!!\n");
return nullptr;
}
printf("已成功创建空列表!!1\n");
temp_creat->head.next = nullptr;
temp_creat->length = 0;
printf("空列表初始化成功!!1\n");
return temp_creat;
}
common_list* insert_list(common_list**old, node*input, int pos)
{
common_list*temp_insert = *old;
if (temp_insert == nullptr)
{
printf("传入空间错误!!!\n\n");
return nullptr;
}
else if (pos < 0)
{
printf("位置号不能小于0");
return nullptr;
}
else if (pos > temp_insert->length)
{
pos = temp_insert->length;
}
node*current = &temp_insert->head;
for (int i = 0; i < pos; i++)
{
current = current->next;
}
input->next = current->next;
current->next = input;
temp_insert->length++;
printf("号码入队成功!!!\n\n");
return temp_insert;
}
node* find_list(common_list*old, int pos)
{
common_list*temp_find = old;
if (temp_find == nullptr)
{
printf("传入空间错误!!!\n\n");
return nullptr;
}
else if (pos < 0 || pos >= temp_find->length)
{
printf("该位置处无号码!!!\n\n");
return nullptr;
}
node* current = temp_find->head.next;
for (int i = 0; i < pos; i++)
{
current = current->next;
}
printf("找到号码啦!!!\n\n");
return current;
}
node* delete_list(common_list**old, int pos)
{
common_list*temp_del = *old;
if (temp_del == nullptr)
{
printf("传入空间错误!!!\n\n");
return NULL;
}
else if (pos < 0 || pos >= temp_del->length)
{
printf("该位置处无号码!!!\n\n");
return NULL;
}
node* current = &temp_del->head;
for (int i = 0; i < pos; i++)
{
current = current->next;
}
node*del = current->next;
current->next = del->next;
temp_del->length--;
printf("数据出队成功!!!\n\n");
return del;
}
void clear_list(common_list**old)
{
common_list* temp_cls = *old;
if (temp_cls == nullptr)
{
printf("传入空间错误!!!\n\n");
return;
}
free(temp_cls);
temp_cls = nullptr;
printf("数据已经清空!!!\n\n");
}
int length_list(common_list*old)
{
return old->length;
}
typedef struct user
{
node head;
void* user_data;
}user;
common_list* creat_queue()
{
common_list* old_queue = creat_list();
return old_queue;
}
int length_queue(common_list*old_queue)
{
return old_queue->length;
}
void insert_queue_rear(common_list**old_queue,void*user_data)
{
common_list*temp_insert = *old_queue;
if (temp_insert == nullptr || user_data == nullptr)
{
printf("原队列为空!!!或用户数据为空!!!\n\n");
return;
}
user* input = (user*)malloc(sizeof(user));
if (input == nullptr)
{
printf("用户空间开辟错误!!!\n");
}
input->user_data = user_data;
common_list*temp = insert_list(&temp_insert,(node*)input,length_queue(temp_insert));
if (temp == nullptr)
{
printf("插入错误!!!\n\n");
return;
}
printf("元素【%d】入队成功!!!\n\n", *(int*)input->user_data);
}
user* find_queue_front(common_list*old_queue)
{
printf("正在查找队头元素!!!\n\n");
common_list*temp_front = old_queue;
user*find = (user*)find_list(temp_front,0);
if (find == nullptr)
{
printf("查找失败!!!\n\n");
return nullptr;
}
return find;
}
void delete_queue_front(common_list**old_queue)
{
common_list*temp_del = *old_queue;
if (temp_del == nullptr )
{
printf("原队列为空!!!\n\n");
return;
}
user*temp = (user*)delete_list(&temp_del,0);
void*buf = temp->user_data;
if (buf == nullptr)
{
printf("数据为空!!!\n\n");
return ;
}
printf("元素【%d】出队成功!!!\n\n",*(int*)buf);
}
void cls_queue(common_list**old_queue)
{
common_list*temp_cls = *old_queue;
if (temp_cls == nullptr)
{
printf("原队列为空!!!\n\n");
return;
}
while (length_queue(temp_cls)>0)
{
printf("队列中有元素——正在出队!!!\n\n");
delete_queue_front(&temp_cls);
}
printf("元素出队完成,队列为空!!!\n\n");
}
void auto_show_queue(common_list*old_queue)
{
user*u1;
for (int i = 0; i < length_queue(old_queue); i++)
{
u1 = find_queue_front(old_queue);
printf("号码为:【%d】\n\n", *(int*)u1->user_data);
}
}
int main()
{
common_list* old_queue = creat_queue();
int i = 111;
insert_queue_rear(&old_queue, &i);
int i1 = 222;
insert_queue_rear(&old_queue, &i1);
int i2 =333 ;
insert_queue_rear(&old_queue, &i2);
int i4 = 444;
insert_queue_rear(&old_queue, &i4);
auto_show_queue(old_queue);
delete_queue_front(&old_queue);
auto_show_queue(old_queue);
cls_queue(&old_queue);
auto_show_queue(old_queue);
return 0;
}