C语言实现顺序队列

2023-12-19 23:54:22

在C语言中,顺序队列是一种数据结构,它是一种先进先出(FIFO)的线性表。顺序队列通常使用数组来实现,具有以下特点:

  1. 队列元素的插入操作(入队)只能在队尾进行,而删除操作(出队)只能在队首进行。
  2. 队列有一个固定的大小,一旦队列满了就无法插入新的元素。
  3. 队列会随着元素的出队操作而缩短,随着元素的入队操作而延长。

在C语言中,顺序队列通常通过数组和两个指针(分别指向队首和队尾)来实现。入队操作通常会使队尾指针向后移动,并将新元素放入队尾;而出队操作会使队首指针向后移动,同时返回队首元素,并将队首元素从队列中删除。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"dongtaishuzu.h"
#define MAX 1024
typedef void* seqQueue;
seqQueue init_seqqueue()
{
	dynaymicArray* arr = init_dynamicArray(MAX);
	return (void*)arr;
}
//入队-》尾插
void push_seqqueue(seqQueue queue,void*data)
{
	if (queue == NULL)
	{
		return;
	}
	dynaymicArray* array = (dynaymicArray*)queue;
	if (array->m_size == MAX)
	{
		return;
	}
	insert_dynamicArray(array, data, array->m_size);
}
//出队 头删
void pop_seqqueue(seqQueue queue)
{
	if (queue == NULL)
	{
		return;
	}
	dynaymicArray* array = (dynaymicArray*)queue;
	if (array->m_size == 0)
	{
		return;
	}
	remove_dynamicArray(array, 0);
}
//返回对头
void* front_seqqueue(seqQueue queue)
{
	if (queue == NULL)
	{
		return NULL;
	}
	dynaymicArray* array = (dynaymicArray*)queue;
	return array->pAddr[0];
}
//返回队尾
void* back_seqqueue(seqQueue queue)
{
	if (queue == NULL)
	{
		return NULL;
	}
	dynaymicArray* array = (dynaymicArray*)queue;
	return array->pAddr[array->m_size - 1];
}
int isEmpty(seqQueue queue)
{
	if (queue == NULL)
	{
		return -1;
	}
	dynaymicArray* array = (dynaymicArray*)queue;
	if (array->m_size == 0)
	{
		return 1;
	}
	return 0;
}
int size_seqqueue(seqQueue queue)
{
	if (queue == NULL)
	{
		return -1;
	}
	dynaymicArray* array = (dynaymicArray*)queue;
	return array->m_size;
}
void destroy_seqqueue(seqQueue queue)
{
	if (queue == NULL)
	{
		return;
	}
	// 释放动态数组的内存
	free(queue);                 // 释放队列的内存
	queue = NULL;                // 将指针置为 NULL
}
struct person
{
	char name[64];
	int age;
};
void test01()
{
	seqQueue myqueue = init_seqqueue();
	struct person p6 = { "aaa",18 };
	struct person p7 = { "bbb",123 };
	struct person p8 = { "ccc",24 };
	struct person p9 = { "ddd",25 };
	push_seqqueue(myqueue, &p6);
	push_seqqueue(myqueue, &p7);
	push_seqqueue(myqueue, &p8);
	push_seqqueue(myqueue, &p9);
	printf("大小:%d\n", size_seqqueue(myqueue));
	while (!isEmpty(myqueue))
	{
		struct person* p1 = (struct person*)front_seqqueue(myqueue);
		printf("队头名字:%s,年龄:%d\n", p1->name, p1->age);
		struct person* p2 = (struct person*)back_seqqueue(myqueue);
		printf("队尾名字:%s,年龄:%d\n", p2->name, p2->age);
		printf("-------\n");
		pop_seqqueue(myqueue);
	}
	printf("大小:%d\n", size_seqqueue(myqueue));
	destroy_seqqueue(myqueue); // 销毁队列
}
int main()
{
	test01();
	return 0;
}

这是.h文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef  struct dynaymicArray
{
	void** pAddr;
	int m_capacity;
	int m_size;
}dynaymicArray;
//struct person
//{
//	char name[64];
//	int age;
//};
//dynaymicArray* init_dynamicArray(int capacity);
//void insert_dynamicArray(dynaymicArray* arr, void* data, int pos);
//void foreach_dynamicArray(dynaymicArray* arr, void(*myprint)(void*));
//void remove_dynamicArray(dynaymicArray* arr, int pos);
//void removevalue_dynamicArray(dynaymicArray* arr, void* data, int(*mycompare)(void*, void*));
dynaymicArray* init_dynamicArray(int capacity)
{
	if (capacity <= 0)
	{
		return NULL;
	}
	dynaymicArray* arr = (dynaymicArray*)malloc(sizeof(dynaymicArray));
	if (arr == NULL)
	{
		return NULL;
	}
	arr->m_capacity = 0;
	arr->m_size = 0;
	arr->pAddr =(void**) malloc(sizeof(void*) * capacity);
	return arr;
}
void insert_dynamicArray(dynaymicArray* arr, void* data, int pos)
{
	if (data == NULL)
	{
		return;
	}
	if (pos<0 || pos>arr->m_size)
	{
		return;
	}
	if (arr->m_size == arr->m_capacity)
	{
		int newcapacity = arr->m_capacity * 2;
		void** newspace =(void**) malloc(sizeof(void*) * newcapacity);
		memcpy(newspace, arr->pAddr, sizeof(void*) * arr->m_capacity);
		free(arr->pAddr);
		arr->pAddr = newspace;
		arr->m_capacity = newcapacity;
	}
	for (int i = arr->m_size - 1; i >= pos; i--)
	{
		arr->pAddr[i + 1] = arr->pAddr[i];
	}
	arr->pAddr[pos] = data;
	arr->m_size++;
}
void foreach_dynamicArray(dynaymicArray* arr, void(*myprint)(void*))
{
	for (int i = 0; i < arr->m_size; i++)
	{
		myprint(arr->pAddr[i]);
	}
}


void remove_dynamicArray(dynaymicArray* arr, int pos)
{
	if (pos<0 || pos>arr->m_size - 1)
	{
		return;
	}
	for (int i = pos; i < arr->m_size; i++)
	{
		arr->pAddr[i] = arr->pAddr[i + 1];
	}
	arr->m_size--;
}
void removevalue_dynamicArray(dynaymicArray* arr, void* data, int(*mycompare)(void*, void*))
{
	for (int i = 0; i < arr->m_size; i++)
	{
		if (mycompare(arr->pAddr[i], data))
		{
			remove_dynamicArray(arr, i);
			arr->m_size--;
			break;
		}
	}
}



文章来源:https://blog.csdn.net/luosuss/article/details/135096163
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。